feat(agent): refactor volumeBrowserService to es6 (#4094)

pull/4098/head
Chaim Lev-Ari 2020-07-23 10:46:29 +03:00 committed by GitHub
parent 822c4e117c
commit 99db41f96e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 53 additions and 55 deletions

View File

@ -1,61 +1,59 @@
angular.module('portainer.agent').factory('VolumeBrowserService', [ import angular from 'angular';
'StateManager',
'Browse', angular.module('portainer.agent').factory('VolumeBrowserService', VolumeBrowserServiceFactory);
'BrowseVersion1',
'$q', function VolumeBrowserServiceFactory(StateManager, Browse, BrowseVersion1, API_ENDPOINT_ENDPOINTS, EndpointProvider, Upload) {
'API_ENDPOINT_ENDPOINTS', return {
'EndpointProvider', ls,
'Upload', get,
function VolumeBrowserServiceFactory(StateManager, Browse, BrowseVersion1, $q, API_ENDPOINT_ENDPOINTS, EndpointProvider, Upload) { delete: deletePath,
'use strict'; rename,
var service = {}; upload,
};
function getAgentApiVersion() { function getAgentApiVersion() {
var state = StateManager.getState(); const state = StateManager.getState();
return state.endpoint.agentApiVersion; return state.endpoint.agentApiVersion;
} }
function getBrowseService() { function getBrowseService() {
var agentVersion = getAgentApiVersion(); const agentVersion = getAgentApiVersion();
return agentVersion > 1 ? Browse : BrowseVersion1; return agentVersion > 1 ? Browse : BrowseVersion1;
} }
service.ls = function (volumeId, path) { function ls(volumeId, path) {
return getBrowseService().ls({ volumeID: volumeId, path: path, version: getAgentApiVersion() }).$promise; return getBrowseService().ls({ volumeID: volumeId, path, version: getAgentApiVersion() }).$promise;
}; }
service.get = function (volumeId, path) { function get(volumeId, path) {
return getBrowseService().get({ volumeID: volumeId, path: path, version: getAgentApiVersion() }).$promise; return getBrowseService().get({ volumeID: volumeId, path, version: getAgentApiVersion() }).$promise;
}; }
service.delete = function (volumeId, path) { function deletePath(volumeId, path) {
return getBrowseService().delete({ volumeID: volumeId, path: path, version: getAgentApiVersion() }).$promise; return getBrowseService().delete({ volumeID: volumeId, path, version: getAgentApiVersion() }).$promise;
}; }
service.rename = function (volumeId, path, newPath) { function rename(volumeId, path, newPath) {
var payload = { const payload = {
CurrentFilePath: path, CurrentFilePath: path,
NewFilePath: newPath, NewFilePath: newPath,
}; };
return getBrowseService().rename({ volumeID: volumeId, version: getAgentApiVersion() }, payload).$promise; 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;
function upload(path, file, volumeId, onProgress) {
const agentVersion = StateManager.getAgentApiVersion();
if (agentVersion < 2) {
throw new Error('upload is not supported on this agent version');
}
const url = `${API_ENDPOINT_ENDPOINTS}/${EndpointProvider.endpointID()}/docker/v${agentVersion}/browse/put?volumeID=${volumeId}`;
return new Promise((resolve, reject) => {
Upload.upload({ Upload.upload({
url: url, url: url,
data: { file: file, Path: path }, data: { file, Path: path },
}).then(deferred.resolve, deferred.reject, onProgress); }).then(resolve, reject, onProgress);
return deferred.promise; });
}; }
}
return service;
},
]);