|
1 | 1 | // Copyright Metatype OÜ, licensed under the Elastic License 2.0.
|
2 | 2 | // SPDX-License-Identifier: Elastic-2.0
|
3 | 3 |
|
| 4 | +import { UploadUrlMeta } from "../engine/query_engine.ts"; |
| 5 | +import { QueryEngine } from "../engine/query_engine.ts"; |
| 6 | + |
| 7 | +interface TypegraphFile { |
| 8 | + name: string; |
| 9 | + fileHash: string; |
| 10 | + fileSizeInBytes: number; |
| 11 | +} |
| 12 | + |
4 | 13 | function createUploadPath(origin: string, typegraphName: string) {
|
5 | 14 | const rand_path = crypto.randomUUID();
|
6 |
| - return `${origin}/${typegraphName}/files/${rand_path}`; |
| 15 | + return `${origin}/upload-files/${typegraphName}/files/${rand_path}`; |
7 | 16 | }
|
8 | 17 |
|
9 |
| -export function handleUploadUrl(request: Request, typegraphName: string) { |
| 18 | +export async function handleUploadUrl(request: Request, engine: QueryEngine) { |
10 | 19 | const url = new URL(request.url);
|
11 | 20 | const origin = url.origin;
|
| 21 | + const { name, fileHash, fileSizeInBytes }: TypegraphFile = await request |
| 22 | + .json(); |
12 | 23 |
|
13 |
| - const uploadUrl = createUploadPath(origin, typegraphName); |
14 |
| - return new Response(uploadUrl); |
15 |
| -} |
| 24 | + const uploadUrlMeta: UploadUrlMeta = { |
| 25 | + fileName: name, |
| 26 | + fileHash: fileHash, |
| 27 | + fileSizeInBytes: fileSizeInBytes, |
| 28 | + urlUsed: false, |
| 29 | + }; |
16 | 30 |
|
17 |
| -interface TypegraphFile { |
18 |
| - name: string; |
19 |
| - fileHash: string; |
20 |
| - fileSizeInBytes: number; |
21 |
| - file: string; |
| 31 | + const uploadUrl = createUploadPath(origin, engine.name); |
| 32 | + |
| 33 | + engine.fileUploadUrlCache.set(uploadUrl, uploadUrlMeta); |
| 34 | + |
| 35 | + return new Response(JSON.stringify({ uploadUrl: [uploadUrl] })); |
22 | 36 | }
|
23 | 37 |
|
24 | 38 | export async function handleFileUpload(
|
25 | 39 | request: Request,
|
26 |
| - typegraphName: string, |
| 40 | + engine: QueryEngine, |
27 | 41 | ) {
|
28 | 42 | const url = new URL(request.url);
|
29 |
| - if (request.method !== "POST") { |
| 43 | + if (request.method !== "PUT") { |
30 | 44 | throw new Error(
|
31 | 45 | `${url.pathname} does not support ${request.method} method`,
|
32 | 46 | );
|
33 | 47 | }
|
34 | 48 |
|
35 |
| - const fileStorageDir = `metatype_artifacts/${typegraphName}/files`; |
36 |
| - const { name, fileHash, fileSizeInBytes, file }: TypegraphFile = await request |
37 |
| - .json(); |
| 49 | + const uploadMeta = engine.fileUploadUrlCache.get(url.toString()); |
| 50 | + |
| 51 | + if (!uploadMeta) { |
| 52 | + throw new Error(`Endpoint ${url.toString()} does not exist`); |
| 53 | + } |
38 | 54 |
|
39 |
| - const fileData = Uint8Array.from(atob(file), (c) => c.charCodeAt(0)); |
| 55 | + const { fileName, fileHash, fileSizeInBytes, urlUsed }: UploadUrlMeta = |
| 56 | + uploadMeta!; |
40 | 57 |
|
41 |
| - if (fileData.length !== fileSizeInBytes) { |
42 |
| - throw new Error(); |
| 58 | + if (urlUsed) { |
| 59 | + throw new Error(`Endpoint ${url.toString()} is disabled`); |
43 | 60 | }
|
44 | 61 |
|
| 62 | + const reader = request.body?.getReader()!; |
| 63 | + |
| 64 | + let fileData = new Uint8Array(); |
| 65 | + let bytesRead = 0; |
| 66 | + while (true) { |
| 67 | + const { done, value } = await reader.read(); |
| 68 | + if (done) { |
| 69 | + break; |
| 70 | + } |
| 71 | + if (value) { |
| 72 | + bytesRead += value.length; |
| 73 | + const temp = new Uint8Array(fileData.length + value.length); |
| 74 | + temp.set(fileData); |
| 75 | + temp.set(value, fileData.length); |
| 76 | + fileData = temp; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + if (bytesRead !== fileSizeInBytes) { |
| 81 | + throw new Error("File size does not match"); |
| 82 | + } |
| 83 | + |
| 84 | + const fileStorageDir = `metatype_artifacts/${engine.name}/files`; |
45 | 85 | await Deno.mkdir(fileStorageDir, { recursive: true });
|
46 |
| - const filePath = `${fileStorageDir}/${name}.${fileHash}`; |
| 86 | + const filePath = `${fileStorageDir}/${fileName}.${fileHash}`; |
47 | 87 | await Deno.writeFile(filePath, fileData);
|
48 | 88 |
|
| 89 | + // mark as the url used once the request completes. |
| 90 | + uploadMeta.urlUsed = true; |
| 91 | + engine.fileUploadUrlCache.set(url.toString(), uploadMeta); |
| 92 | + |
49 | 93 | return new Response();
|
50 | 94 | }
|
0 commit comments