2018-12-29 23:51:13 +00:00
|
|
|
import http from '../http';
|
2019-02-10 05:15:45 +00:00
|
|
|
import {filter, isObject} from 'lodash';
|
2019-02-19 04:41:58 +00:00
|
|
|
import {DirectoryContentObject, DirectoryContents} from "./types";
|
2018-09-23 23:06:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the contents of a specific directory for a given server.
|
|
|
|
*/
|
2019-02-10 05:15:45 +00:00
|
|
|
export function getDirectoryContents(server: string, directory: string): Promise<DirectoryContents> {
|
2018-09-23 23:06:23 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
2019-05-02 03:54:40 +00:00
|
|
|
http.get(`/api/client/servers/${server}/files/list`, {
|
|
|
|
params: {directory}
|
|
|
|
})
|
2018-09-23 23:06:23 +00:00
|
|
|
.then((response) => {
|
|
|
|
return resolve({
|
2019-02-19 04:41:58 +00:00
|
|
|
files: filter(response.data.contents, function (o: DirectoryContentObject) {
|
2018-09-23 23:06:23 +00:00
|
|
|
return o.file;
|
|
|
|
}),
|
2019-02-19 04:41:58 +00:00
|
|
|
directories: filter(response.data.contents, function (o: DirectoryContentObject) {
|
2018-09-23 23:06:23 +00:00
|
|
|
return o.directory;
|
|
|
|
}),
|
|
|
|
editable: response.data.editable,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
if (err.response && err.response.status === 404) {
|
|
|
|
return reject('The directory you requested could not be located on the server');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (err.response.data && isObject(err.response.data.errors)) {
|
2018-12-29 23:51:13 +00:00
|
|
|
err.response.data.errors.forEach((error: any) => {
|
2018-09-23 23:06:23 +00:00
|
|
|
return reject(error.detail);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return reject(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export default getDirectoryContents;
|