feat: universal upload

Implementation of a universal upload allowing for multiple storage backends
starting with `database` and `s3`.

Allows clients to put and retrieve files from either client or server using
a blend of client and server actions.
This commit is contained in:
Mythie
2023-09-14 12:46:36 +10:00
parent ed4cbe9fa6
commit 9014f01276
42 changed files with 2372 additions and 305 deletions

View File

@ -0,0 +1,54 @@
import { base64 } from '@scure/base';
import { match } from 'ts-pattern';
import { DocumentDataType } from '@documenso/prisma/client';
import { getAbsolutePresignPostUrl } from './server-actions';
export type UpdateFileOptions = {
type: DocumentDataType;
oldData: string;
newData: string;
};
export const updateFile = async ({ type, oldData, newData }: UpdateFileOptions) => {
return await match(type)
.with(DocumentDataType.BYTES, () => updateFileWithBytes(newData))
.with(DocumentDataType.BYTES_64, () => updateFileWithBytes64(newData))
.with(DocumentDataType.S3_PATH, async () => updateFileWithS3(oldData, newData))
.exhaustive();
};
const updateFileWithBytes = (data: string) => {
return {
type: DocumentDataType.BYTES,
data,
};
};
const updateFileWithBytes64 = (data: string) => {
const encoder = new TextEncoder();
const binaryData = encoder.encode(data);
const asciiData = base64.encode(binaryData);
return {
type: DocumentDataType.BYTES_64,
data: asciiData,
};
};
const updateFileWithS3 = async (key: string, data: string) => {
const { url } = await getAbsolutePresignPostUrl(key);
await fetch(url, {
method: 'PUT',
body: data,
});
return {
type: DocumentDataType.S3_PATH,
data: key,
};
};