Skip to content

Commit 916d80a

Browse files
authored
Merge pull request #92 from DenisaCG/refactor
Refactor requests
2 parents 322d6b1 + d020aa0 commit 916d80a

File tree

2 files changed

+254
-168
lines changed

2 files changed

+254
-168
lines changed

src/contents.ts

Lines changed: 217 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,6 @@ import {
2727
includeDrive
2828
} from './requests';
2929

30-
let data: Contents.IModel = {
31-
name: '',
32-
path: '',
33-
last_modified: '',
34-
created: '',
35-
content: [],
36-
format: null,
37-
mimetype: '',
38-
size: 0,
39-
writable: true,
40-
type: ''
41-
};
42-
4330
export class Drive implements Contents.IDrive {
4431
/**
4532
* Construct a new drive object.
@@ -227,6 +214,8 @@ export class Drive implements Contents.IDrive {
227214
localPath: string,
228215
options?: Contents.IFetchOptions
229216
): Promise<Contents.IModel> {
217+
let data: Contents.IModel;
218+
230219
if (localPath !== '') {
231220
const currentDrive = extractCurrentDrive(localPath, this._drivesList);
232221

@@ -243,10 +232,35 @@ export class Drive implements Contents.IDrive {
243232
}
244233
}
245234

246-
data = await getContents(currentDrive.name, {
247-
path: formatPath(localPath),
235+
const currentPath = formatPath(localPath);
236+
const result = await getContents(currentDrive.name, {
237+
path: currentPath,
248238
registeredFileTypes: this._registeredFileTypes
249239
});
240+
241+
data = {
242+
name: result.isDir
243+
? currentPath
244+
? PathExt.basename(currentPath)
245+
: currentDrive.name
246+
: PathExt.basename(currentPath),
247+
path: PathExt.join(
248+
currentDrive.name,
249+
result.isDir
250+
? currentPath
251+
? currentPath + '/'
252+
: ''
253+
: result.response.data.path
254+
),
255+
last_modified: result.isDir ? '' : result.response.data.last_modified,
256+
created: '',
257+
content: result.isDir ? result.files : result.response.data.content,
258+
format: result.isDir ? 'json' : result.format!,
259+
mimetype: result.isDir ? '' : result.mimetype!,
260+
size: result.isDir ? undefined : result.response.data.size,
261+
writable: true,
262+
type: result.isDir ? 'directory' : result.type!
263+
};
250264
} else {
251265
// retriving list of contents from root
252266
// in our case: list available drives
@@ -304,6 +318,18 @@ export class Drive implements Contents.IDrive {
304318
async newUntitled(
305319
options: Contents.ICreateOptions = {}
306320
): Promise<Contents.IModel> {
321+
let data: Contents.IModel = {
322+
name: '',
323+
path: '',
324+
last_modified: '',
325+
created: '',
326+
content: [],
327+
format: null,
328+
mimetype: '',
329+
size: 0,
330+
writable: true,
331+
type: ''
332+
};
307333
const path = options.path ?? '';
308334

309335
if (path !== '') {
@@ -314,20 +340,53 @@ export class Drive implements Contents.IDrive {
314340
path.indexOf('/') !== -1 ? path.substring(path.indexOf('/') + 1) : '';
315341

316342
// get current list of contents of drive
317-
const old_data = await getContents(currentDrive.name, {
343+
const result = await getContents(currentDrive.name, {
318344
path: relativePath,
319345
registeredFileTypes: this._registeredFileTypes
320346
});
321347

348+
const old_data: Contents.IModel = {
349+
name: relativePath ? PathExt.basename(relativePath) : currentDrive.name,
350+
path: PathExt.join(
351+
currentDrive.name,
352+
relativePath ? relativePath + '/' : ''
353+
),
354+
last_modified: '',
355+
created: '',
356+
content: result.files,
357+
format: 'json'!,
358+
mimetype: '',
359+
size: undefined,
360+
writable: true,
361+
type: 'directory'
362+
};
363+
322364
if (options.type !== undefined) {
323365
// get incremented untitled name
324366
const name = this.incrementUntitledName(old_data, options);
325-
data = await createObject(currentDrive.name, {
367+
const currentPath = relativePath
368+
? PathExt.join(relativePath, name)
369+
: name;
370+
371+
const result = await createObject(currentDrive.name, {
326372
name: name,
327-
path: relativePath,
373+
path: currentPath,
328374
type: options.type,
329375
registeredFileTypes: this._registeredFileTypes
330376
});
377+
378+
data = {
379+
name: name,
380+
path: PathExt.join(currentDrive.name, currentPath),
381+
last_modified: result.response.data.last_modified,
382+
created: result.response.data.last_modified,
383+
content: result.response.data.content,
384+
format: result.format,
385+
mimetype: result.mimetype,
386+
size: result.response.data.size,
387+
writable: true,
388+
type: result.type
389+
};
331390
} else {
332391
console.warn('Type of new element is undefined');
333392
}
@@ -436,6 +495,18 @@ export class Drive implements Contents.IDrive {
436495
newLocalPath: string,
437496
options: Contents.ICreateOptions = {}
438497
): Promise<Contents.IModel> {
498+
let data: Contents.IModel = {
499+
name: '',
500+
path: '',
501+
last_modified: '',
502+
created: '',
503+
content: [],
504+
format: null,
505+
mimetype: '',
506+
size: 0,
507+
writable: true,
508+
type: ''
509+
};
439510
if (oldLocalPath !== '') {
440511
const currentDrive = extractCurrentDrive(oldLocalPath, this._drivesList);
441512

@@ -458,12 +529,29 @@ export class Drive implements Contents.IDrive {
458529
} catch (error) {
459530
// HEAD request failed for this file name, continue, as name doesn't already exist.
460531
} finally {
461-
data = await renameObjects(currentDrive.name, {
532+
const result = await renameObjects(currentDrive.name, {
462533
path: relativePath,
463534
newPath: newRelativePath,
464535
newFileName: newFileName,
465536
registeredFileTypes: this._registeredFileTypes
466537
});
538+
539+
data = {
540+
name: newFileName,
541+
path: PathExt.join(currentDrive.name, result.formattedNewPath!),
542+
last_modified:
543+
result.response.length > 0
544+
? result.response.data.last_modified
545+
: '',
546+
created: '',
547+
content: PathExt.extname(newFileName) !== '' ? null : [], // TODO: add dir check
548+
format: result.format!,
549+
mimetype: result.mimetype!,
550+
size:
551+
result.response.length > 0 ? result.response.data.size : undefined,
552+
writable: true,
553+
type: result.type!
554+
};
467555
}
468556
} else {
469557
// create new element at root would mean modifying a drive
@@ -530,14 +618,40 @@ export class Drive implements Contents.IDrive {
530618
localPath: string,
531619
options: Partial<Contents.IModel> = {}
532620
): Promise<Contents.IModel> {
621+
let data: Contents.IModel = {
622+
name: '',
623+
path: '',
624+
last_modified: '',
625+
created: '',
626+
content: [],
627+
format: null,
628+
mimetype: '',
629+
size: 0,
630+
writable: true,
631+
type: ''
632+
};
533633
if (localPath !== '') {
534634
const currentDrive = extractCurrentDrive(localPath, this._drivesList);
635+
const currentPath = formatPath(localPath);
535636

536-
data = await saveObject(currentDrive.name, {
537-
path: formatPath(localPath),
637+
const result = await saveObject(currentDrive.name, {
638+
path: currentPath,
538639
param: options,
539640
registeredFileTypes: this._registeredFileTypes
540641
});
642+
643+
data = {
644+
name: currentPath,
645+
path: PathExt.join(currentDrive.name, currentPath),
646+
last_modified: result.response.data.last_modified as string,
647+
created: result.response.data.last_modified as string,
648+
content: result.response.data.content,
649+
format: result.format,
650+
mimetype: result.mimetype,
651+
size: result.response.data.size,
652+
writable: true,
653+
type: result.type
654+
};
541655
} else {
542656
// create new element at root would mean modifying a drive
543657
console.warn('Operation not supported.');
@@ -604,6 +718,18 @@ export class Drive implements Contents.IDrive {
604718
toDir: string,
605719
options: Contents.ICreateOptions = {}
606720
): Promise<Contents.IModel> {
721+
let data: Contents.IModel = {
722+
name: '',
723+
path: '',
724+
last_modified: '',
725+
created: '',
726+
content: [],
727+
format: null,
728+
mimetype: '',
729+
size: 0,
730+
writable: true,
731+
type: ''
732+
};
607733
if (path !== '') {
608734
const currentDrive = extractCurrentDrive(path, this._drivesList);
609735
const toDrive = extractCurrentDrive(toDir, this._drivesList);
@@ -619,13 +745,26 @@ export class Drive implements Contents.IDrive {
619745
toDrive.name
620746
);
621747

622-
data = await copyObjects(currentDrive.name, {
748+
const result = await copyObjects(currentDrive.name, {
623749
path: relativePath,
624750
toPath: toRelativePath,
625751
newFileName: newFileName,
626752
toDrive: toDrive.name,
627753
registeredFileTypes: this._registeredFileTypes
628754
});
755+
756+
data = {
757+
name: newFileName,
758+
path: PathExt.join(currentDrive.name, result.formattedNewPath!),
759+
last_modified: result.response!.data.last_modified,
760+
created: '',
761+
content: PathExt.extname(newFileName) !== '' ? null : [], // TODO: add dir check
762+
format: result.format! as Contents.FileFormat,
763+
mimetype: result.mimetype!,
764+
size: result.response!.data.size,
765+
writable: true,
766+
type: result.type!
767+
};
629768
} else {
630769
// create new element at root would mean modifying a drive
631770
console.warn('Operation not supported.');
@@ -651,10 +790,23 @@ export class Drive implements Contents.IDrive {
651790
newDriveName: string,
652791
region: string
653792
): Promise<Contents.IModel> {
654-
data = await createDrive(newDriveName, {
793+
await createDrive(newDriveName, {
655794
location: region
656795
});
657796

797+
const data: Contents.IModel = {
798+
name: newDriveName,
799+
path: newDriveName,
800+
last_modified: '',
801+
created: '',
802+
content: [],
803+
format: 'json',
804+
mimetype: '',
805+
size: 0,
806+
writable: true,
807+
type: 'directory'
808+
};
809+
658810
Contents.validateContentsModel(data);
659811
this._fileChanged.emit({
660812
type: 'new',
@@ -673,7 +825,20 @@ export class Drive implements Contents.IDrive {
673825
* @returns A promise which resolves with the contents model.
674826
*/
675827
async addPublicDrive(driveUrl: string): Promise<Contents.IModel> {
676-
data = await addPublicDrive(driveUrl);
828+
await addPublicDrive(driveUrl);
829+
830+
const data: Contents.IModel = {
831+
name: driveUrl,
832+
path: driveUrl,
833+
last_modified: '',
834+
created: '',
835+
content: [],
836+
format: 'json',
837+
mimetype: '',
838+
size: 0,
839+
writable: true,
840+
type: 'directory'
841+
};
677842

678843
Contents.validateContentsModel(data);
679844
this._fileChanged.emit({
@@ -693,7 +858,20 @@ export class Drive implements Contents.IDrive {
693858
* @returns A promise which resolves with the contents model.
694859
*/
695860
async excludeDrive(driveName: string): Promise<Contents.IModel> {
696-
data = await excludeDrive(driveName);
861+
await excludeDrive(driveName);
862+
863+
const data: Contents.IModel = {
864+
name: driveName,
865+
path: driveName,
866+
last_modified: '',
867+
created: '',
868+
content: [],
869+
format: 'json',
870+
mimetype: '',
871+
size: 0,
872+
writable: true,
873+
type: 'directory'
874+
};
697875

698876
Contents.validateContentsModel(data);
699877
this._fileChanged.emit({
@@ -713,7 +891,20 @@ export class Drive implements Contents.IDrive {
713891
* @returns A promise which resolves with the contents model.
714892
*/
715893
async includeDrive(driveName: string): Promise<Contents.IModel> {
716-
data = await includeDrive(driveName);
894+
await includeDrive(driveName);
895+
896+
const data: Contents.IModel = {
897+
name: driveName,
898+
path: driveName,
899+
last_modified: '',
900+
created: '',
901+
content: [],
902+
format: 'json',
903+
mimetype: '',
904+
size: 0,
905+
writable: true,
906+
type: 'directory'
907+
};
717908

718909
Contents.validateContentsModel(data);
719910
this._fileChanged.emit({

0 commit comments

Comments
 (0)