mirror of https://github.com/portainer/portainer
feat(agent): refactor volumeBrowserService to es6 (#4094)
parent
822c4e117c
commit
99db41f96e
|
@ -1,61 +1,59 @@
|
|||
angular.module('portainer.agent').factory('VolumeBrowserService', [
|
||||
'StateManager',
|
||||
'Browse',
|
||||
'BrowseVersion1',
|
||||
'$q',
|
||||
'API_ENDPOINT_ENDPOINTS',
|
||||
'EndpointProvider',
|
||||
'Upload',
|
||||
function VolumeBrowserServiceFactory(StateManager, Browse, BrowseVersion1, $q, API_ENDPOINT_ENDPOINTS, EndpointProvider, Upload) {
|
||||
'use strict';
|
||||
var service = {};
|
||||
import angular from 'angular';
|
||||
|
||||
function getAgentApiVersion() {
|
||||
var state = StateManager.getState();
|
||||
return state.endpoint.agentApiVersion;
|
||||
angular.module('portainer.agent').factory('VolumeBrowserService', VolumeBrowserServiceFactory);
|
||||
|
||||
function VolumeBrowserServiceFactory(StateManager, Browse, BrowseVersion1, API_ENDPOINT_ENDPOINTS, EndpointProvider, Upload) {
|
||||
return {
|
||||
ls,
|
||||
get,
|
||||
delete: deletePath,
|
||||
rename,
|
||||
upload,
|
||||
};
|
||||
|
||||
function getAgentApiVersion() {
|
||||
const state = StateManager.getState();
|
||||
return state.endpoint.agentApiVersion;
|
||||
}
|
||||
|
||||
function getBrowseService() {
|
||||
const agentVersion = getAgentApiVersion();
|
||||
return agentVersion > 1 ? Browse : BrowseVersion1;
|
||||
}
|
||||
|
||||
function ls(volumeId, path) {
|
||||
return getBrowseService().ls({ volumeID: volumeId, path, version: getAgentApiVersion() }).$promise;
|
||||
}
|
||||
|
||||
function get(volumeId, path) {
|
||||
return getBrowseService().get({ volumeID: volumeId, path, version: getAgentApiVersion() }).$promise;
|
||||
}
|
||||
|
||||
function deletePath(volumeId, path) {
|
||||
return getBrowseService().delete({ volumeID: volumeId, path, version: getAgentApiVersion() }).$promise;
|
||||
}
|
||||
|
||||
function rename(volumeId, path, newPath) {
|
||||
const payload = {
|
||||
CurrentFilePath: path,
|
||||
NewFilePath: newPath,
|
||||
};
|
||||
return getBrowseService().rename({ volumeID: volumeId, version: getAgentApiVersion() }, payload).$promise;
|
||||
}
|
||||
|
||||
function upload(path, file, volumeId, onProgress) {
|
||||
const agentVersion = StateManager.getAgentApiVersion();
|
||||
if (agentVersion < 2) {
|
||||
throw new Error('upload is not supported on this agent version');
|
||||
}
|
||||
|
||||
function getBrowseService() {
|
||||
var agentVersion = getAgentApiVersion();
|
||||
return agentVersion > 1 ? Browse : BrowseVersion1;
|
||||
}
|
||||
|
||||
service.ls = function (volumeId, path) {
|
||||
return getBrowseService().ls({ volumeID: volumeId, path: path, version: getAgentApiVersion() }).$promise;
|
||||
};
|
||||
|
||||
service.get = function (volumeId, path) {
|
||||
return getBrowseService().get({ volumeID: volumeId, path: path, version: getAgentApiVersion() }).$promise;
|
||||
};
|
||||
|
||||
service.delete = function (volumeId, path) {
|
||||
return getBrowseService().delete({ volumeID: volumeId, path: path, version: getAgentApiVersion() }).$promise;
|
||||
};
|
||||
|
||||
service.rename = function (volumeId, path, newPath) {
|
||||
var payload = {
|
||||
CurrentFilePath: path,
|
||||
NewFilePath: newPath,
|
||||
};
|
||||
return getBrowseService().rename({ volumeID: volumeId, version: getAgentApiVersion() }, payload).$promise;
|
||||
};
|
||||
|
||||
service.upload = function upload(path, file, volumeId, onProgress) {
|
||||
var deferred = $q.defer();
|
||||
var agentVersion = StateManager.getAgentApiVersion();
|
||||
if (agentVersion < 2) {
|
||||
deferred.reject('upload is not supported on this agent version');
|
||||
return;
|
||||
}
|
||||
var url = API_ENDPOINT_ENDPOINTS + '/' + EndpointProvider.endpointID() + '/docker' + '/v' + agentVersion + '/browse/put?volumeID=' + volumeId;
|
||||
const url = `${API_ENDPOINT_ENDPOINTS}/${EndpointProvider.endpointID()}/docker/v${agentVersion}/browse/put?volumeID=${volumeId}`;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
Upload.upload({
|
||||
url: url,
|
||||
data: { file: file, Path: path },
|
||||
}).then(deferred.resolve, deferred.reject, onProgress);
|
||||
return deferred.promise;
|
||||
};
|
||||
|
||||
return service;
|
||||
},
|
||||
]);
|
||||
data: { file, Path: path },
|
||||
}).then(resolve, reject, onProgress);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue