misc_pterodactyl-panel/resources/assets/scripts/api/server/getDirectoryContents.ts

41 lines
1.4 KiB
TypeScript
Raw Normal View History

import http from '../http';
2019-02-10 05:15:45 +00:00
import {filter, isObject} from 'lodash';
// @ts-ignore
import route from '../../../../../vendor/tightenco/ziggy/src/js/route';
import {DirectoryContents} from "./types";
/**
* 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> {
return new Promise((resolve, reject) => {
2019-02-10 05:15:45 +00:00
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)) {
err.response.data.errors.forEach((error: any) => {
return reject(error.detail);
});
}
return reject(err);
});
});
}
export default getDirectoryContents;