-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuploads.ts
More file actions
192 lines (178 loc) · 5 KB
/
Copy pathuploads.ts
File metadata and controls
192 lines (178 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import { nanoid } from 'nanoid';
import { and, eq } from 'drizzle-orm';
import { db } from '../db/client.js';
import { application, applicationFile } from '../db/schema.js';
import {
signUpload,
deleteObject,
type UploadTicket,
} from '../integrations/storage/index.js';
const ALLOWED_CONTENT_TYPES = new Set([
'application/pdf',
'image/png',
'image/jpeg',
]);
const MAX_SIZE = 25 * 1024 * 1024; // 25 MB
function nowSeconds(): number {
return Math.floor(Date.now() / 1000);
}
export type SignInput = {
userId: string;
applicationId: string;
kind: 'transcript' | 'aid_doc';
filename: string;
contentType: string;
size: number;
};
export type SignError = 'unsupported_type' | 'too_large' | 'app_locked';
// Guardians can still upload aid docs while the application is
// `awaiting_guardian`; the applicant can only touch files while in `draft`.
// The caller (route handler) is responsible for auth; this function only
// enforces that the application isn't past `awaiting_guardian`.
function isEditableFor(status: string, kind: 'transcript' | 'aid_doc'): boolean {
if (status === 'draft') return true;
if (status === 'awaiting_guardian' && kind === 'aid_doc') return true;
return false;
}
export async function requestSignedUpload(
input: SignInput,
): Promise<
{ ok: true; ticket: UploadTicket } | { ok: false; reason: SignError }
> {
if (!ALLOWED_CONTENT_TYPES.has(input.contentType)) {
return { ok: false, reason: 'unsupported_type' };
}
if (input.size <= 0 || input.size > MAX_SIZE) {
return { ok: false, reason: 'too_large' };
}
const app = db
.select({ status: application.status })
.from(application)
.where(eq(application.id, input.applicationId))
.get();
if (!app || !isEditableFor(app.status, input.kind)) {
return { ok: false, reason: 'app_locked' };
}
const ticket = await signUpload({
userId: input.userId,
kind: input.kind,
contentType: input.contentType,
size: input.size,
});
return { ok: true, ticket };
}
export type RegisterInput = {
applicationId: string;
kind: 'transcript' | 'aid_doc';
storageKey: string;
filename: string;
contentType: string;
size: number;
uploadedByUserId?: string | undefined;
};
export type ApplicationFileRow = {
id: string;
kind: 'transcript' | 'aid_doc';
filename: string;
contentType: string;
size: number;
uploadedAt: number;
};
export async function registerFile(input: RegisterInput): Promise<ApplicationFileRow> {
// Transcript is single-file (replace on new upload). Aid docs are multi-file.
if (input.kind === 'transcript') {
const existing = db
.select()
.from(applicationFile)
.where(
and(
eq(applicationFile.applicationId, input.applicationId),
eq(applicationFile.kind, 'transcript'),
),
)
.all();
for (const row of existing) {
await deleteObject(row.storageKey);
db.delete(applicationFile).where(eq(applicationFile.id, row.id)).run();
}
}
const id = nanoid();
const now = nowSeconds();
db.insert(applicationFile)
.values({
id,
applicationId: input.applicationId,
kind: input.kind,
storageKey: input.storageKey,
filename: input.filename,
contentType: input.contentType,
size: input.size,
uploadedByUserId: input.uploadedByUserId ?? null,
uploadedAt: now,
})
.run();
db.update(application)
.set({ updatedAt: now })
.where(eq(application.id, input.applicationId))
.run();
return {
id,
kind: input.kind,
filename: input.filename,
contentType: input.contentType,
size: input.size,
uploadedAt: now,
};
}
export function listFiles(applicationId: string): ApplicationFileRow[] {
return db
.select({
id: applicationFile.id,
kind: applicationFile.kind,
filename: applicationFile.filename,
contentType: applicationFile.contentType,
size: applicationFile.size,
uploadedAt: applicationFile.uploadedAt,
})
.from(applicationFile)
.where(eq(applicationFile.applicationId, applicationId))
.all();
}
export async function deleteFile(
applicationId: string,
fileId: string,
): Promise<boolean> {
const row = db
.select()
.from(applicationFile)
.where(
and(eq(applicationFile.id, fileId), eq(applicationFile.applicationId, applicationId)),
)
.get();
if (!row) return false;
await deleteObject(row.storageKey);
db.delete(applicationFile).where(eq(applicationFile.id, fileId)).run();
return true;
}
export function getFile(
applicationId: string,
fileId: string,
): (ApplicationFileRow & { storageKey: string }) | null {
const row = db
.select()
.from(applicationFile)
.where(
and(eq(applicationFile.id, fileId), eq(applicationFile.applicationId, applicationId)),
)
.get();
if (!row) return null;
return {
id: row.id,
kind: row.kind,
filename: row.filename,
contentType: row.contentType,
size: row.size,
uploadedAt: row.uploadedAt,
storageKey: row.storageKey,
};
}