From 0bb0d69543c8bc393d28e25f25019cd545be040a Mon Sep 17 00:00:00 2001 From: smitten Date: Sat, 30 Dec 2023 20:44:31 -0500 Subject: [PATCH 1/4] Use hashed filename for exists check --- .../src/queue/processors/ImportNotesProcessorService.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/backend/src/queue/processors/ImportNotesProcessorService.ts b/packages/backend/src/queue/processors/ImportNotesProcessorService.ts index 552b69d92d..67078adaf6 100644 --- a/packages/backend/src/queue/processors/ImportNotesProcessorService.ts +++ b/packages/backend/src/queue/processors/ImportNotesProcessorService.ts @@ -1,5 +1,6 @@ import * as fs from 'node:fs'; import * as vm from 'node:vm'; +import * as crypto from 'node:crypto'; import { Inject, Injectable } from '@nestjs/common'; import { ZipReader } from 'slacc'; import { DI } from '@/di-symbols.js'; @@ -469,7 +470,10 @@ export class ImportNotesProcessorService { for await (const file of post.object.attachment) { const slashdex = file.url.lastIndexOf('/'); - const name = file.url.substring(slashdex + 1); + const filename = file.url.substring(slashdex + 1); + const hash = crypto.createHash('md5').setEncoding('hex'); + const urlHash = hash.update(file.url).digest('base64'); + const name = `${urlHash}-${filename}`; const [filePath, cleanup] = await createTemp(); const exists = await this.driveFilesRepository.findOneBy({ name: name, userId: user.id }) ?? await this.driveFilesRepository.findOneBy({ name: name, userId: user.id, folderId: pleroFolder?.id }); @@ -484,6 +488,7 @@ export class ImportNotesProcessorService { user: user, path: filePath, name: name, + comment: file.name, folderId: pleroFolder?.id, }); files.push(driveFile); From e9428a5a0542479a4b45e3b905c677d4f1da6e9a Mon Sep 17 00:00:00 2001 From: smitten Date: Sun, 31 Dec 2023 09:03:46 -0500 Subject: [PATCH 2/4] Use hex digest --- .../src/queue/processors/ImportNotesProcessorService.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/queue/processors/ImportNotesProcessorService.ts b/packages/backend/src/queue/processors/ImportNotesProcessorService.ts index 67078adaf6..769f4d1db7 100644 --- a/packages/backend/src/queue/processors/ImportNotesProcessorService.ts +++ b/packages/backend/src/queue/processors/ImportNotesProcessorService.ts @@ -471,8 +471,8 @@ export class ImportNotesProcessorService { for await (const file of post.object.attachment) { const slashdex = file.url.lastIndexOf('/'); const filename = file.url.substring(slashdex + 1); - const hash = crypto.createHash('md5').setEncoding('hex'); - const urlHash = hash.update(file.url).digest('base64'); + const hash = crypto.createHash('md5'); + const urlHash = hash.update(file.url).digest('hex'); const name = `${urlHash}-${filename}`; const [filePath, cleanup] = await createTemp(); From 327694d4cff00f64f5529d017524c27e848db562 Mon Sep 17 00:00:00 2001 From: smitten Date: Sun, 31 Dec 2023 09:13:51 -0500 Subject: [PATCH 3/4] Use base64url digest --- .../backend/src/queue/processors/ImportNotesProcessorService.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/backend/src/queue/processors/ImportNotesProcessorService.ts b/packages/backend/src/queue/processors/ImportNotesProcessorService.ts index 769f4d1db7..40aa7ffc18 100644 --- a/packages/backend/src/queue/processors/ImportNotesProcessorService.ts +++ b/packages/backend/src/queue/processors/ImportNotesProcessorService.ts @@ -472,7 +472,7 @@ export class ImportNotesProcessorService { const slashdex = file.url.lastIndexOf('/'); const filename = file.url.substring(slashdex + 1); const hash = crypto.createHash('md5'); - const urlHash = hash.update(file.url).digest('hex'); + const urlHash = hash.update(file.url).digest('base64url'); const name = `${urlHash}-${filename}`; const [filePath, cleanup] = await createTemp(); From 8d6d5923daff95b484a4d9ad92d0c43e6ac9ec96 Mon Sep 17 00:00:00 2001 From: smitten Date: Sun, 31 Dec 2023 11:14:41 -0500 Subject: [PATCH 4/4] Simplify hash steps --- .../src/queue/processors/ImportNotesProcessorService.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/packages/backend/src/queue/processors/ImportNotesProcessorService.ts b/packages/backend/src/queue/processors/ImportNotesProcessorService.ts index 40aa7ffc18..a49e0d4dde 100644 --- a/packages/backend/src/queue/processors/ImportNotesProcessorService.ts +++ b/packages/backend/src/queue/processors/ImportNotesProcessorService.ts @@ -471,9 +471,8 @@ export class ImportNotesProcessorService { for await (const file of post.object.attachment) { const slashdex = file.url.lastIndexOf('/'); const filename = file.url.substring(slashdex + 1); - const hash = crypto.createHash('md5'); - const urlHash = hash.update(file.url).digest('base64url'); - const name = `${urlHash}-${filename}`; + const hash = crypto.createHash('md5').update(file.url).digest('base64url'); + const name = `${hash}-${filename}`; const [filePath, cleanup] = await createTemp(); const exists = await this.driveFilesRepository.findOneBy({ name: name, userId: user.id }) ?? await this.driveFilesRepository.findOneBy({ name: name, userId: user.id, folderId: pleroFolder?.id });