This repository was archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfileManagerBackendApi.service.ts
More file actions
230 lines (186 loc) · 6.43 KB
/
fileManagerBackendApi.service.ts
File metadata and controls
230 lines (186 loc) · 6.43 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import {Injectable} from '@angular/core';
import {IOuterNode} from '@rign/angular2-tree';
import {Observable} from 'rxjs/Observable';
import {ICropBounds, IFileManagerApi, IOuterFile, IFileDataProperties} from '../../main';
import {FileManagerConfiguration} from '../configuration/fileManagerConfiguration.service';
import {AbstractFileManagerApiService} from './fileManagerApiAbstract.class';
import {HttpClient, HttpParams} from '@angular/common/http';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/throw';
@Injectable()
export class FileManagerBackendApiService extends AbstractFileManagerApiService implements IFileManagerApi {
public constructor(private $http: HttpClient,
private configuration: FileManagerConfiguration) {
super();
this.nodes = [];
this.files = [];
}
/**
* Load folder chidls for given folder id
*/
public load(nodeId = ''): Observable<IOuterNode[]> {
const nodeIds = this.nodes.map((node: IOuterNode) => node.id);
const params = new HttpParams().set('nodeId', nodeId || '');
return this.$http.get<IOuterNode[]>(this.configuration.folderUrls.foldersUrl, {params})
.map((nodes: IOuterNode[]) => {
nodes.forEach((node: IOuterNode) => {
if (nodeIds.indexOf(node.id) === -1) {
this.nodes.push(node);
} else {
const index = this.nodes.findIndex((item: IOuterNode) => node.id === item.id);
this.nodes[index] = node;
}
});
return nodes;
});
}
/**
* Create new folder
*/
public add(node: IOuterNode, parentNodeId: string = null): Observable<IOuterNode> {
const data = {
node: node,
parentNodeId: parentNodeId
};
return this.$http.post<IOuterNode>(this.configuration.folderUrls.foldersUrl, data)
.map((newNode: IOuterNode) => {
this.nodes.push(newNode);
return newNode;
})
}
/**
* Move folder from source parent to target parent
*/
public move(srcNode: IOuterNode, targetNode: IOuterNode | null): Observable<IOuterNode> {
const srcId = srcNode.id;
const targetId = targetNode ? targetNode.id : null;
return this.$http.put<IOuterNode>(this.configuration.folderUrls.folderMoveUrl, {source: srcId, target: targetId})
.map((movedNode: IOuterNode) => {
const index = this.findIndexByNodeId(srcId);
this.nodes[index].parentId = targetId;
return movedNode;
});
}
/**
* Update folder name
*/
public update(node: IOuterNode): Observable<IOuterNode> {
return this.$http.put<IOuterNode>(this.configuration.folderUrls.foldersUrl, node)
.map((newNode: IOuterNode) => {
const index = this.findIndexByNodeId(node.id);
this.nodes[index] = newNode;
return newNode;
});
}
/**
* Remove node by given id
*/
public remove(nodeId: string): Observable<IOuterNode> {
const index = this.findIndexByNodeId(nodeId);
const hasChildren = this.getChildren(nodeId).length > 0;
if (!hasChildren) {
const params = new HttpParams().set('nodeId', nodeId);
return this.$http.delete<IOuterNode>(this.configuration.folderUrls.foldersUrl, {params})
.map((removedNode: IOuterNode) => {
this.nodes.splice(index, 1);
return removedNode;
});
} else {
return Observable.throw('Node is not empty');
}
}
/**
* Crop file
*/
public cropFile(file: IOuterFile, bounds: ICropBounds): Observable<IOuterFile> {
return this.$http.put<IOuterFile>(this.configuration.fileUrl, {id: file.id, bounds: bounds});
}
/**
* Load files from directory
*/
public loadFiles(nodeId = ''): Observable<IOuterFile[]> {
this.currentNodeId = nodeId;
const params = new HttpParams().set('dirId', nodeId);
return this.$http.get<IOuterFile[]>(this.configuration.fileUrl, {params})
.map((files: IOuterFile[]) => {
this.files = files.map((file: IOuterFile) => <IFileDataProperties>file);
return files;
})
}
/**
* Remove file from folder
*/
public removeFile(file: IOuterFile): Observable<boolean> {
const index = this.findIndexByFileId(file.id.toString());
if (index === -1) {
return Observable.of(false);
}
const params = new HttpParams().set('id', file.id.toString());
return this.$http.delete<any>(this.configuration.fileUrl, {params})
.map(() => {
this.files.splice(index, 1);
return true;
});
}
public removeSelectedFiles(selectedFiles: string[]) {
const params = new HttpParams().set('id', selectedFiles.join('|'));
return this.$http.delete<any>(this.configuration.fileUrl, {params})
.map(() => {
selectedFiles.forEach((fileId: string) => {
const index = this.findIndexByFileId(fileId);
if (index > -1) {
this.files.splice(index, 1);
}
});
return true;
});
}
/**
* This method is success method, real upload is done in ExtendedFileUploader
*/
public uploadFile(file: IOuterFile): Observable<IOuterFile> {
const fileData = <IFileDataProperties>file;
this.files.push(fileData);
return Observable.of(file);
}
public moveFile(files: IOuterFile[], node: IOuterNode): Observable<IOuterFile[]> {
const ids: string[] = files.map(file => file.id.toString());
return this.$http.put<IOuterFile[]>(this.configuration.fileUrl, {files: ids, folderId: node ? node.id : ''});
}
private findIndexByNodeId(nodeId: string): number {
return this.nodes.findIndex((node) => {
return node.id === nodeId;
});
}
private findIndexByFileId(fileId: string): number {
return this.files.findIndex((file) => file.id === fileId);
}
private getChildren(nodeId: string): IOuterNode[] {
return this.nodes.filter((node: IOuterNode) => node.parentId === nodeId);
}
private convertLocalData2IOuterFile(file: IFileDataProperties): IOuterFile {
return {
id: file.id,
folderId: file.folderId,
name: file.name,
thumbnailUrl: file.data,
url: file.data,
width: file.width,
height: file.height,
type: file.type,
size: file.size
}
}
private convertIOuterFile2LocalData(file: IOuterFile): IFileDataProperties {
return {
id: file.id.toString(),
folderId: file.folderId,
name: file.name,
type: file.type,
data: file.data,
size: file.size,
width: file.width,
height: file.height
}
}
}