jbilcke-hf HF staff commited on
Commit
df346d9
1 Parent(s): 9e51b11

trying to handle more cases

Browse files
Files changed (1) hide show
  1. src/index.mts +10 -3
src/index.mts CHANGED
@@ -33,13 +33,14 @@ app.post("/", async (req: Request, res: Response, _next: NextFunction) => {
33
  let dataFile: fileUpload.UploadedFile | string = "";
34
 
35
  try {
 
36
  if (req.is("json")) {
37
  const { data } = await axios.get(req.body.assetUrl, {
38
  responseType: "arraybuffer",
39
  });
40
  dataFile = Buffer.from(data, "binary");
41
  }
42
- // Request is not JSON type is file upload request
43
  else {
44
  if (!req.files || !req.files.data || req.files.data.mimetype !== 'video/mp4') {
45
  return res.status(400).send("Missing or invalid data file in request");
@@ -92,12 +93,18 @@ function setupDirectories() {
92
  }
93
 
94
  async function handleFileStorage(dataFile: fileUpload.UploadedFile | string, projectTempDir: string) {
95
- if (typeof dataFile === "string") {
96
  fs.writeFile(path.join(projectTempDir, "data.mp4"), dataFile, (err) => {
97
  if (err) throw err;
98
  });
 
 
 
 
 
 
99
  } else {
100
- await dataFile.mv(path.join(projectTempDir, dataFile.name));
101
  }
102
  }
103
 
 
33
  let dataFile: fileUpload.UploadedFile | string = "";
34
 
35
  try {
36
+ // we accept either JSON requests
37
  if (req.is("json")) {
38
  const { data } = await axios.get(req.body.assetUrl, {
39
  responseType: "arraybuffer",
40
  });
41
  dataFile = Buffer.from(data, "binary");
42
  }
43
+ // or file uploads
44
  else {
45
  if (!req.files || !req.files.data || req.files.data.mimetype !== 'video/mp4') {
46
  return res.status(400).send("Missing or invalid data file in request");
 
93
  }
94
 
95
  async function handleFileStorage(dataFile: fileUpload.UploadedFile | string, projectTempDir: string) {
96
+ if (dataFile instanceof Buffer) {
97
  fs.writeFile(path.join(projectTempDir, "data.mp4"), dataFile, (err) => {
98
  if (err) throw err;
99
  });
100
+ } else if (typeof dataFile === "object" && dataFile.mv) {
101
+ try {
102
+ await dataFile.mv(path.join(projectTempDir, dataFile.name));
103
+ } catch (error) {
104
+ throw new Error(`File can't be moved: ${error}`);
105
+ }
106
  } else {
107
+ throw new Error("Invalid File");
108
  }
109
  }
110