2019-03-21 05:46:49 +00:00
|
|
|
import _ from 'lodash-es';
|
|
|
|
|
2020-07-23 07:45:12 +00:00
|
|
|
export class VolumeBrowserController {
|
|
|
|
constructor($async, HttpRequestHelper, VolumeBrowserService, FileSaver, Blob, ModalService, Notifications) {
|
|
|
|
Object.assign(this, { $async, HttpRequestHelper, VolumeBrowserService, FileSaver, Blob, ModalService, Notifications });
|
2020-04-10 21:54:53 +00:00
|
|
|
this.state = {
|
|
|
|
path: '/',
|
|
|
|
};
|
|
|
|
|
2020-07-23 07:45:12 +00:00
|
|
|
this.rename = this.rename.bind(this);
|
|
|
|
this.renameAsync = this.renameAsync.bind(this);
|
|
|
|
this.confirmDelete = this.confirmDelete.bind(this);
|
|
|
|
this.download = this.download.bind(this);
|
|
|
|
this.downloadAsync = this.downloadAsync.bind(this);
|
|
|
|
this.up = this.up.bind(this);
|
|
|
|
this.browse = this.browse.bind(this);
|
|
|
|
this.deleteFile = this.deleteFile.bind(this);
|
|
|
|
this.deleteFileAsync = this.deleteFileAsync.bind(this);
|
|
|
|
this.getFilesForPath = this.getFilesForPath.bind(this);
|
|
|
|
this.getFilesForPathAsync = this.getFilesForPathAsync.bind(this);
|
|
|
|
this.onFileSelectedForUpload = this.onFileSelectedForUpload.bind(this);
|
|
|
|
this.onFileSelectedForUploadAsync = this.onFileSelectedForUploadAsync.bind(this);
|
|
|
|
this.parentPath = this.parentPath.bind(this);
|
|
|
|
this.buildPath = this.buildPath.bind(this);
|
|
|
|
this.$onInit = this.$onInit.bind(this);
|
|
|
|
this.onFileUploaded = this.onFileUploaded.bind(this);
|
|
|
|
this.refreshList = this.refreshList.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
rename(file, newName) {
|
|
|
|
return this.$async(this.renameAsync, file, newName);
|
|
|
|
}
|
|
|
|
async renameAsync(file, newName) {
|
|
|
|
const filePath = this.state.path === '/' ? file : `${this.state.path}/${file}`;
|
|
|
|
const newFilePath = this.state.path === '/' ? newName : `${this.state.path}/${newName}`;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.VolumeBrowserService.rename(this.volumeId, filePath, newFilePath);
|
|
|
|
this.Notifications.success('File successfully renamed', newFilePath);
|
|
|
|
this.files = await this.VolumeBrowserService.ls(this.volumeId, this.state.path);
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to rename file');
|
2018-07-23 05:01:03 +00:00
|
|
|
}
|
2020-07-23 07:45:12 +00:00
|
|
|
}
|
2018-07-23 05:01:03 +00:00
|
|
|
|
2020-07-23 07:45:12 +00:00
|
|
|
confirmDelete(file) {
|
|
|
|
const filePath = this.state.path === '/' ? file : `${this.state.path}/${file}`;
|
2020-04-10 21:54:53 +00:00
|
|
|
|
2020-07-23 07:45:12 +00:00
|
|
|
this.ModalService.confirmDeletion(`Are you sure that you want to delete ${filePath} ?`, (confirmed) => {
|
|
|
|
if (!confirmed) {
|
|
|
|
return;
|
2020-04-10 21:54:53 +00:00
|
|
|
}
|
2020-07-23 07:45:12 +00:00
|
|
|
this.deleteFile(filePath);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
download(file) {
|
|
|
|
return this.$async(this.downloadAsync, file);
|
|
|
|
}
|
|
|
|
async downloadAsync(file) {
|
|
|
|
const filePath = this.state.path === '/' ? file : `${this.state.path}/${file}`;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const data = await this.VolumeBrowserService.get(this.volumeId, filePath);
|
|
|
|
const downloadData = new Blob([data.file]);
|
|
|
|
this.FileSaver.saveAs(downloadData, file);
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to download file');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
up() {
|
|
|
|
const parentFolder = this.parentPath(this.state.path);
|
|
|
|
this.getFilesForPath(parentFolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
browse(folder) {
|
|
|
|
const path = this.buildPath(this.state.path, folder);
|
|
|
|
this.getFilesForPath(path);
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteFile(file) {
|
|
|
|
return this.$async(this.deleteFileAsync, file);
|
|
|
|
}
|
|
|
|
async deleteFileAsync(file) {
|
|
|
|
try {
|
|
|
|
await this.VolumeBrowserService.delete(this.volumeId, file);
|
|
|
|
this.Notifications.success('File successfully deleted', file);
|
|
|
|
this.files = await this.VolumeBrowserService.ls(this.volumeId, this.state.path);
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to delete file');
|
2020-04-10 21:54:53 +00:00
|
|
|
}
|
2020-07-23 07:45:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getFilesForPath(path) {
|
|
|
|
return this.$async(this.getFilesForPathAsync, path);
|
|
|
|
}
|
|
|
|
async getFilesForPathAsync(path) {
|
|
|
|
try {
|
|
|
|
const files = await this.VolumeBrowserService.ls(this.volumeId, path);
|
|
|
|
this.state.path = path;
|
|
|
|
this.files = files;
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to browse volume');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
onFileSelectedForUpload(file) {
|
|
|
|
return this.$async(this.onFileSelectedForUploadAsync, file);
|
|
|
|
}
|
|
|
|
async onFileSelectedForUploadAsync(file) {
|
|
|
|
try {
|
|
|
|
await this.VolumeBrowserService.upload(this.state.path, file, this.volumeId);
|
|
|
|
this.onFileUploaded();
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to upload file');
|
|
|
|
}
|
|
|
|
}
|
2018-10-26 03:16:29 +00:00
|
|
|
|
2020-07-23 07:45:12 +00:00
|
|
|
parentPath(path) {
|
|
|
|
if (path.lastIndexOf('/') === 0) {
|
|
|
|
return '/';
|
2020-04-10 21:54:53 +00:00
|
|
|
}
|
2018-10-26 03:16:29 +00:00
|
|
|
|
2020-07-23 07:45:12 +00:00
|
|
|
const split = _.split(path, '/');
|
|
|
|
return _.join(_.slice(split, 0, split.length - 1), '/');
|
|
|
|
}
|
2020-04-10 21:54:53 +00:00
|
|
|
|
2020-07-23 07:45:12 +00:00
|
|
|
buildPath(parent, file) {
|
|
|
|
if (parent === '/') {
|
|
|
|
return parent + file;
|
2020-04-10 21:54:53 +00:00
|
|
|
}
|
2020-07-23 07:45:12 +00:00
|
|
|
return `${parent}/${file}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
onFileUploaded() {
|
|
|
|
this.refreshList();
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshList() {
|
|
|
|
this.getFilesForPath(this.state.path);
|
|
|
|
}
|
|
|
|
|
|
|
|
async $onInit() {
|
|
|
|
this.HttpRequestHelper.setPortainerAgentTargetHeader(this.nodeName);
|
|
|
|
try {
|
|
|
|
this.files = await this.VolumeBrowserService.ls(this.volumeId, this.state.path);
|
|
|
|
} catch (err) {
|
|
|
|
this.Notifications.error('Failure', err, 'Unable to browse volume');
|
2020-04-10 21:54:53 +00:00
|
|
|
}
|
2020-07-23 07:45:12 +00:00
|
|
|
}
|
|
|
|
}
|