-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplorer.service.ts
More file actions
85 lines (73 loc) · 2.71 KB
/
explorer.service.ts
File metadata and controls
85 lines (73 loc) · 2.71 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
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { FileItem } from '../../models';
import { environment } from '../../../environments/environment';
@Injectable({
providedIn: 'root',
})
export class ExplorerService {
baseUrl = environment.apiUrl;
// api url
apiUrl = this.baseUrl + '/api/file';
// create constructor to get Http instance
constructor(private http: HttpClient) {}
// fetch file and dir list
getFiles(relPath: string): Observable<FileItem[]> {
return this.http.get<FileItem[]>(this.apiUrl + '/list/?relPath=' + relPath);
}
// get files after sorted
getFilesSortedByTypeAndName(relPath: string): Observable<FileItem[]> {
// sort by directory first, then by name
return this.getFiles(relPath).pipe(
map((data: FileItem[]) =>
data.sort((p1, p2) => {
if (p1.isDir == p2.isDir) {
return p1.name < p2.name ? -1 : 1;
} else {
return p1.isDir ? -1 : 1;
}
}),
),
);
}
// create folder
createFolder(relPath: string, folderName: string): Observable<any> {
return this.http
.post(this.apiUrl + '/create', { relPath, folderName }, { observe: 'response' })
.pipe(map((res) => res.status));
}
// upload file
upload(formData: any): Observable<any> {
return this.http.post(this.apiUrl + '/upload', formData, { observe: 'response' }).pipe(map((res) => res.status));
}
// download file
/* download(relPath: string): Observable<any[]> {
return this.http.get<any>(this.apiUrl + '/download/?path=' + relPath);
} */
// rename file or directory
rename(oldPath: string, newPath: string): Observable<any> {
return this.http
.post(this.apiUrl + '/rename', { oldPath, newPath }, { observe: 'response' })
.pipe(map((res) => res.status));
}
// move files or directories
move(srcPath: string, targetPath: string, fileNames: string[]): Observable<any> {
return this.http
.post(this.apiUrl + '/move', { srcPath, targetPath, fileNames }, { observe: 'response' })
.pipe(map((res) => res.status));
}
// archive file
archive(relPath: string, fileNames: string[], archivedFileName: string, embedDirs: boolean): Observable<any> {
return this.http
.post(this.apiUrl + '/archive', { relPath, fileNames, archivedFileName, embedDirs }, { observe: 'response' })
.pipe(map((res) => res.status));
}
// delete file
delete(relPath: string, fileNames: string[]): Observable<any> {
return this.http
.post(this.apiUrl + '/delete', { relPath, fileNames }, { observe: 'response' })
.pipe(map((res) => res.status));
}
}