Skip to content

Commit

Permalink
fix crashing when trying to view a file that doesn't exist
Browse files Browse the repository at this point in the history
  • Loading branch information
sky-is-winning committed Jul 29, 2024
1 parent 62b0474 commit 283c8ac
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
cd cparchives
npm install
screen -XS cparchives quit
screen -dmS cparchives sudo npm run start
screen -dmS cparchives -L -Logfile log.txt sudo npm run start
16 changes: 14 additions & 2 deletions src/webserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ export default class WebServer {
const url = request.url.split(domainKey)[1];
const gdriveUrl = this.getFileURLFromGDrive(url, REDIRECT_DOMAINS[domainKey]);

if (!gdriveUrl) {
response.writeHead(404, {"Content-Type": "text/html"});
response.end("File not found", "utf-8");
return;
}

this.cache[request.url] = {status: 302, location: gdriveUrl};
response.writeHead(302, {
"Content-Type": contentType,
Expand Down Expand Up @@ -220,11 +226,17 @@ export default class WebServer {
let jsonObject = this.fileStructure[originalDomain];
for (let i = 0; i < fileArr.length; i++) {
if (!fileArr[i]) continue;
if (!jsonObject) return console.log("Error: " + filePath + " not found in file structure");
if (!jsonObject) {
console.log("Error: " + filePath + " not found in file structure");
return null;
}
if (!jsonObject.children) return jsonObject.id;
jsonObject = jsonObject.children[fileArr[i]];
}
if (!jsonObject) return console.log("Error: " + filePath + " not found in file structure");
if (!jsonObject) {
console.log("Error: " + filePath + " not found in file structure");
return null;
}
return `https://drive.usercontent.google.com/download?id=${jsonObject.id}&export=download&authuser=0`;
}

Expand Down

0 comments on commit 283c8ac

Please sign in to comment.