-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfolders.service.ts
281 lines (233 loc) · 9.96 KB
/
folders.service.ts
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
import { InjectRepository } from "@mikro-orm/nestjs";
import { EntityRepository, QueryBuilder } from "@mikro-orm/postgresql";
import { forwardRef, Inject, Injectable, Logger } from "@nestjs/common";
import JSZip from "jszip";
import { BlobStorageBackendService } from "../../blob-storage/backends/blob-storage-backend.service";
import { CometEntityNotFoundException } from "../../common/errors/entity-not-found.exception";
import { SortDirection } from "../../common/sorting/sort-direction.enum";
import { DamConfig } from "../dam.config";
import { DAM_CONFIG } from "../dam.constants";
import { FolderArgs } from "./dto/folder.args";
import { CreateFolderInput, UpdateFolderInput } from "./dto/folder.input";
import { Folder } from "./entities/folder.entity";
import { FilesService } from "./files.service";
import { createHashedPath } from "./files.utils";
export const withFoldersSelect = (
qb: QueryBuilder<Folder>,
args: {
includeArchived?: boolean;
parentId?: string | null;
query?: string;
sortColumnName?: string;
sortDirection?: SortDirection;
offset?: number;
limit?: number;
},
): QueryBuilder<Folder> => {
if (!args.includeArchived) {
qb.where({ archived: false });
}
const isSearching = args.query !== undefined && args.query.length > 0;
if (!isSearching) {
if (args.parentId !== undefined) {
qb.where({ parent: { id: args.parentId } });
} else {
qb.where({ parent: { id: null } });
}
}
if (args.query) {
qb = addSearchTermFiltertoQueryBuilder(qb, args.query);
}
if (args.sortColumnName && args.sortDirection) {
qb.orderBy({ [`folder.${args.sortColumnName}`]: args.sortDirection });
}
if (args.offset) {
qb.offset(args.offset);
}
if (args.limit) {
qb.limit(args.limit);
}
return qb;
};
const addSearchTermFiltertoQueryBuilder = (qb: QueryBuilder<Folder>, searchText: string): QueryBuilder<Folder> => {
const terms = searchText.split(" ");
for (const term of terms) {
qb.andWhere({ name: { $ilike: `%${term}%` } });
}
return qb;
};
@Injectable()
export class FoldersService {
protected readonly logger = new Logger(FoldersService.name);
constructor(
@InjectRepository(Folder) private readonly foldersRepository: EntityRepository<Folder>,
@Inject(forwardRef(() => FilesService)) private readonly filesService: FilesService,
@Inject(forwardRef(() => BlobStorageBackendService)) private readonly blobStorageBackendService: BlobStorageBackendService,
@Inject(DAM_CONFIG) private readonly config: DamConfig,
) {}
async findAll({ parentId, includeArchived, filter, sortColumnName, sortDirection }: Omit<FolderArgs, "offset" | "limit">): Promise<Folder[]> {
const qb = withFoldersSelect(this.selectQueryBuilder(), {
includeArchived,
parentId,
query: filter?.searchText,
sortColumnName,
sortDirection,
});
return qb.getResult();
}
async findAndCount({ parentId, includeArchived, filter, sortColumnName, sortDirection, offset, limit }: FolderArgs): Promise<[Folder[], number]> {
const args = {
includeArchived,
parentId,
query: filter?.searchText,
sortColumnName,
sortDirection,
offset,
limit,
};
const qb = withFoldersSelect(this.selectQueryBuilder(), args);
const folders = await qb.getResult();
const countQb = withFoldersSelect(this.countQueryBuilder(), args);
const totalCount = await countQb.getCount();
return [folders, totalCount];
}
async findAllByIds(ids: string[]): Promise<Folder[]> {
return this.selectQueryBuilder()
.where({ id: { $in: ids } })
.getResult();
}
async findOneById(id: string): Promise<Folder | null> {
const qb = this.selectQueryBuilder().where({ id });
return qb.getSingleResult();
}
async findOneByNameAndParentId(name: string, parentId?: string): Promise<Folder | null> {
const qb = this.selectQueryBuilder().andWhere({ name });
if (parentId) {
qb.andWhere({ parent: { id: parentId } });
} else {
qb.andWhere({ parent: { id: null } });
}
return qb.getSingleResult();
}
async create({ parentId, ...data }: CreateFolderInput): Promise<Folder> {
let parent = undefined;
let mpath: string[] = [];
if (parentId) {
parent = await this.findOneById(parentId);
mpath = (await this.findAncestorsByParentId(parentId)).map((folder) => folder.id);
}
const folder = this.foldersRepository.create({ ...data, parent, mpath });
await this.foldersRepository.persistAndFlush(folder);
return folder;
}
async updateById(id: string, data: UpdateFolderInput): Promise<Folder> {
const folder = await this.findOneById(id);
if (!folder) throw new CometEntityNotFoundException();
return this.updateByEntity(folder, data);
}
async updateByEntity(entity: Folder, { parentId, ...input }: UpdateFolderInput): Promise<Folder> {
const parentIsDirty = parentId !== undefined && entity.parent?.id !== parentId;
const parent = parentId ? await this.findOneById(parentId) : null;
const folder = entity.assign({
...input,
parent: parentId !== undefined ? parent : entity.parent,
});
if (parentIsDirty) {
folder.mpath = folder.parent ? (await this.findAncestorsByParentId(folder.parent.id)).map((f) => f.id) : [];
const qb = this.foldersRepository.createQueryBuilder();
await qb
.update({
mpath: qb.raw("array_cat(ARRAY[?]::uuid[], mpath[(array_position(mpath, ?)):array_length(mpath,1)])", [folder.mpath, folder.id]),
})
.where("? = ANY(mpath)", [folder.id])
.execute();
}
await this.foldersRepository.persistAndFlush(folder);
return folder;
}
async moveBatch(folderIds: string[], targetFolderId?: string): Promise<Folder[]> {
const folders = [];
for (const id of folderIds) {
const folder = await this.updateById(id, { parentId: targetFolderId });
folders.push(folder);
}
return folders;
}
async delete(id: string): Promise<boolean> {
const files = await this.filesService.findAll({ folderId: id });
for (const file of files) {
await this.filesService.delete(file.id);
}
const subFolders = await this.findAll({ parentId: id });
for (const subFolder of subFolders) {
await this.delete(subFolder.id);
}
const result = await this.foldersRepository.nativeDelete(id);
return result === 1;
}
async findAncestorsByParentId(parentId: string | null): Promise<Folder[]> {
const parents: Folder[] = [];
while (parentId !== null) {
const folder = (await this.findOneById(parentId)) as Folder;
parents.push(folder);
parentId = folder.parent?.id ?? null;
}
return parents.reverse();
}
async findAncestorsByMaterializedPath(mpath: string[]): Promise<Folder[]> {
const folders = await this.selectQueryBuilder()
.where({ id: { $in: mpath } })
.getResult();
return mpath.map((id) => folders.find((folder) => folder.id === id) as Folder);
}
async createZipStreamFromFolder(folderId: string): Promise<NodeJS.ReadableStream> {
const zip = new JSZip();
await this.addFolderToZip(folderId, zip);
return zip.generateNodeStream({ streamFiles: true });
}
private async addFolderToZip(folderId: string, zip: JSZip): Promise<void> {
const files = await this.filesService.findAll({ folderId: folderId });
const subfolders = await this.findAll({ parentId: folderId });
for (const file of files) {
const fileStream = await this.blobStorageBackendService.getFile(this.config.filesDirectory, createHashedPath(file.contentHash));
zip.file(file.name, fileStream);
}
const countedSubfolderNames: Record<string, number> = {};
for (const subfolder of subfolders) {
const subfolderName = subfolder.name;
const updatedSubfolderName = this.getUniqueFolderName(subfolderName, countedSubfolderNames);
const subfolderZip = zip.folder(updatedSubfolderName);
if (!subfolderZip) {
throw new Error(`Error while creating zip from folder with id ${folderId}`);
}
await this.addFolderToZip(subfolder.id, subfolderZip);
}
}
private getUniqueFolderName(folderName: string, countedFolderNames: Record<string, number>) {
if (!countedFolderNames[folderName]) {
countedFolderNames[folderName] = 1;
} else {
countedFolderNames[folderName]++;
}
const duplicateCount = countedFolderNames[folderName];
let updatedFolderName = folderName;
if (duplicateCount > 1) {
updatedFolderName = `${folderName} ${duplicateCount}`;
}
return updatedFolderName;
}
private selectQueryBuilder(): QueryBuilder<Folder> {
return this.foldersRepository
.createQueryBuilder("folder")
.select("*")
.leftJoinAndSelect("folder.parent", "parent")
.addSelect('COUNT(DISTINCT children.id) as "numberOfChildFolders"')
.leftJoin("folder.children", "children")
.addSelect('COUNT(DISTINCT files.id) as "numberOfFiles"')
.leftJoin("folder.files", "files")
.groupBy(["folder.id", "parent.id"]);
}
private countQueryBuilder(): QueryBuilder<Folder> {
return this.foldersRepository.createQueryBuilder("folder").select("*");
}
}