2018-12-29 23:51:13 +00:00
|
|
|
import http from '../http';
|
|
|
|
import { filter, isObject } from 'lodash';
|
|
|
|
// @ts-ignore
|
2018-09-23 23:06:23 +00:00
|
|
|
import route from '../../../../../vendor/tightenco/ziggy/src/js/route';
|
2018-12-29 23:51:13 +00:00
|
|
|
import {DirectoryContents} from "./types";
|
2018-09-23 23:06:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the contents of a specific directory for a given server.
|
|
|
|
*/
|
2018-12-29 23:51:13 +00:00
|
|
|
export function getDirectoryContents (server: string, directory: string): Promise<DirectoryContents> {
|
2018-09-23 23:06:23 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
http.get(route('server.files', { server, directory }))
|
|
|
|
.then((response) => {
|
|
|
|
return resolve({
|
|
|
|
files: filter(response.data.contents, function (o) {
|
|
|
|
return o.file;
|
|
|
|
}),
|
|
|
|
directories: filter(response.data.contents, function (o) {
|
|
|
|
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;
|