Skip to content

Commit

Permalink
feat(documents): prevent duplicate document uploads within an organiz…
Browse files Browse the repository at this point in the history
…ation (#140)

* feat(documents): prevent duplicate document uploads within an organization

* Update apps/papra-server/src/modules/documents/documents.usecases.test.ts

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
  • Loading branch information
CorentinTh and Copilot authored Feb 28, 2025
1 parent 4be13d0 commit f78d42c
Show file tree
Hide file tree
Showing 14 changed files with 1,216 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import type { Document } from './documents.types';
import { safely } from '@corentinth/chisels';
import { createSignal } from 'solid-js';
import { useConfirmModal } from '../shared/confirm';
import { promptUploadFiles } from '../shared/files/upload';
import { isHttpErrorWithCode } from '../shared/http/http-errors';
import { queryClient } from '../shared/query/query-client';
import { createToast } from '../ui/components/sonner';
import { deleteDocument, restoreDocument, uploadDocument } from './documents.services';
Expand Down Expand Up @@ -75,7 +77,15 @@ export function useRestoreDocument() {
export function useUploadDocuments({ organizationId }: { organizationId: string }) {
const uploadDocuments = async ({ files }: { files: File[] }) => {
for (const file of files) {
await uploadDocument({ file, organizationId });
const [, error] = await safely(uploadDocument({ file, organizationId }));

if (isHttpErrorWithCode({ error, code: 'document.already_exists' })) {
createToast({
type: 'error',
message: 'Document already exists',
description: `The document ${file.name} already exists, it has not been uploaded.`,
});
}
}

queryClient.invalidateQueries({
Expand Down
2 changes: 2 additions & 0 deletions apps/papra-server/migrations/0006_documents-hash.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `documents` ADD `original_sha256_hash` text NOT NULL;--> statement-breakpoint
CREATE UNIQUE INDEX `documents_organization_id_original_sha256_hash_unique` ON `documents` (`organization_id`,`original_sha256_hash`);
Loading

0 comments on commit f78d42c

Please sign in to comment.