Skip to content

Commit d497e04

Browse files
committed
refactor get contents request
1 parent 9994ee3 commit d497e04

File tree

2 files changed

+52
-39
lines changed

2 files changed

+52
-39
lines changed

src/contents.ts

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,35 @@ export class Drive implements Contents.IDrive {
243243
}
244244
}
245245

246-
data = await getContents(currentDrive.name, {
247-
path: formatPath(localPath),
246+
const currentPath = formatPath(localPath);
247+
const result = await getContents(currentDrive.name, {
248+
path: currentPath,
248249
registeredFileTypes: this._registeredFileTypes
249250
});
251+
252+
data = {
253+
name: result.isDir
254+
? currentPath
255+
? PathExt.basename(currentPath)
256+
: currentDrive.name
257+
: PathExt.basename(currentPath),
258+
path: PathExt.join(
259+
currentDrive.name,
260+
result.isDir
261+
? currentPath
262+
? currentPath + '/'
263+
: ''
264+
: result.response.data.path
265+
),
266+
last_modified: result.isDir ? '' : result.response.data.last_modified,
267+
created: '',
268+
content: result.isDir ? result.files : result.response.data.content,
269+
format: result.isDir ? 'json' : result.format!,
270+
mimetype: result.isDir ? '' : result.mimetype!,
271+
size: result.isDir ? undefined : result.response.data.size,
272+
writable: true,
273+
type: result.isDir ? 'directory' : result.type!
274+
};
250275
} else {
251276
// retriving list of contents from root
252277
// in our case: list available drives
@@ -314,11 +339,27 @@ export class Drive implements Contents.IDrive {
314339
path.indexOf('/') !== -1 ? path.substring(path.indexOf('/') + 1) : '';
315340

316341
// get current list of contents of drive
317-
const old_data = await getContents(currentDrive.name, {
342+
const result = await getContents(currentDrive.name, {
318343
path: relativePath,
319344
registeredFileTypes: this._registeredFileTypes
320345
});
321346

347+
const old_data: Contents.IModel = {
348+
name: relativePath ? PathExt.basename(relativePath) : currentDrive.name,
349+
path: PathExt.join(
350+
currentDrive.name,
351+
relativePath ? relativePath + '/' : ''
352+
),
353+
last_modified: '',
354+
created: '',
355+
content: result.files,
356+
format: 'json'!,
357+
mimetype: '',
358+
size: undefined,
359+
writable: true,
360+
type: 'directory'
361+
};
362+
322363
if (options.type !== undefined) {
323364
// get incremented untitled name
324365
const name = this.incrementUntitledName(old_data, options);

src/requests.ts

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,6 @@ import {
1010
IDriveInfo
1111
} from './token';
1212

13-
/**
14-
* The data contents model.
15-
*/
16-
let data: Contents.IModel = {
17-
name: '',
18-
path: '',
19-
last_modified: '',
20-
created: '',
21-
content: null,
22-
format: null,
23-
mimetype: '',
24-
size: 0,
25-
writable: true,
26-
type: ''
27-
};
28-
2913
/**
3014
* Set new limit for number of objects to be listed inside the DriveBrowser, given any path.
3115
*
@@ -147,17 +131,10 @@ export async function getContents(
147131
}
148132
});
149133

150-
data = {
151-
name: options.path ? PathExt.basename(options.path) : driveName,
152-
path: PathExt.join(driveName, options.path ? options.path + '/' : ''),
153-
last_modified: '',
154-
created: '',
155-
content: Object.values(fileList),
156-
format: 'json',
157-
mimetype: '',
158-
size: undefined,
159-
writable: true,
160-
type: 'directory'
134+
return {
135+
isDir: isDir,
136+
response: response,
137+
files: Object.values(fileList)
161138
};
162139
}
163140
// getting the contents of a file
@@ -167,22 +144,17 @@ export async function getContents(
167144
options.registeredFileTypes
168145
);
169146

170-
data = {
171-
name: PathExt.basename(options.path),
172-
path: PathExt.join(driveName, response.data.path),
173-
last_modified: response.data.last_modified,
174-
created: '',
175-
content: response.data.content,
147+
return {
148+
isDir: isDir,
149+
response: response,
176150
format: fileFormat as Contents.FileFormat,
177151
mimetype: fileMimeType,
178-
size: response.data.size,
179-
writable: true,
180152
type: fileType
181153
};
182154
}
183155
}
184156

185-
return data;
157+
return {};
186158
}
187159

188160
/**

0 commit comments

Comments
 (0)