mirror of
https://github.com/hanzoai/sign.git
synced 2026-10-21 01:21:51 +00:00
Three unrelated defects, one image. The profile toast fired only after a session refresh. That refresh is a second read: it can stall, and when it answers unauthenticated it clears the session, which throws in useSession and takes down the subtree holding the toaster. A save that had already persisted could therefore go unconfirmed. The save is confirmed on the save now and the refresh follows it, so the confirmation depends only on the thing it reports. Document bytes are routed by NEXT_PUBLIC_UPLOAD_TRANSPORT. Three places read it with three slightly different rules, and the two that chose a store fell through to the database for any value they did not recognise -- so a transport named under a variable nothing reads stored every PDF inline while the configuration read as object storage, with nothing logged either way. One function resolves it now: unset means the database, and an unrecognised value throws rather than quietly picking a store. Reads still dispatch on each row's own type, so rows written under either transport keep working. entry.client and entry.server built different root trees -- StrictMode on the client only, and a second child beside the router. useId numbers a component by its position, and a parent's child count is part of that position, so every id below the root diverged and hydration failed on the first component to ask for one. Measured on the documents page: the header's menu trigger came back radix-:R26j5: from the server and radix-:R8qcl: from the client. The trees are identical now and all seven server-rendered ids survive hydration unchanged. Also: .gitignore still named the license artifact by its pre-rebrand filename, so the file the app actually writes was landing in git status untracked.
110 lines
2.9 KiB
TypeScript
110 lines
2.9 KiB
TypeScript
import { PDF } from '@libpdf/core';
|
|
import { DocumentDataType } from '@prisma/client';
|
|
import { base64 } from '@scure/base';
|
|
import { match } from 'ts-pattern';
|
|
|
|
import { AppError } from '../../errors/app-error';
|
|
import { createDocumentData } from '../../server-only/document-data/create-document-data';
|
|
import { normalizePdf } from '../../server-only/pdf/normalize-pdf';
|
|
import { uploadS3File } from './server-actions';
|
|
import { uploadTransport } from './transport';
|
|
|
|
type File = {
|
|
name: string;
|
|
type: string;
|
|
arrayBuffer: () => Promise<ArrayBuffer>;
|
|
};
|
|
|
|
/**
|
|
* Uploads a document file to the appropriate storage location and creates
|
|
* a document data record.
|
|
*/
|
|
export const putPdfFileServerSide = async (file: File, initialData?: string) => {
|
|
const isEncryptedDocumentsAllowed = false; // Was feature flag.
|
|
|
|
const arrayBuffer = await file.arrayBuffer();
|
|
|
|
const pdf = await PDF.load(new Uint8Array(arrayBuffer)).catch((e) => {
|
|
console.error(`PDF upload parse error: ${e.message}`);
|
|
|
|
throw new AppError('INVALID_DOCUMENT_FILE');
|
|
});
|
|
|
|
if (!isEncryptedDocumentsAllowed && pdf.isEncrypted) {
|
|
throw new AppError('INVALID_DOCUMENT_FILE');
|
|
}
|
|
|
|
if (!file.name.endsWith('.pdf')) {
|
|
file.name = `${file.name}.pdf`;
|
|
}
|
|
|
|
const { type, data } = await putFileServerSide(file);
|
|
|
|
return await createDocumentData({ type, data, initialData });
|
|
};
|
|
|
|
/**
|
|
* Uploads a pdf file and normalizes it.
|
|
*/
|
|
export const putNormalizedPdfFileServerSide = async (
|
|
file: File,
|
|
options: { flattenForm?: boolean } = {},
|
|
) => {
|
|
const buffer = Buffer.from(await file.arrayBuffer());
|
|
|
|
const normalized = await normalizePdf(buffer, options);
|
|
|
|
const fileName = file.name.endsWith('.pdf') ? file.name : `${file.name}.pdf`;
|
|
|
|
const documentData = await putFileServerSide({
|
|
name: fileName,
|
|
type: 'application/pdf',
|
|
arrayBuffer: async () => Promise.resolve(normalized as unknown as ArrayBuffer),
|
|
});
|
|
|
|
return await createDocumentData({
|
|
type: documentData.type,
|
|
data: documentData.data,
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Uploads a file to the appropriate storage location.
|
|
*/
|
|
export const putFileServerSide = async (file: File) => {
|
|
return await match(uploadTransport())
|
|
.with('s3', async () => putFileInS3(file))
|
|
.with('database', async () => putFileInDatabase(file))
|
|
.exhaustive();
|
|
};
|
|
|
|
const putFileInDatabase = async (file: File) => {
|
|
const contents = await file.arrayBuffer();
|
|
|
|
const binaryData = new Uint8Array(contents);
|
|
|
|
const asciiData = base64.encode(binaryData);
|
|
|
|
return {
|
|
type: DocumentDataType.BYTES_64,
|
|
data: asciiData,
|
|
};
|
|
};
|
|
|
|
const putFileInS3 = async (file: File) => {
|
|
const buffer = await file.arrayBuffer();
|
|
|
|
const blob = new Blob([buffer], { type: file.type });
|
|
|
|
const newFile = new File([blob], file.name, {
|
|
type: file.type,
|
|
});
|
|
|
|
const { key } = await uploadS3File(newFile);
|
|
|
|
return {
|
|
type: DocumentDataType.S3_PATH,
|
|
data: key,
|
|
};
|
|
};
|