MCPcopy Create free account
hub / github.com/bewcloud/bewcloud / post

Function post

pages/api/files/upload-chunk.ts:44–171  ·  view source on GitHub ↗
({ request, user }: RequestHandlerParams)

Source from the content-addressed store, hash-verified

42}
43
44async function post({ request, user }: RequestHandlerParams) {
45 if (
46 !(await AppConfig.isAppEnabled('files')) && !(await AppConfig.isAppEnabled('photos')) &&
47 !(await AppConfig.isAppEnabled('notes'))
48 ) {
49 return new Response('Forbidden', { status: 403 });
50 }
51
52 const requestBody = await request.clone().formData();
53
54 const uploadId = requestBody.get('upload_id') as string;
55 const chunkIndexStr = requestBody.get('chunk_index') as string;
56 const totalChunksStr = requestBody.get('total_chunks') as string;
57 const pathInView = requestBody.get('path_in_view') as string;
58 const parentPath = requestBody.get('parent_path') as string;
59 const name = requestBody.get('name') as string;
60 const chunk = requestBody.get('chunk') as File | null;
61
62 const chunkIndex = parseInt(chunkIndexStr, 10);
63 const totalChunks = parseInt(totalChunksStr, 10);
64
65 if (
66 !uploadId ||
67 !/^[a-zA-Z0-9-]+$/.test(uploadId) ||
68 isNaN(chunkIndex) || chunkIndex < 0 ||
69 isNaN(totalChunks) || totalChunks < 1 ||
70 chunkIndex >= totalChunks ||
71 !parentPath ||
72 !pathInView ||
73 !name?.trim() ||
74 !chunk ||
75 !parentPath.startsWith('/') ||
76 parentPath.includes('../') ||
77 !pathInView.startsWith('/') ||
78 pathInView.includes('../')
79 ) {
80 return new Response('Bad Request', { status: 400 });
81 }
82
83 try {
84 await ensureUserPathIsValidAndSecurelyAccessible(user!.id, join(parentPath, name.trim()));
85 } catch {
86 return new Response('Bad Request', { status: 400 });
87 }
88
89 const filesRootPath = await AppConfig.getFilesRootPath();
90 const userUploadDir = join(filesRootPath, user!.id, '.chunk-uploads');
91 const uploadDir = join(userUploadDir, uploadId);
92
93 // On the first chunk of a new upload, evict any stale sessions from this user.
94 if (chunkIndex === 0) {
95 await cleanStaleUploads(userUploadDir);
96 }
97
98 try {
99 await Deno.mkdir(uploadDir, { recursive: true });
100
101 const chunkData = await chunk.arrayBuffer();

Callers

nothing calls this directly

Calls 10

cleanStaleUploadsFunction · 0.85
writeAllFunction · 0.85
isAppEnabledMethod · 0.80
cloneMethod · 0.80
getFilesRootPathMethod · 0.80
removeMethod · 0.80
closeMethod · 0.80
getMethod · 0.45
listMethod · 0.45

Tested by

no test coverage detected