plsgivemeachane commited on
Commit
ae656e3
·
1 Parent(s): b175adc

Error handleing

Browse files
Files changed (1) hide show
  1. index.js +92 -89
index.js CHANGED
@@ -9,7 +9,8 @@ const statusMonitor = require("express-status-monitor")({
9
  title: "Bee3Hive API Status",
10
  });
11
  const cors = require("cors");
12
- const { withAccelerate } = require("@prisma/extension-accelerate")
 
13
  const prisma = new PrismaClient().$extends(withAccelerate())
14
  const app = express();
15
 
@@ -141,10 +142,6 @@ const storage = multer.diskStorage({
141
  },
142
  });
143
 
144
- if (!fs.existsSync("uploads")) {
145
- fs.mkdirSync("uploads");
146
- }
147
-
148
  // Set file size limit to 10GB
149
  const upload = multer({
150
  storage: storage,
@@ -419,104 +416,110 @@ app.post("/auth/delete", async (req, res) => {
419
  }
420
  });
421
 
422
- app.post("/file/upload", upload.single("file"), async (req, res) => {
423
-
424
- try {
425
- const upload_path = req.file.path;
426
- const file_path = req.body.filePath;
427
- const full_file_path = req.user.username + (file_path ? "/" + file_path : "");
428
-
429
- // Upload the file to another backend
430
-
431
- if(services.length == 0) {
432
- res.status(500).send("Service unavailable");
433
- return;
434
  }
435
-
436
- const server = services[Math.floor(Math.random() * services.length)];
437
-
438
- const body = {
439
- filename: file_path,
440
- directory: full_file_path,
441
- username: req.user.username,
442
- fileURI: req.file.filename,
443
- }
444
-
445
- // console.log(body)
446
-
447
- // Upload the file
448
  try {
449
- info("Uploading file to " + server);
450
- const resp = await fetch(server + "/fetch", {
451
- method: "POST",
452
- headers: {
453
- "Content-Type": "application/json",
454
- },
455
- body: JSON.stringify(body),
456
- });
457
 
458
- if(!resp.ok) {
459
- warn(await resp.text());
460
- res.sendStatus(500);
 
461
  return;
462
  }
463
 
464
- const json = await resp.json();
465
- console.log(json)
466
 
467
- if(await prisma.storage.findUnique({ where: { dir: full_file_path } })) {
468
- // File already exists and same content
469
- warn("File already exists. No override: " + full_file_path);
470
- res.sendStatus(200);
471
- return;
472
  }
473
 
474
- if(await prisma.storage.findUnique({ where: { dir: full_file_path } })) {
475
- warn("File already exists. Override: " + full_file_path);
476
- await prisma.storage.update({
477
- where:{
478
- dir: full_file_path
 
 
 
 
479
  },
 
 
480
 
481
- data: {
482
- handle: server,
483
- username: req.user.username,
484
- CID: json[0].Hash,
485
- size: +json[0].Size,
486
- dir: full_file_path,
487
- }
488
- })
489
- } else {
490
- info("Adding record: " + full_file_path);
491
- //* Add record
492
- await prisma.storage.create({
493
- data: {
494
- handle: server,
495
- username: req.user.username,
496
- CID: json[0].Hash,
497
- size: +json[0].Size,
498
- dir: full_file_path,
499
- }
500
- })
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
501
  }
502
- } catch (e) {
503
- warn("Error on uploading file");
504
- console.log(e);
505
- res.sendStatus(500);
506
- fs.unlinkSync(upload_path);
507
- return;
508
- }
509
 
510
- fs.unlinkSync(upload_path);
511
 
512
- info("File uploaded successfully: " + full_file_path);
513
- res.send("File uploaded successfully.");
514
- } catch (error) {
515
- // Fail to upload
516
- warn("File upload failed");
517
- console.log(error);
518
- res.status(500).send("File upload failed.");
519
- }
 
520
  });
521
 
522
  app.get("/file/list", async (req, res) => {
 
9
  title: "Bee3Hive API Status",
10
  });
11
  const cors = require("cors");
12
+ const { withAccelerate } = require("@prisma/extension-accelerate");
13
+ const { error } = require("console");
14
  const prisma = new PrismaClient().$extends(withAccelerate())
15
  const app = express();
16
 
 
142
  },
