Skip to content

Commit 2a8137e

Browse files
Give every account a Fast-Upload folder for Quick-Upload drops
Each user now owns a 'Fast-Upload' root folder. Code uploads with no explicit target land there (instead of the account root), so there is always one predictable place to find them. The folder is created eagerly on account creation (admin route + seed) and lazily ensured at upload time for older accounts or if it was deleted. Adds an integration test (39 tests pass).
1 parent 6dad794 commit 2a8137e

6 files changed

Lines changed: 95 additions & 2 deletions

File tree

‎apps/api/prisma/seed.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@ async function main(): Promise<void> {
2828
}
2929

3030
const quota = process.env.DEFAULT_USER_QUOTA_BYTES;
31-
await prisma.user.create({
31+
const admin = await prisma.user.create({
3232
data: {
3333
email,
3434
passwordHash: await argon2.hash(password, { type: argon2.argon2id }),
3535
role: 'ADMIN',
3636
quotaBytes: quota ? BigInt(quota) : null,
3737
},
3838
});
39+
// Every account owns a Fast-Upload folder where Quick-Upload code drops land.
40+
await prisma.folder.create({
41+
data: { ownerId: admin.id, parentId: null, name: 'Fast-Upload', isZeroKnowledge: false },
42+
});
3943
console.log(`Created admin user: ${email}`);
4044
}
4145

‎apps/api/src/routes/admin.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { parseOr400 } from '../lib/validate.js';
1111
import { hashPassword } from '../services/password.js';
1212
import { toPublicQuickCode, toPublicUser } from '../lib/serialize.js';
1313
import { getGlobalCapBytes, getGlobalUsedBytes } from '../services/quota.js';
14+
import { ensureFastUploadFolder } from '../services/fastupload.js';
1415
import { runMaintenance } from '../services/maintenance.js';
1516
import { audit } from '../services/audit.js';
1617

@@ -59,6 +60,8 @@ export const adminRoutes: FastifyPluginAsync = async (app) => {
5960
quotaBytes,
6061
},
6162
});
63+
// Give the new account its Fast-Upload folder up front so it's there from day one.
64+
await ensureFastUploadFolder(user.id);
6265
await audit(req, 'admin.user.create', { target: user.id });
6366
return reply.code(201).send({ user: toPublicUser(user) });
6467
});

‎apps/api/src/routes/quick.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
} from '../services/ingest.js';
2121
import { adjustUsage, remainingAllowance } from '../services/quota.js';
2222
import { checkLock, clearFailures, recordFailure } from '../services/throttle.js';
23+
import { ensureFastUploadFolder } from '../services/fastupload.js';
2324
import { audit } from '../services/audit.js';
2425

