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 pathfileManagerApi.service.ts
More file actions
310 lines (237 loc) · 7.6 KB
/
fileManagerApi.service.ts
File metadata and controls
310 lines (237 loc) · 7.6 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import {Injectable} from '@angular/core';
import {IOuterNode} from '@rign/angular2-tree';
import {Observable} from 'rxjs/Observable';
import {UUID} from 'angular2-uuid';
import {IFileManagerApi} from './IFileManagerApi';
import {IOuterFile} from '../filesList/interface/IOuterFile';
import {IFileDataProperties} from '../services/imageDataConverter.service';
import {ICropBounds} from '../crop/ICropBounds';
import {FilemanagerNotifcations} from '../services/FilemanagerNotifcations';
import {AbstractFileManagerApiService} from './fileManagerApiAbstract.class';
import {INodeService} from '@rign/angular2-tree';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/empty';
import 'rxjs/add/observable/throw';
@Injectable()
export class FileManagerApiService extends AbstractFileManagerApiService implements IFileManagerApi, INodeService {
public constructor(private filemanagerNotfication: FilemanagerNotifcations) {
super();
}
public load(nodeId = ''): Observable<IOuterNode[]> {
if (!this.nodes) {
this.nodes = this.getAllDataFromLocalStorage();
}
const nodes = this.getChildren(nodeId);
return Observable.of(nodes);
}
public add(node: IOuterNode, parentNodeId: string = null): Observable<IOuterNode> {
node.parentId = parentNodeId;
node.id = UUID.UUID();
this.nodes.push(node);
if (this.saveNodes()) {
return Observable.of(node);
} else {
return Observable.empty();
}
}
public move(srcNode: IOuterNode, targetNode: IOuterNode | null): Observable<IOuterNode> {
const srcId = srcNode.id;
const targetId = targetNode ? targetNode.id : '';
const index = this.findIndexByNodeId(srcId);
this.nodes[index].parentId = targetId;
if (this.saveNodes()) {
return Observable.of(this.nodes[index]);
} else {
return Observable.empty();
}
}
public update(node: IOuterNode): Observable<IOuterNode> {
const index = this.findIndexByNodeId(node.id);
this.nodes[index] = node;
if (this.saveNodes()) {
return Observable.of(node);
} else {
return Observable.empty();
}
}
public remove(nodeId: string): Observable<IOuterNode> {
const index = this.findIndexByNodeId(nodeId);
const node = this.nodes[index];
const hasChildren = this.getChildren(nodeId).length > 0;
if (!hasChildren) {
this.nodes.splice(index, 1);
this.saveNodes();
return Observable.of(node);
} else {
return Observable.throw('Node is not empty');
}
}
/**
* Crop file
*/
public cropFile(file: IOuterFile, bounds: ICropBounds): Observable<IOuterFile> {
return Observable.throw('This functionality is not available with LocalStorage');
}
/**
* Load files from directory
*/
public loadFiles(nodeId = ''): Observable<IOuterFile[]> {
this.currentNodeId = nodeId;
if (!this.files) {
this.files = this.getAllFileDataFromLocalStorage();
}
const files = this.getFilesFromFolder(nodeId);
const newFiles: IOuterFile[] = files.map((file: IFileDataProperties) => {
return this.convertLocalData2IOuterFile(file);
});
return Observable.of(newFiles);
}
public removeFile(file: IOuterFile): Observable<boolean> {
const index = this.findIndexByFileId(file.id.toString());
if (index === -1) {
return Observable.of(false);
}
this.files.splice(index, 1);
this.saveFiles();
return Observable.of(true);
}
public removeSelectedFiles(selectedFiles: string[]) {
const numberOfFiles = this.files.length;
selectedFiles.forEach((fileId: string) => {
const index = this.findIndexByFileId(fileId);
if (index > -1) {
this.files.splice(index, 1);
}
});
this.saveFiles();
return Observable.of((this.files.length + selectedFiles.length === numberOfFiles));
}
public uploadFile(file: IOuterFile): Observable<IOuterFile> {
const fileData = this.convertIOuterFile2LocalData(file);
this.files.push(fileData);
if (this.saveFiles()) {
return Observable.of(this.convertLocalData2IOuterFile(fileData));
} else {
return Observable.throw('Upload error');
}
}
public moveFile(files: IOuterFile[], node: IOuterNode = null): Observable<IOuterFile[]> {
const ids: string[] = files.map(file => file.id.toString());
const folderId = node ? node.id.toString() : '';
const movedFiles = this.files.filter(file => ids.indexOf(file.id.toString()) > -1);
const errorMsg = 'Can not move file to the same folder';
let isMovedToSameFolder = false;
movedFiles.forEach((file) => {
if (node) {
if (node.id === file.folderId) {
}
} else {
if (file.folderId === '' || file.folderId === null) {
return Observable.throw(errorMsg);
}
}
file.folderId = folderId;
});
if (isMovedToSameFolder) {
return Observable.throw(errorMsg);
}
if (this.saveFiles()) {
return Observable.of(movedFiles.map(file => this.convertLocalData2IOuterFile(file)));
} else {
return Observable.throw('Move files error');
}
}
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 getFilesFromFolder(nodeId: string): IFileDataProperties[] {
return this.files.filter((file: IFileDataProperties) => file.folderId === nodeId);
}
protected getAllDataFromLocalStorage(): IOuterNode[] {
try {
const data = localStorage.getItem(this.treeName);
if (data) {
return JSON.parse(data);
}
return [];
} catch (e) {
return [];
}
}
protected getAllFileDataFromLocalStorage(): IFileDataProperties[] {
try {
const data = localStorage.getItem(this.fileManagerName);
if (data) {
return JSON.parse(data);
}
return [];
} catch (e) {
return [];
}
}
private saveNodes() {
try {
localStorage.setItem(this.treeName, JSON.stringify(this.nodes));
return true;
} catch (e) {
this.filemanagerNotfication.sendNotification({
type: 'error',
title: 'State is not saved.',
message: 'Reload previous state.'
});
this.files = null;
this.nodes = null;
this.load();
return false;
}
}
private saveFiles(): boolean {
try {
localStorage.setItem(this.fileManagerName, JSON.stringify(this.files));
return true;
} catch (e) {
this.filemanagerNotfication.sendNotification({
type: 'error',
title: 'State is not saved.',
message: 'Reload previous state.'
});
const nodeId = this.files[(this.files.length - 1)].folderId || null;
this.files = null;
this.load(nodeId);
return false;
}
}
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
};
}
}