143
  });
144
 
 
 
 
 
145
  // Set file size limit to 10GB
146
  const upload = multer({
147
  storage: storage,
 
416
  }
417
  });
418
 
419
+ app.post("/file/upload", async (req, res) => {
420
+ upload.single("file")(req, res, async (err) => {
421
+ if(err) {
422
+ console.log(err)
423
+ return
 
 
 
 
 
 
 
424
  }
425
+
 
 
 
 
 
 
 
 
 
 
 
 
426
  try {
427
+ const upload_path = req.file.path;
428
+ const file_path = req.body.filePath;
429
+ const full_file_path = req.user.username + (file_path ? "/" + file_path : "");
 
 
 
 
 
430
 
431
+ // Upload the file to another backend
432
+
433
+ if(services.length == 0) {
434
+ res.status(500).send("Service unavailable");
435
  return;
436
  }
437
 
438
+ const server = services[Math.floor(Math.random() * services.length)];
 
439
 
440
+ const body = {
441
+ filename: file_path,
442
+ directory: full_file_path,
443
+ username: req.user.username,
444
+ fileURI: req.file.filename,
445
  }
446
 
447
+ // console.log(body)
448
+
449
+ // Upload the file
450
+ try {
451
+ info("Uploading file to " + server);
452
+ const resp = await fetch(server + "/fetch", {
453
+ method: "POST",
454
+ headers: {
455
+ "Content-Type": "application/json",
456
  },
457
+ body: JSON.stringify(body),
458
+ });
459
 
460
+ if(!resp.ok) {
461
+ warn(await resp.text());
462
+ res.sendStatus(500);
463
+ return;
464
+ }
465
+
466
+ const json = await resp.json();
467
+ console.log(json)
468
+
469
+ if(await prisma.storage.findUnique({ where: { dir: full_file_path } })) {
470
+ // File already exists and same content
471
+ warn("File already exists. No override: " + full_file_path);
472
+ res.sendStatus(200);
473
+ return;
474
+ }
475
+
476
+ if(await prisma.storage.findUnique({ where: { dir: full_file_path } })) {
477
+ warn("File already exists. Override: " + full_file_path);
478
+ await prisma.storage.update({
479
+ where:{
480
+ dir: full_file_path
481
+ },
482
+
483
+ data: {
484
+ handle: server,
485
+ username: req.user.username,
486
+ CID: json[0].Hash,
487
+ size: +json[0].Size,
488
+ dir: full_file_path,
489
+ }
490
+ })
491
+ } else {
492
+ info("Adding record: " + full_file_path);
493
+ //* Add record
494
+ await prisma.storage.create({
495
+ data: {
496
+ handle: server,
497
+ username: req.user.username,
498
+ CID: json[0].Hash,
499
+ size: +json[0].Size,
500
+ dir: full_file_path,
501
+ }
502
+ })
503
+ }
504
+ } catch (e) {
505
+ warn("Error on uploading file");
506
+ console.log(e);
507
+ res.sendStatus(500);
508
+ fs.unlinkSync(upload_path);
509
+ return;
510
  }
 
 
 
 
 
 
 
511
 
512
+ fs.unlinkSync(upload_path);
513
 
514
+ info("File uploaded successfully: " + full_file_path);
515
+ res.send("File uploaded successfully.");
516
+ } catch (error) {
517
+ // Fail to upload
518
+ warn("File upload failed");
519
+ console.log(error);
520
+ res.status(500).send("File upload failed.");
521
+ }
522
+ })
523
  });
524
 
525
  app.get("/file/list", async (req, res) => {