Spaces:
Paused
Paused
plsgivemeachane
commited on
Commit
·
c959c78
1
Parent(s):
258667e
Error checking 8
Browse files
index.js
CHANGED
|
@@ -416,120 +416,107 @@ app.post("/auth/delete", async (req, res) => {
|
|
| 416 |
}
|
| 417 |
});
|
| 418 |
|
| 419 |
-
app.post("/file/upload", async (req, res) => {
|
| 420 |
-
// Try to write file
|
| 421 |
-
fs.writeFileSync("uploads/test2.txt", 'TEST');
|
| 422 |
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
|
|
|
|
|
|
|
|
|
| 427 |
|
|
|
|
| 428 |
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 433 |
}
|
| 434 |
-
|
| 435 |
-
try {
|
| 436 |
-
console.log(req.file)
|
| 437 |
-
const upload_path = req.file.path;
|
| 438 |
-
const file_path = req.body.filePath;
|
| 439 |
-
const full_file_path = req.user.username + (file_path ? "/" + file_path : "");
|
| 440 |
|
| 441 |
-
|
| 442 |
|
| 443 |
-
|
| 444 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 445 |
return;
|
| 446 |
}
|
| 447 |
|
| 448 |
-
const
|
|
|
|
| 449 |
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
}
|
| 456 |
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
const resp = await fetch(server + "/fetch", {
|
| 463 |
-
method: "POST",
|
| 464 |
-
headers: {
|
| 465 |
-
"Content-Type": "application/json",
|
| 466 |
},
|
| 467 |
-
body: JSON.stringify(body),
|
| 468 |
-
});
|
| 469 |
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
dir: full_file_path
|
| 491 |
-
},
|
| 492 |
-
|
| 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 |
-
} else {
|
| 502 |
-
info("Adding record: " + full_file_path);
|
| 503 |
-
//* Add record
|
| 504 |
-
await prisma.storage.create({
|
| 505 |
-
data: {
|
| 506 |
-
handle: server,
|
| 507 |
-
username: req.user.username,
|
| 508 |
-
CID: json[0].Hash,
|
| 509 |
-
size: +json[0].Size,
|
| 510 |
-
dir: full_file_path,
|
| 511 |
-
}
|
| 512 |
-
})
|
| 513 |
-
}
|
| 514 |
-
} catch (e) {
|
| 515 |
-
warn("Error on uploading file");
|
| 516 |
-
console.log(e);
|
| 517 |
-
res.sendStatus(500);
|
| 518 |
-
fs.unlinkSync(upload_path);
|
| 519 |
-
return;
|
| 520 |
}
|
| 521 |
-
|
|
|
|
|
|
|
|
|
|
| 522 |
fs.unlinkSync(upload_path);
|
| 523 |
-
|
| 524 |
-
info("File uploaded successfully: " + full_file_path);
|
| 525 |
-
res.send("File uploaded successfully.");
|
| 526 |
-
} catch (error) {
|
| 527 |
-
// Fail to upload
|
| 528 |
-
warn("File upload failed");
|
| 529 |
-
console.log(error);
|
| 530 |
-
res.status(500).send("File upload failed.");
|
| 531 |
}
|
| 532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
});
|
| 534 |
|
| 535 |
app.get("/file/list", async (req, res) => {
|
|
|
|
| 416 |
}
|
| 417 |
});
|
| 418 |
|
| 419 |
+
app.post("/file/upload", upload.single("file"), async (req, res) => {
|
|
|
|
|
|
|
| 420 |
|
| 421 |
+
try {
|
| 422 |
+
const tests = fs.readdirSync("uploads");
|
| 423 |
+
console.log(tests)
|
| 424 |
+
console.log(req.file)
|
| 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) => {
|