2526
function isExpired(expiresAt: Date | null): boolean {
@@ -94,6 +95,9 @@ export const quickRoutes: FastifyPluginAsync = async (app) => {
9495
}
9596

9697
const ownerId = entry.createdById;
98+
// No explicit target → drop into the owner's Fast-Upload folder, so every code upload
99+
// lands somewhere predictable instead of the account root.
100+
const targetFolderId = entry.targetFolderId ?? (await ensureFastUploadFolder(ownerId));
97101
const ownerAllowance = await remainingAllowance(ownerId);
98102
const maxBytes = Math.min(
99103
ownerAllowance,
@@ -107,7 +111,7 @@ export const quickRoutes: FastifyPluginAsync = async (app) => {
107111
const file = await prisma.fileObject.create({
108112
data: {
109113
ownerId,
110-
folderId: entry.targetFolderId,
114+
folderId: targetFolderId,
111115
name: filePart.filename,
112116
sizeBytes: BigInt(result.sizeBytes),
113117
mimeType: filePart.mimetype,
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Every account owns a "Fast-Upload" folder — a normal (server-encrypted) root folder that
3+
* collects everything dropped through a Quick-Upload code. Code uploads with no explicit
4+
* target land here, so the owner always has a single, predictable place to find them.
5+
*
6+
* The folder is created eagerly on account creation, but `ensureFastUploadFolder` is also a
7+
* lazy safety net: it (re)creates the folder for older accounts, or if the owner deleted it.
8+
*/
9+
import { FAST_UPLOAD_FOLDER_NAME } from '@opencoperlock/shared';
10+
import { prisma } from '../db.js';
11+
12+
/** Return the id of the user's Fast-Upload folder, creating it at the root if absent. */
13+
export async function ensureFastUploadFolder(userId: string): Promise<string> {
14+
const existing = await prisma.folder.findFirst({
15+
where: {
16+
ownerId: userId,
17+
parentId: null,
18+
name: FAST_UPLOAD_FOLDER_NAME,
19+
isZeroKnowledge: false,
20+
},
21+
select: { id: true },
22+
});
23+
if (existing) return existing.id;
24+
25+
const folder = await prisma.folder.create({
26+
data: {
27+
ownerId: userId,
28+
parentId: null,
29+
name: FAST_UPLOAD_FOLDER_NAME,
30+
isZeroKnowledge: false,
31+
},
32+
select: { id: true },
33+
});
34+
return folder.id;
35+
}

‎apps/api/test/integration.test.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import FormData from 'form-data';
12
import type { FastifyInstance } from 'fastify';
23
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
34
import { prisma } from '../src/db.js';
@@ -289,6 +290,45 @@ runIf('API integration', () => {
289290
});
290291
expect(bad.statusCode).toBe(400);
291292
});
293+
294+
it('drops code uploads with no explicit target into the owner Fast-Upload folder', async () => {
295+
const admin = await createUser({ email: 'fu@test.local', password: 'correct-horse-battery', role: 'ADMIN' });
296+
const auth = await login(app, 'fu@test.local', 'correct-horse-battery');
297+
298+
// A code with no targetFolderId.
299+
const created = await app.inject({
300+
method: 'POST',
301+
url: '/admin/quick-codes',
302+
headers: authHeaders(auth),
303+
payload: {},
304+
});
305+
expect(created.statusCode).toBe(201);
306+
const code = created.json().code.code as string;
307+
308+
// Guest upload (no auth / no CSRF on the public quick route).
309+
const form = new FormData();
310+
form.append('file', Buffer.from('dropped by code'), { filename: 'drop.txt', contentType: 'text/plain' });
311+
const up = await app.inject({
312+
method: 'POST',
313+
url: `/quick/${code}`,
314+
payload: form.getBuffer(),
315+
headers: form.getHeaders(),
316+
});
317+
expect(up.statusCode).toBe(201);
318+
319+
// The file landed in the owner's Fast-Upload folder.
320+
const fastFolder = await prisma.folder.findFirst({
321+
where: { ownerId: admin.id, parentId: null, name: 'Fast-Upload' },
322+
});
323+
expect(fastFolder).not.toBeNull();
324+
const listed = await app.inject({
325+
method: 'GET',
326+
url: `/files?folderId=${fastFolder!.id}`,
327+
headers: authHeaders(auth),
328+
});
329+
expect(listed.statusCode).toBe(200);
330+
expect(listed.json().files.map((f: { name: string }) => f.name)).toContain('drop.txt');
331+
});
292332
});
293333

294334
describe('share links', () => {

‎packages/shared/src/constants.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ export const SESSION_COOKIE = 'ocl_session';
3737
/** Header carrying the CSRF token on mutating requests. */
3838
export const CSRF_HEADER = 'x-ocl-csrf';
3939

40+
/**
41+
* Well-known name of the per-user folder that collects everything dropped through a
42+
* Quick-Upload code. Every account gets one, and code uploads with no explicit target
43+
* land here so the owner always knows where to look.
44+
*/
45+
export const FAST_UPLOAD_FOLDER_NAME = 'Fast-Upload';
46+
4047
/** Crypto parameters for server-side envelope encryption. */
4148
export const CRYPTO = {
4249
/** AES-256-GCM. */

0 commit comments

Comments
 (0)