forked from joni2back/angular-filemanager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapimiddleware.js
135 lines (107 loc) · 5.24 KB
/
apimiddleware.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
(function(angular) {
'use strict';
angular.module('FileManagerApp').service('apiMiddleware', ['$window', 'fileManagerConfig', 'apiHandler',
function ($window, fileManagerConfig, ApiHandler) {
var ApiMiddleware = function() {
this.apiHandler = new ApiHandler();
};
ApiMiddleware.prototype.getPath = function(arrayPath) {
return '/' + arrayPath.join('/');
};
ApiMiddleware.prototype.getFileList = function(files) {
return (files || []).map(function(file) {
return file && file.model.fullPath();
});
};
ApiMiddleware.prototype.getFilePath = function(item) {
return item && item.model.fullPath();
};
ApiMiddleware.prototype.list = function(path, customDeferredHandler) {
return this.apiHandler.list(fileManagerConfig.listUrl, this.getPath(path), customDeferredHandler);
};
ApiMiddleware.prototype.copy = function(files, path) {
var items = this.getFileList(files);
var singleFilename = items.length === 1 ? files[0].tempModel.name : undefined;
return this.apiHandler.copy(fileManagerConfig.copyUrl, items, this.getPath(path), singleFilename);
};
ApiMiddleware.prototype.move = function(files, path) {
var items = this.getFileList(files);
return this.apiHandler.move(fileManagerConfig.moveUrl, items, this.getPath(path));
};
ApiMiddleware.prototype.remove = function(files) {
var items = this.getFileList(files);
return this.apiHandler.remove(fileManagerConfig.removeUrl, items);
};
ApiMiddleware.prototype.upload = function(files, path) {
if (! $window.FormData) {
throw new Error('Unsupported browser version');
}
var destination = this.getPath(path);
return this.apiHandler.upload(fileManagerConfig.uploadUrl, destination, files);
};
ApiMiddleware.prototype.getContent = function(item) {
var itemPath = this.getFilePath(item);
return this.apiHandler.getContent(fileManagerConfig.getContentUrl, itemPath);
};
ApiMiddleware.prototype.edit = function(item) {
var itemPath = this.getFilePath(item);
return this.apiHandler.edit(fileManagerConfig.editUrl, itemPath, item.tempModel.content);
};
ApiMiddleware.prototype.rename = function(item) {
var itemPath = this.getFilePath(item);
var newPath = item.tempModel.fullPath();
return this.apiHandler.rename(fileManagerConfig.renameUrl, itemPath, newPath);
};
ApiMiddleware.prototype.getUrl = function(item) {
var itemPath = this.getFilePath(item);
return this.apiHandler.getUrl(fileManagerConfig.downloadFileUrl, itemPath);
};
ApiMiddleware.prototype.download = function(item, forceNewWindow) {
//TODO: add spinner to indicate file is downloading
var itemPath = this.getFilePath(item);
var toFilename = item.model.name;
if (item.isFolder()) {
return;
}
return this.apiHandler.download(
fileManagerConfig.downloadFileUrl,
itemPath,
toFilename,
fileManagerConfig.downloadFilesByAjax,
forceNewWindow
);
};
ApiMiddleware.prototype.downloadMultiple = function(files, forceNewWindow) {
var items = this.getFileList(files);
var timestamp = new Date().getTime().toString().substr(8, 13);
var toFilename = timestamp + '-' + fileManagerConfig.multipleDownloadFileName;
return this.apiHandler.downloadMultiple(
fileManagerConfig.downloadMultipleUrl,
items,
toFilename,
fileManagerConfig.downloadFilesByAjax,
forceNewWindow
);
};
ApiMiddleware.prototype.compress = function(files, compressedFilename, path) {
var items = this.getFileList(files);
return this.apiHandler.compress(fileManagerConfig.compressUrl, items, compressedFilename, this.getPath(path));
};
ApiMiddleware.prototype.extract = function(item, folderName, path) {
var itemPath = this.getFilePath(item);
return this.apiHandler.extract(fileManagerConfig.extractUrl, itemPath, folderName, this.getPath(path));
};
ApiMiddleware.prototype.changePermissions = function(files, dataItem) {
var items = this.getFileList(files);
var code = dataItem.tempModel.perms.toCode();
var octal = dataItem.tempModel.perms.toOctal();
var recursive = !!dataItem.tempModel.recursive;
return this.apiHandler.changePermissions(fileManagerConfig.permissionsUrl, items, code, octal, recursive);
};
ApiMiddleware.prototype.createFolder = function(item) {
var path = item.tempModel.fullPath();
return this.apiHandler.createFolder(fileManagerConfig.createFolderUrl, path);
};
return ApiMiddleware;
}]);
})(angular);