/ src / storage / src / onupload.ts
onupload.ts
 1  #!/bin/bun
 2  
 3  const input = (await Bun.stdin.json()) as {
 4    Type: "post-finish";
 5    Event: {
 6      Upload: {
 7        ID: string;
 8        Size: number;
 9        SizeIsDeferred: boolean;
10        Offset: number;
11        MetaData: {
12          filename: string;
13          filetype: "image/png";
14        };
15        IsPartial: boolean;
16        IsFinal: boolean;
17        PartialUploads: null;
18        Storage: {
19          InfoPath: string;
20          Path: string;
21          Type: "filestore";
22        };
23      };
24    };
25  };
26  
27  if (!input.Event.Upload.MetaData.filename) {
28    Bun.stdout.write(`{"RejectUpload": true}`);
29  } else {
30    Bun.stdout.write("{}");
31  
32    const uploadedFile = Bun.file(input.Event.Upload.Storage.Path);
33    await Bun.write(
34      `/data/files/${input.Event.Upload.MetaData.filename.replaceAll("..", "")}`,
35      uploadedFile,
36      { createPath: true, mode: 600 }
37    );
38  }
39  
40  await Promise.all([
41    Bun.file(input.Event.Upload.Storage.Path).unlink(),
42    Bun.file(input.Event.Upload.Storage.InfoPath).unlink(),
43  ]);