mirror of
https://github.com/portainer/portainer.git
synced 2025-07-19 05:19:39 +02:00
feat(volume-browser): add the ability to browse volume content (#2051)
This commit is contained in:
parent
cec878b01d
commit
48179b9e3d
22 changed files with 366 additions and 18 deletions
115
app/agent/components/volume-browser/volumeBrowserController.js
Normal file
115
app/agent/components/volume-browser/volumeBrowserController.js
Normal file
|
@ -0,0 +1,115 @@
|
|||
angular.module('portainer.agent')
|
||||
.controller('VolumeBrowserController', ['HttpRequestHelper', 'VolumeBrowserService', 'FileSaver', 'Blob', 'ModalService', 'Notifications',
|
||||
function (HttpRequestHelper, VolumeBrowserService, FileSaver, Blob, ModalService, Notifications) {
|
||||
var ctrl = this;
|
||||
|
||||
this.state = {
|
||||
path: '/'
|
||||
};
|
||||
|
||||
this.rename = function(file, newName) {
|
||||
var filePath = this.state.path === '/' ? file : this.state.path + '/' + file;
|
||||
var newFilePath = this.state.path === '/' ? newName : this.state.path + '/' + newName;
|
||||
|
||||
VolumeBrowserService.rename(this.volumeId, filePath, newFilePath)
|
||||
.then(function success() {
|
||||
Notifications.success('File successfully renamed', newFilePath);
|
||||
return VolumeBrowserService.ls(ctrl.volumeId, ctrl.state.path);
|
||||
})
|
||||
.then(function success(data) {
|
||||
ctrl.files = data;
|
||||
})
|
||||
.catch(function error(err) {
|
||||
Notifications.error('Failure', err, 'Unable to rename file');
|
||||
});
|
||||
};
|
||||
|
||||
this.delete = function(file) {
|
||||
var filePath = this.state.path === '/' ? file : this.state.path + '/' + file;
|
||||
|
||||
ModalService.confirmDeletion(
|
||||
'Are you sure that you want to delete ' + filePath + ' ?',
|
||||
function onConfirm(confirmed) {
|
||||
if(!confirmed) { return; }
|
||||
deleteFile(filePath);
|
||||
}
|
||||
);
|
||||
};
|
||||
|
||||
this.download = function(file) {
|
||||
var filePath = this.state.path === '/' ? file : this.state.path + '/' + file;
|
||||
VolumeBrowserService.get(this.volumeId, filePath)
|
||||
.then(function success(data) {
|
||||
var downloadData = new Blob([data.file], { type: 'text/plain;charset=utf-8' });
|
||||
FileSaver.saveAs(downloadData, file);
|
||||
})
|
||||
.catch(function error(err) {
|
||||
Notifications.error('Failure', err, 'Unable to download file');
|
||||
});
|
||||
};
|
||||
|
||||
this.up = function() {
|
||||
var parentFolder = parentPath(this.state.path);
|
||||
browse(parentFolder);
|
||||
};
|
||||
|
||||
this.browse = function(folder) {
|
||||
var path = buildPath(this.state.path, folder);
|
||||
browse(path);
|
||||
};
|
||||
|
||||
function deleteFile(file) {
|
||||
VolumeBrowserService.delete(ctrl.volumeId, file)
|
||||
.then(function success() {
|
||||
Notifications.success('File successfully deleted', file);
|
||||
return VolumeBrowserService.ls(ctrl.volumeId, ctrl.state.path);
|
||||
})
|
||||
.then(function success(data) {
|
||||
ctrl.files = data;
|
||||
})
|
||||
.catch(function error(err) {
|
||||
Notifications.error('Failure', err, 'Unable to delete file');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function browse(path) {
|
||||
VolumeBrowserService.ls(ctrl.volumeId, path)
|
||||
.then(function success(data) {
|
||||
ctrl.state.path = path;
|
||||
ctrl.files = data;
|
||||
})
|
||||
.catch(function error(err) {
|
||||
Notifications.error('Failure', err, 'Unable to browse volume');
|
||||
});
|
||||
}
|
||||
|
||||
function parentPath(path) {
|
||||
if (path.lastIndexOf('/') === 0) {
|
||||
return '/';
|
||||
}
|
||||
|
||||
var split = _.split(path, '/');
|
||||
return _.join(_.slice(split, 0, split.length - 1), '/');
|
||||
}
|
||||
|
||||
function buildPath(parent, file) {
|
||||
if (parent === '/') {
|
||||
return parent + file;
|
||||
}
|
||||
return parent + '/' + file;
|
||||
}
|
||||
|
||||
|
||||
this.$onInit = function() {
|
||||
HttpRequestHelper.setPortainerAgentTargetHeader(this.nodeName);
|
||||
VolumeBrowserService.ls(this.volumeId, this.state.path)
|
||||
.then(function success(data) {
|
||||
ctrl.files = data;
|
||||
})
|
||||
.catch(function error(err) {
|
||||
Notifications.error('Failure', err, 'Unable to browse volume');
|
||||
});
|
||||
};
|
||||
|
||||
}]);
|
Loading…
Add table
Add a link
Reference in a new issue