Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 1db2dcd

Browse files
Blckbrry-PiNathanFlurry
authored andcommitted
feat: Create uploads module
1 parent 140f6aa commit 1db2dcd

File tree

21 files changed

+1523
-2
lines changed

21 files changed

+1523
-2
lines changed

modules/tokens/scripts/extend.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { ScriptContext } from "../module.gen.ts";
2-
import { TokenWithSecret } from "../utils/types.ts";
3-
import { tokenFromRow } from "../utils/types.ts";
2+
import { TokenWithSecret, tokenFromRow } from "../utils/types.ts";
43

54
export interface Request {
65
token: string;

modules/uploads/config.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { UploadSize } from "./utils/data_size.ts";
2+
3+
export interface S3UserConfig {
4+
bucketName: string;
5+
region: string;
6+
endpoint: string;
7+
8+
accessKeyId?: string;
9+
secretAccessKey?: string;
10+
};
11+
12+
13+
export interface Config {
14+
maxUploadSize?: UploadSize;
15+
maxMultipartUploadSize?: UploadSize;
16+
maxFilesPerUpload?: number;
17+
defaultMultipartChunkSize?: UploadSize;
18+
19+
s3?: S3UserConfig;
20+
}
21+
22+
export const DEFAULT_MAX_FILES_PER_UPLOAD = 10;
23+
24+
export const DEFAULT_MAX_UPLOAD_SIZE: UploadSize = "30mib";
25+
export const DEFAULT_MAX_MULTIPART_UPLOAD_SIZE: UploadSize = "10gib";
26+
export const DEFAULT_MULTIPART_CHUNK_SIZE: UploadSize = "10mib";
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
-- CreateTable
2+
CREATE TABLE "Upload" (
3+
"id" UUID NOT NULL,
4+
"userId" UUID,
5+
"bucket" TEXT NOT NULL,
6+
"contentLength" BIGINT NOT NULL,
7+
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
8+
"updatedAt" TIMESTAMP(3) NOT NULL,
9+
"completedAt" TIMESTAMP(3),
10+
"deletedAt" TIMESTAMP(3),
11+
12+
CONSTRAINT "Upload_pkey" PRIMARY KEY ("id")
13+
);
14+
15+
-- CreateTable
16+
CREATE TABLE "Files" (
17+
"uploadId" UUID NOT NULL,
18+
"multipartUploadId" TEXT,
19+
"path" TEXT NOT NULL,
20+
"mime" TEXT,
21+
"contentLength" BIGINT NOT NULL,
22+
23+
CONSTRAINT "Files_pkey" PRIMARY KEY ("uploadId","path")
24+
);
25+
26+
-- AddForeignKey
27+
ALTER TABLE "Files" ADD CONSTRAINT "Files_uploadId_fkey" FOREIGN KEY ("uploadId") REFERENCES "Upload"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- AlterTable
2+
ALTER TABLE "Upload"
3+
RENAME COLUMN "userId" TO "tag";
4+
5+
ALTER TABLE "Upload"
6+
ALTER COLUMN "tag" TYPE TEXT
7+
USING CASE
8+
WHEN "tag" IS NULL THEN NULL
9+
ELSE "tag"::TEXT
10+
END;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Please do not edit this file manually
2+
# It should be added in your version-control system (i.e. Git)
3+
provider = "postgresql"

modules/uploads/db/schema.prisma

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Do not modify this `datasource` block
2+
datasource db {
3+
provider = "postgresql"
4+
url = env("DATABASE_URL")
5+
}
6+
7+
model Upload {
8+
id String @id @default(uuid()) @db.Uuid
9+
tag String?
10+
11+
bucket String
12+
contentLength BigInt
13+
14+
createdAt DateTime @default(now())
15+
updatedAt DateTime @updatedAt
16+
completedAt DateTime?
17+
deletedAt DateTime?
18+
19+
files Files[] @relation("Files")
20+
}
21+
22+
model Files {
23+
uploadId String @db.Uuid
24+
upload Upload @relation("Files", fields: [uploadId], references: [id])
25+
26+
multipartUploadId String?
27+
28+
path String
29+
mime String?
30+
contentLength BigInt
31+
32+
@@id([uploadId, path])
33+
}

modules/uploads/module.json

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"scripts": {
3+
"prepare": {
4+
"name": "Prepare Upload",
5+
"description": "Prepare an upload batch for data transfer"
6+
},
7+
"complete": {
8+
"name": "Complete Upload",
9+
"description": "Alert the module that the upload has been completed"
10+
},
11+
"get": {
12+
"name": "Get Upload Metadata",
13+
"description": "Get the metadata (including contained files) for specified upload IDs"
14+
},
15+
"get_public_file_urls": {
16+
"name": "Get File Link",
17+
"description": "Get presigned download links for each of the specified files"
18+
},
19+
"list_by_tags": {
20+
"name": "List Uploads by Tags",
21+
"description": "Get a list of upload IDs associated with the specified tags"
22+
},
23+
"delete": {
24+
"name": "Delete Upload",
25+
"description": "Removes the upload and deletes the files from the bucket"
26+
}
27+
},
28+
"errors": {
29+
"no_files": {
30+
"name": "No Files Provided",
31+
"description": "An upload must have at least 1 file"
32+
},
33+
"too_many_files": {
34+
"name": "Too Many Files Provided",
35+
"description": "There is a limit to how many files can be put into a single upload (see config)"
36+
},
37+
"duplicate_paths": {
38+
"name": "Duplicate Paths Provided",
39+
"description": "An upload cannot contain 2 files with the same paths (see `cause` for offending paths)"
40+
},
41+
"size_limit_exceeded": {
42+
"name": "Combined Size Limit Exceeded",
43+
"description": "There is a maximum total size per upload (see config)"
44+
},
45+
"upload_not_found": {
46+
"name": "Upload Not Found",
47+
"description": "The provided upload ID didn't match any known existing uploads"
48+
},
49+
"upload_already_completed": {
50+
"name": "Upload Already completed",
51+
"description": "\\`complete\\` was already called on this upload"
52+
},
53+
"s3_not_configured": {
54+
"name": "S3 Not Configured",
55+
"description": "The S3 bucket is not configured (missing env variables)"
56+
},
57+
"too_many_chunks": {
58+
"name": "Possibility Of Too Many Chunks",
59+
"description": "AWS S3 has a limit on the number of parts that can be uploaded in a\nmultipart upload. This limit is 10,000 parts. If the number of chunks\nrequired to upload the maximum multipart upload size exceeds this limit,\nany operation will preemptively throw this error.\n"
60+
},
61+
"multipart_upload_completion_fail": {
62+
"name": "Multipart Upload Completion Failure",
63+
"description": "The multipart upload failed to complete (see `cause` for more information)"
64+
}
65+
},
66+
"dependencies": {}
67+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { RuntimeError, ScriptContext } from "../module.gen.ts";
2+
import {
3+
completeMultipartUpload,
4+
getMultipartUploadParts,
5+
keyExists,
6+
} from "../utils/bucket.ts";
7+
import { getConfig } from "../utils/config_defaults.ts";
8+
import { getKey, prismaToOutputWithFiles, Upload } from "../utils/types.ts";
9+
10+
export interface Request {
11+
uploadId: string;
12+
}
13+
14+
export interface Response {
15+
upload: Upload;
16+
}
17+
18+
export async function run(
19+
ctx: ScriptContext,
20+
req: Request,
21+
): Promise<Response> {
22+
const config = getConfig(ctx.userConfig);
23+
24+
const newUpload = await ctx.db.$transaction(async (db) => {
25+
// Find the upload by ID
26+
const upload = await db.upload.findFirst({
27+
where: {
28+
id: req.uploadId,
29+
},
30+
select: {
31+
id: true,
32+
tag: true,
33+
bucket: true,
34+
contentLength: true,
35+
files: true,
36+
createdAt: true,
37+
updatedAt: true,
38+
completedAt: true,
39+
},
40+
});
41+
42+
// Error if the upload wasn't prepared
43+
if (!upload) {
44+
throw new RuntimeError(
45+
"upload_not_found",
46+
{
47+
meta: { uploadId: req.uploadId },
48+
},
49+
);
50+
}
51+
52+
// Error if `complete` was already called with this ID
53+
if (upload.completedAt !== null) {
54+
throw new RuntimeError(
55+
"upload_already_completed",
56+
{
57+
meta: { uploadId: req.uploadId },
58+
},
59+
);
60+
}
61+
62+
// Check with S3 to see if the files were uploaded
63+
const fileExistencePromises = upload.files.map(
64+
async (file) => {
65+
// If the file was uploaded in parts, complete the multipart upload
66+
if (file.multipartUploadId) {
67+
try {
68+
const parts = await getMultipartUploadParts(
69+
config.s3,
70+
getKey(upload.id, file.path),
71+
file.multipartUploadId,
72+
);
73+
if (parts.length === 0) return false;
74+
75+
await completeMultipartUpload(
76+
config.s3,
77+
getKey(upload.id, file.path),
78+
file.multipartUploadId,
79+
parts,
80+
);
81+
} catch (e) {
82+
throw new RuntimeError(
83+
"multipart_upload_completion_fail",
84+
{ cause: e },
85+
);
86+
}
87+
88+
return true;
89+
} else {
90+
// Check if the file exists
91+
return await keyExists(config.s3, getKey(upload.id, file.path));
92+
}
93+
},
94+
);
95+
const fileExistence = await Promise.all(fileExistencePromises);
96+
const filesAllExist = fileExistence.every(Boolean);
97+
if (!filesAllExist) {
98+
const missingFiles = upload.files.filter((_, i) => !fileExistence[i]);
99+
throw new RuntimeError(
100+
"files_not_uploaded",
101+
{
102+
meta: {
103+
uploadId: req.uploadId,
104+
missingFiles: missingFiles.map((file) => file.path),
105+
},
106+
},
107+
);
108+
}
109+
110+
// Update the upload to mark it as completed
111+
const completedUpload = await db.upload.update({
112+
where: {
113+
id: req.uploadId,
114+
},
115+
data: {
116+
completedAt: new Date(),
117+
},
118+
select: {
119+
id: true,
120+
tag: true,
121+
bucket: true,
122+
contentLength: true,
123+
files: true,
124+
createdAt: true,
125+
updatedAt: true,
126+
completedAt: true,
127+
},
128+
});
129+
130+
return completedUpload;
131+
});
132+
133+
return {
134+
upload: prismaToOutputWithFiles(newUpload),
135+
};
136+
}

0 commit comments

Comments
 (0)