Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(documents): restore document in trash when same file is uploaded (#213)

Corentin Thomasset2a73551ca4be
diff --git a/apps/papra-server/src/modules/documents/documents.repository.ts b/apps/papra-server/src/modules/documents/documents.repository.ts
index b999c2d6..550874fe 100644
--- a/apps/papra-server/src/modules/documents/documents.repository.ts
+++ b/apps/papra-server/src/modules/documents/documents.repository.ts
@@ -226,20 +226,25 @@ async function softDeleteDocument({ documentId, organizationId, userId, db, now
);
}
-async function restoreDocument({ documentId, organizationId, db }: { documentId: string;organizationId: string; db: Database }) {
- await db
+async function restoreDocument({ documentId, organizationId, name, userId, db }: { documentId: string;organizationId: string; name?: string; userId?: string; db: Database }) {
+ const [document] = await db
.update(documentsTable)
.set({
isDeleted: false,
deletedBy: null,
deletedAt: null,
+ ...(name ? { name, originalName: name } : {}),
+ ...(userId ? { createdBy: userId } : {}),
})
.where(
and(
eq(documentsTable.id, documentId),
eq(documentsTable.organizationId, organizationId),
),
- );
+ )
+ .returning();
+
+ return { document };
}
async function hardDeleteDocument({ documentId, db }: { documentId: string; db: Database }) {
diff --git a/apps/papra-server/src/modules/documents/documents.usecases.test.ts b/apps/papra-server/src/modules/documents/documents.usecases.test.ts
index 90e27df3..9106498b 100644
--- a/apps/papra-server/src/modules/documents/documents.usecases.test.ts
+++ b/apps/papra-server/src/modules/documents/documents.usecases.test.ts
@@ -7,6 +7,7 @@ import { collectReadableStreamToString } from '../shared/streams/readable-stream
import { createSubscriptionsRepository } from '../subscriptions/subscriptions.repository';
import { createTaggingRulesRepository } from '../tagging-rules/tagging-rules.repository';
import { createTagsRepository } from '../tags/tags.repository';
+import { documentsTagsTable } from '../tags/tags.table';
import { createDummyTrackingServices } from '../tracking/tracking.services';
import { createDocumentAlreadyExistsError } from './documents.errors';
import { createDocumentsRepository } from './documents.repository';
@@ -75,7 +76,7 @@ describe('documents usecases', () => {
expect(documentRecords).to.eql([document]);
});
- test('in the same organization, we should be able to have two documents with the same content, an error is raised if the document already exists', async () => {
+ test('in the same organization, we should not be able to have two documents with the same content, an error is raised if the document already exists', async () => {
const { db } = await createInMemoryDatabase({
users: [{ id: 'user-1', email: '[email protected]' }],
organizations: [{ id: 'organization-1', name: 'Organization 1' }],
@@ -151,6 +152,96 @@ describe('documents usecases', () => {
).rejects.toThrow('File not found');
});
+ test(`if the document already exists but is in the trash
+ - we restore the document
+ - update the existing record, setting the name to the new file name (same content does not mean same name)
+ - pre-existing tags are removed
+ - the tagging rules are applied to the restored document`, async () => {
+ // this is the sha256 hash of 'hello world'
+ const hash = 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9';
+
+ // The document is deleted and has the tag-1
+ // A tagging rule that apply tag-2 if the content contains 'hello'
+ // When restoring the document, the tagging rule should apply tag-2
+ const { db } = await createInMemoryDatabase({
+ users: [{ id: 'user-1', email: '[email protected]' }],
+ organizations: [{ id: 'organization-1', name: 'Organization 1' }],
+ organizationMembers: [{ organizationId: 'organization-1', userId: 'user-1', role: ORGANIZATION_ROLES.OWNER }],
+ tags: [
+ { id: 'tag-1', name: 'Tag 1', color: '#000000', organizationId: 'organization-1' },
+ { id: 'tag-2', name: 'Tag 2', color: '#000000', organizationId: 'organization-1' },
+ ],
+ documents: [{
+ id: 'document-1',
+ organizationId: 'organization-1',
+ originalSha256Hash: hash,
+ isDeleted: true,
+ mimeType: 'text/plain',
+ originalStorageKey: 'organization-1/originals/document-1.txt',
+ name: 'file-1.txt',
+ originalName: 'file-1.txt',
+ content: 'hello world',
+ }],
+ documentsTags: [{
+ documentId: 'document-1',
+ tagId: 'tag-1',
+ }],
+ taggingRules: [
+ { id: 'tagging-rule-1', organizationId: 'organization-1', name: 'Tagging Rule 1', enabled: true },
+ ],
+ taggingRuleConditions: [
+ { id: 'tagging-rule-condition-1', taggingRuleId: 'tagging-rule-1', field: 'content', operator: 'contains', value: 'hello' },
+ ],
+ taggingRuleActions: [
+ { id: 'tagging-rule-action-1', taggingRuleId: 'tagging-rule-1', tagId: 'tag-2' },
+ ],
+ });
+
+ const documentsRepository = createDocumentsRepository({ db });
+ const documentsStorageService = await createDocumentStorageService({ config: { documentsStorage: { driver: 'in-memory' } } as Config });
+ const plansRepository = createPlansRepository({ config: { organizationPlans: { isFreePlanUnlimited: true } } as Config });
+ const subscriptionsRepository = createSubscriptionsRepository({ db });
+ const trackingServices = createDummyTrackingServices();
+ const taggingRulesRepository = createTaggingRulesRepository({ db });
+ const tagsRepository = createTagsRepository({ db });
+
+ // 3. Re-create the document
+ const { document: documentRestored } = await createDocument({
+ file: new File(['hello world'], 'file-2.txt', { type: 'text/plain' }),
+ organizationId: 'organization-1',
+ documentsRepository,
+ documentsStorageService,
+ plansRepository,
+ subscriptionsRepository,
+ trackingServices,
+ taggingRulesRepository,
+ tagsRepository,
+ });
+
+ expect(documentRestored).to.deep.include({
+ id: 'document-1',
+ organizationId: 'organization-1',
+ name: 'file-2.txt',
+ originalName: 'file-2.txt',
+ isDeleted: false,
+ deletedBy: null,
+ deletedAt: null,
+ });
+
+ const documentsRecordsAfterRestoration = await db.select().from(documentsTable);
+
+ expect(documentsRecordsAfterRestoration.length).to.eql(1);
+
+ expect(documentsRecordsAfterRestoration[0]).to.eql(documentRestored);
+
+ const documentsTagsRecordsAfterRestoration = await db.select().from(documentsTagsTable);
+
+ expect(documentsTagsRecordsAfterRestoration).to.eql([{
+ documentId: 'document-1',
+ tagId: 'tag-2',
+ }]);
+ });
+
test('when there is an issue when inserting the document in the db, the file should not be saved in the storage', async () => {
const { db } = await createInMemoryDatabase({
users: [{ id: 'user-1', email: '[email protected]' }],
diff --git a/apps/papra-server/src/modules/documents/documents.usecases.ts b/apps/papra-server/src/modules/documents/documents.usecases.ts
index 2fdde9c9..df5d8265 100644
--- a/apps/papra-server/src/modules/documents/documents.usecases.ts
+++ b/apps/papra-server/src/modules/documents/documents.usecases.ts
@@ -6,6 +6,7 @@ import type { TaggingRulesRepository } from '../tagging-rules/tagging-rules.repo
import type { TagsRepository } from '../tags/tags.repository';
import type { TrackingServices } from '../tracking/tracking.services';
import type { DocumentsRepository } from './documents.repository';
+import type { Document } from './documents.types';
import type { DocumentStorageService } from './storage/documents.storage.services';
import { safely } from '@corentinth/chisels';
import { extractTextFromFile } from '@papra/lecture';
@@ -77,10 +78,93 @@ export async function createDocument({
// Early check to avoid saving the file and then realizing it already exists with the db constraint
const { document: existingDocument } = await documentsRepository.getOrganizationDocumentBySha256Hash({ sha256Hash: hash, organizationId });
- if (existingDocument) {
+ const { document } = existingDocument
+ ? await handleExistingDocument({
+ existingDocument,
+ fileName,
+ organizationId,
+ documentsRepository,
+ tagsRepository,
+ logger,
+ })
+ : await createNewDocument({
+ file,
+ fileName,
+ size,
+ mimeType,
+ hash,
+ userId,
+ organizationId,
+ documentsRepository,
+ documentsStorageService,
+ generateDocumentId,
+ trackingServices,
+ logger,
+ });
+
+ await applyTaggingRules({ document, taggingRulesRepository, tagsRepository });
+
+ return { document };
+}
+
+async function handleExistingDocument({
+ existingDocument,
+ fileName,
+ userId,
+ organizationId,
+ documentsRepository,
+ tagsRepository,
+ logger,
+}: {
+ existingDocument: Document;
+ fileName: string;
+ userId?: string;
+ organizationId: string;
+ documentsRepository: DocumentsRepository;
+ tagsRepository: TagsRepository;
+ logger: Logger;
+}) {
+ if (existingDocument && !existingDocument.isDeleted) {
throw createDocumentAlreadyExistsError();
}
+ logger.info({ documentId: existingDocument.id }, 'Document already exists, restoring for deduplication');
+
+ const [, { document: restoredDocument }] = await Promise.all([
+ tagsRepository.removeAllTagsFromDocument({ documentId: existingDocument.id }),
+ documentsRepository.restoreDocument({ documentId: existingDocument.id, organizationId, name: fileName, userId }),
+ ]);
+
+ return { document: restoredDocument };
+}
+
+async function createNewDocument({
+ file,
+ fileName,
+ size,
+ mimeType,
+ hash,
+ userId,
+ organizationId,
+ documentsRepository,
+ documentsStorageService,
+ generateDocumentId,
+ trackingServices,
+ logger,
+}: {
+ file: File;
+ fileName: string;
+ size: number;
+ mimeType: string;
+ hash: string;
+ userId?: string;
+ organizationId: string;
+ documentsRepository: DocumentsRepository;
+ documentsStorageService: DocumentStorageService;
+ generateDocumentId: () => string;
+ trackingServices: TrackingServices;
+ logger: Logger;
+}) {
const documentId = generateDocumentId();
const { originalDocumentStorageKey } = buildOriginalDocumentKey({
@@ -107,8 +191,7 @@ export async function createDocument({
mimeType,
content: text,
originalSha256Hash: hash,
- }),
- );
+ }));
if (error) {
logger.error({ error }, 'Error while creating document');
@@ -127,11 +210,7 @@ export async function createDocument({
logger.info({ documentId, userId, organizationId }, 'Document created');
- await applyTaggingRules({ document: result.document, taggingRulesRepository, tagsRepository });
-
- const { document } = result;
-
- return { document };
+ return { document: result.document };
}
export async function getDocumentOrThrow({
diff --git a/apps/papra-server/src/modules/tags/tags.repository.ts b/apps/papra-server/src/modules/tags/tags.repository.ts
index afdef72a..aa26016f 100644
--- a/apps/papra-server/src/modules/tags/tags.repository.ts
+++ b/apps/papra-server/src/modules/tags/tags.repository.ts
@@ -19,6 +19,7 @@ export function createTagsRepository({ db }: { db: Database }) {
addTagToDocument,
addTagsToDocument,
removeTagFromDocument,
+ removeAllTagsFromDocument,
},
{ db },
);
@@ -92,3 +93,7 @@ async function removeTagFromDocument({ tagId, documentId, db }: { tagId: string;
),
);
}
+
+async function removeAllTagsFromDocument({ documentId, db }: { documentId: string; db: Database }) {
+ await db.delete(documentsTagsTable).where(eq(documentsTagsTable.documentId, documentId));
+}