2018-09-30 08:16:10 +00:00
|
|
|
angular.module('portainer.agent').controller('HostBrowserController', [
|
|
|
|
'HostBrowserService',
|
|
|
|
'Notifications',
|
2018-09-30 08:44:35 +00:00
|
|
|
'FileSaver',
|
|
|
|
function HostBrowserController(HostBrowserService, Notifications, FileSaver) {
|
2018-09-30 08:16:10 +00:00
|
|
|
var ctrl = this;
|
|
|
|
ctrl.state = {
|
|
|
|
path: '/'
|
|
|
|
};
|
2018-08-23 08:49:03 +00:00
|
|
|
|
2018-09-30 08:16:10 +00:00
|
|
|
ctrl.goToParent = goToParent;
|
|
|
|
ctrl.browse = browse;
|
|
|
|
ctrl.renameFile = renameFile;
|
|
|
|
ctrl.downloadFile = downloadFile;
|
|
|
|
ctrl.deleteFile = deleteFile;
|
|
|
|
ctrl.isRoot = isRoot;
|
|
|
|
ctrl.$onInit = $onInit;
|
2018-08-23 08:49:03 +00:00
|
|
|
|
|
|
|
function goToParent() {
|
2018-09-30 08:16:10 +00:00
|
|
|
getFilesForPath(parentPath(this.state.path));
|
|
|
|
}
|
2018-08-23 08:49:03 +00:00
|
|
|
|
2018-09-30 08:16:10 +00:00
|
|
|
function isRoot() {
|
|
|
|
return ctrl.state.path === '/';
|
2018-08-23 08:49:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 08:16:10 +00:00
|
|
|
function browse(folder) {
|
|
|
|
getFilesForPath(buildPath(ctrl.state.path, folder));
|
|
|
|
}
|
2018-08-23 08:49:03 +00:00
|
|
|
|
2018-09-30 08:16:10 +00:00
|
|
|
function getFilesForPath(path) {
|
|
|
|
HostBrowserService.ls(path)
|
|
|
|
.then(function onFilesLoaded(files) {
|
|
|
|
ctrl.state.path = path;
|
|
|
|
ctrl.files = files;
|
|
|
|
})
|
|
|
|
.catch(function onLoadingFailed(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to browse');
|
|
|
|
});
|
|
|
|
}
|
2018-08-23 08:49:03 +00:00
|
|
|
|
2018-09-30 08:24:01 +00:00
|
|
|
function renameFile(name, newName) {
|
|
|
|
var isRoot = ctrl.isRoot();
|
|
|
|
var filePath = isRoot ? '/' + name : this.state.path + '/' + file;
|
2018-09-30 08:44:35 +00:00
|
|
|
var newFilePath = isRoot
|
|
|
|
? '/' + newName
|
|
|
|
: this.state.path + '/' + newName;
|
2018-09-30 08:24:01 +00:00
|
|
|
|
|
|
|
HostBrowserService.rename(filePath, newFilePath)
|
|
|
|
.then(function onRenameSuccess() {
|
|
|
|
Notifications.success('File successfully renamed', newFilePath);
|
|
|
|
return HostBrowserService.ls(ctrl.state.path);
|
|
|
|
})
|
|
|
|
.then(function onFilesLoaded(files) {
|
|
|
|
ctrl.files = files;
|
|
|
|
})
|
|
|
|
.catch(function notifyOnError(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to rename file');
|
|
|
|
});
|
|
|
|
}
|
2018-08-23 08:49:03 +00:00
|
|
|
|
2018-09-30 08:44:35 +00:00
|
|
|
function downloadFile(file) {
|
2018-09-30 08:45:51 +00:00
|
|
|
var filePath = buildPath(ctrl.state.path, file);
|
2018-09-30 08:44:35 +00:00
|
|
|
HostBrowserService.get(filePath)
|
|
|
|
.then(function onFileReceived(data) {
|
|
|
|
var downloadData = new Blob([data.file], {
|
|
|
|
type: 'text/plain;charset=utf-8'
|
|
|
|
});
|
|
|
|
FileSaver.saveAs(downloadData, file);
|
|
|
|
})
|
|
|
|
.catch(function notifyOnError(err) {
|
|
|
|
Notifications.error('Failure', err, 'Unable to download file');
|
|
|
|
});
|
|
|
|
}
|
2018-08-23 08:49:03 +00:00
|
|
|
|
2018-09-30 08:16:10 +00:00
|
|
|
function deleteFile(name) {}
|
2018-08-23 08:49:03 +00:00
|
|
|
|
2018-09-30 08:16:10 +00:00
|
|
|
function $onInit() {
|
|
|
|
getFilesForPath('/');
|
2018-08-23 08:49:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 08:16:10 +00:00
|
|
|
function parentPath(path) {
|
|
|
|
if (path.lastIndexOf('/') === 0) {
|
|
|
|
return '/';
|
|
|
|
}
|
2018-09-30 08:24:01 +00:00
|
|
|
|
2018-09-30 08:16:10 +00:00
|
|
|
var split = _.split(path, '/');
|
|
|
|
return _.join(_.slice(split, 0, split.length - 1), '/');
|
2018-08-23 08:49:03 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 08:16:10 +00:00
|
|
|
function buildPath(parent, file) {
|
|
|
|
if (parent === '/') {
|
|
|
|
return parent + file;
|
|
|
|
}
|
|
|
|
return parent + '/' + file;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]);
|