From 19cc94e594e84b6ade861ceb0af26dc378974a15 Mon Sep 17 00:00:00 2001 From: Jason Rasmussen Date: Sat, 1 Jul 2023 00:44:55 -0400 Subject: [PATCH] refactor(server): use better algorithm for calculating the duplicate filename (#3061) --- server/src/domain/asset/asset.service.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/server/src/domain/asset/asset.service.ts b/server/src/domain/asset/asset.service.ts index 51d3afb8dc..901b21a143 100644 --- a/server/src/domain/asset/asset.service.ts +++ b/server/src/domain/asset/asset.service.ts @@ -96,19 +96,17 @@ export class AssetService { const zip = this.storageRepository.createZipStream(); const assets = await this.assetRepository.getByIds(dto.assetIds); - const paths: Record = {}; + const paths: Record = {}; for (const { originalPath, originalFileName } of assets) { const ext = extname(originalPath); let filename = `${originalFileName}${ext}`; - for (let i = 0; i < 10_000; i++) { - if (!paths[filename]) { - break; - } - filename = `${originalFileName}+${i + 1}${ext}`; + const count = paths[filename] || 0; + paths[filename] = count + 1; + if (count !== 0) { + filename = `${originalFileName}+${count}${ext}`; } - paths[filename] = true; zip.addFile(originalPath, filename); }