Skip to content

Commit cf31d43

Browse files
committed
check if operations are done outside of a drive
1 parent 103a73c commit cf31d43

File tree

1 file changed

+124
-105
lines changed

1 file changed

+124
-105
lines changed

src/contents.ts

Lines changed: 124 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,6 @@ export class Drive implements Contents.IDrive {
285285
* @returns A promise which resolves with the created file content when the
286286
* file is created.
287287
*/
288-
289288
async newUntitled(
290289
options: Contents.ICreateOptions = {}
291290
): Promise<Contents.IModel> {
@@ -395,29 +394,30 @@ export class Drive implements Contents.IDrive {
395394
*
396395
* @returns A promise which resolves when the file is deleted.
397396
*/
398-
/*delete(path: string): Promise<void> {
399-
return Promise.reject('Repository is read only');
400-
}*/
401-
402397
async delete(localPath: string): Promise<void> {
403-
// extract current drive name
404-
const currentDrive = this._drivesList.filter(
405-
x =>
406-
x.name ===
407-
(localPath.indexOf('/') !== -1
408-
? localPath.substring(0, localPath.indexOf('/'))
409-
: localPath)
410-
)[0];
411-
412-
// eliminate drive name from path
413-
const relativePath =
414-
localPath.indexOf('/') !== -1
415-
? localPath.substring(localPath.indexOf('/') + 1)
416-
: '';
417-
418-
await deleteObjects(currentDrive.name, {
419-
path: relativePath
420-
});
398+
if (localPath !== '') {
399+
// extract current drive name
400+
const currentDrive = this._drivesList.filter(
401+
x =>
402+
x.name ===
403+
(localPath.indexOf('/') !== -1
404+
? localPath.substring(0, localPath.indexOf('/'))
405+
: localPath)
406+
)[0];
407+
408+
// eliminate drive name from path
409+
const relativePath =
410+
localPath.indexOf('/') !== -1
411+
? localPath.substring(localPath.indexOf('/') + 1)
412+
: '';
413+
414+
await deleteObjects(currentDrive.name, {
415+
path: relativePath
416+
});
417+
} else {
418+
// create new element at root would mean modifying a drive
419+
console.warn('Operation not supported.');
420+
}
421421

422422
this._fileChanged.emit({
423423
type: 'delete',
@@ -444,43 +444,48 @@ export class Drive implements Contents.IDrive {
444444
newLocalPath: string,
445445
options: Contents.ICreateOptions = {}
446446
): Promise<Contents.IModel> {
447-
// extract current drive name
448-
const currentDrive = this._drivesList.filter(
449-
x =>
450-
x.name ===
451-
(oldLocalPath.indexOf('/') !== -1
452-
? oldLocalPath.substring(0, oldLocalPath.indexOf('/'))
453-
: oldLocalPath)
454-
)[0];
455-
456-
// eliminate drive name from path
457-
const relativePath =
458-
oldLocalPath.indexOf('/') !== -1
459-
? oldLocalPath.substring(oldLocalPath.indexOf('/') + 1)
460-
: '';
461-
const newRelativePath =
462-
newLocalPath.indexOf('/') !== -1
463-
? newLocalPath.substring(newLocalPath.indexOf('/') + 1)
464-
: '';
465-
466-
// extract new file name
467-
let newFileName = PathExt.basename(newLocalPath);
468-
469-
try {
470-
// check if object with chosen name already exists
471-
await checkObject(currentDrive.name, {
472-
path: newLocalPath
473-
});
474-
newFileName = await this.incrementName(newLocalPath, this._name);
475-
} catch (error) {
476-
// HEAD request failed for this file name, continue, as name doesn't already exist.
477-
} finally {
478-
data = await renameObjects(currentDrive.name, {
479-
path: relativePath,
480-
newPath: newRelativePath,
481-
newFileName: newFileName,
482-
registeredFileTypes: this._registeredFileTypes
483-
});
447+
if (oldLocalPath !== '') {
448+
// extract current drive name
449+
const currentDrive = this._drivesList.filter(
450+
x =>
451+
x.name ===
452+
(oldLocalPath.indexOf('/') !== -1
453+
? oldLocalPath.substring(0, oldLocalPath.indexOf('/'))
454+
: oldLocalPath)
455+
)[0];
456+
457+
// eliminate drive name from path
458+
const relativePath =
459+
oldLocalPath.indexOf('/') !== -1
460+
? oldLocalPath.substring(oldLocalPath.indexOf('/') + 1)
461+
: '';
462+
const newRelativePath =
463+
newLocalPath.indexOf('/') !== -1
464+
? newLocalPath.substring(newLocalPath.indexOf('/') + 1)
465+
: '';
466+
467+
// extract new file name
468+
let newFileName = PathExt.basename(newLocalPath);
469+
470+
try {
471+
// check if object with chosen name already exists
472+
await checkObject(currentDrive.name, {
473+
path: newLocalPath
474+
});
475+
newFileName = await this.incrementName(newLocalPath, this._name);
476+
} catch (error) {
477+
// HEAD request failed for this file name, continue, as name doesn't already exist.
478+
} finally {
479+
data = await renameObjects(currentDrive.name, {
480+
path: relativePath,
481+
newPath: newRelativePath,
482+
newFileName: newFileName,
483+
registeredFileTypes: this._registeredFileTypes
484+
});
485+
}
486+
} else {
487+
// create new element at root would mean modifying a drive
488+
console.warn('Operation not supported.');
484489
}
485490

486491
Contents.validateContentsModel(data);
@@ -543,26 +548,31 @@ export class Drive implements Contents.IDrive {
543548
localPath: string,
544549
options: Partial<Contents.IModel> = {}
545550
): Promise<Contents.IModel> {
546-
// extract current drive name
547-
const currentDrive = this._drivesList.filter(
548-
x =>
549-
x.name ===
550-
(localPath.indexOf('/') !== -1
551-
? localPath.substring(0, localPath.indexOf('/'))
552-
: localPath)
553-
)[0];
554-
555-
// eliminate drive name from path
556-
const relativePath =
557-
localPath.indexOf('/') !== -1
558-
? localPath.substring(localPath.indexOf('/') + 1)
559-
: '';
560-
561-
const data = await saveObject(currentDrive.name, {
562-
path: relativePath,
563-
param: options,
564-
registeredFileTypes: this._registeredFileTypes
565-
});
551+
if (localPath !== '') {
552+
// extract current drive name
553+
const currentDrive = this._drivesList.filter(
554+
x =>
555+
x.name ===
556+
(localPath.indexOf('/') !== -1
557+
? localPath.substring(0, localPath.indexOf('/'))
558+
: localPath)
559+
)[0];
560+
561+
// eliminate drive name from path
562+
const relativePath =
563+
localPath.indexOf('/') !== -1
564+
? localPath.substring(localPath.indexOf('/') + 1)
565+
: '';
566+
567+
data = await saveObject(currentDrive.name, {
568+
path: relativePath,
569+
param: options,
570+
registeredFileTypes: this._registeredFileTypes
571+
});
572+
} else {
573+
// create new element at root would mean modifying a drive
574+
console.warn('Operation not supported.');
575+
}
566576

567577
Contents.validateContentsModel(data);
568578
this._fileChanged.emit({
@@ -625,32 +635,41 @@ export class Drive implements Contents.IDrive {
625635
toDir: string,
626636
options: Contents.ICreateOptions = {}
627637
): Promise<Contents.IModel> {
628-
// extract current drive name
629-
const currentDrive = this._drivesList.filter(
630-
x =>
631-
x.name ===
632-
(path.indexOf('/') !== -1 ? path.substring(0, path.indexOf('/')) : path)
633-
)[0];
634-
635-
// eliminate drive name from path
636-
const relativePath =
637-
path.indexOf('/') !== -1 ? path.substring(path.indexOf('/') + 1) : '';
638-
const toRelativePath =
639-
toDir.indexOf('/') !== -1 ? toDir.substring(toDir.indexOf('/') + 1) : '';
640-
641-
// construct new file or directory name for the copy
642-
const newFileName = await this.incrementCopyName(
643-
relativePath,
644-
toRelativePath,
645-
currentDrive.name
646-
);
638+
if (path !== '') {
639+
// extract current drive name
640+
const currentDrive = this._drivesList.filter(
641+
x =>
642+
x.name ===
643+
(path.indexOf('/') !== -1
644+
? path.substring(0, path.indexOf('/'))
645+
: path)
646+
)[0];
647647

648-
data = await copyObjects(currentDrive.name, {
649-
path: relativePath,
650-
toPath: toRelativePath,
651-
newFileName: newFileName,
652-
registeredFileTypes: this._registeredFileTypes
653-
});
648+
// eliminate drive name from path
649+
const relativePath =
650+
path.indexOf('/') !== -1 ? path.substring(path.indexOf('/') + 1) : '';
651+
const toRelativePath =
652+
toDir.indexOf('/') !== -1
653+
? toDir.substring(toDir.indexOf('/') + 1)
654+
: '';
655+
656+
// construct new file or directory name for the copy
657+
const newFileName = await this.incrementCopyName(
658+
relativePath,
659+
toRelativePath,
660+
currentDrive.name
661+
);
662+
663+
data = await copyObjects(currentDrive.name, {
664+
path: relativePath,
665+
toPath: toRelativePath,
666+
newFileName: newFileName,
667+
registeredFileTypes: this._registeredFileTypes
668+
});
669+
} else {
670+
// create new element at root would mean modifying a drive
671+
console.warn('Operation not supported.');
672+
}
654673

655674
this._fileChanged.emit({
656675
type: 'new',

0 commit comments

Comments
 (0)