Skip to content

Commit

Permalink
Merge pull request #42 from vcheckzen/patch-1
Browse files Browse the repository at this point in the history
refactor(all): simplify the code
  • Loading branch information
vcheckzen authored Jul 28, 2024
2 parents fd46129 + 2ddeb07 commit 5e5a18b
Showing 1 changed file with 25 additions and 19 deletions.
44 changes: 25 additions & 19 deletions back-end-cf/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const PROTECTED_LAYERS = -1;
const EXPOSE_PASSWD = "";

addEventListener('scheduled', event => {
event.waitUntil(fetchAccessToken(/* event.scheduledTime */));
event.waitUntil(fetchAccessToken( /* event.scheduledTime */ ));
});

addEventListener("fetch", (event) => {
Expand Down Expand Up @@ -63,9 +63,11 @@ async function handleRequest(request) {
const url = await fetchFiles(requestPath, fileName);
return Response.redirect(url, 302);
} else {
const { headers } = request;
const {
headers
} = request;
const contentType = headers.get("content-type");
let body = {};
const body = {};
if (contentType && contentType.includes("form")) {
const formData = await request.formData();
for (const entry of formData.entries()) {
Expand All @@ -85,12 +87,13 @@ async function handleRequest(request) {
}

async function gatherResponse(response) {
const { headers } = response;
const {
headers
} = response;
const contentType = headers.get("content-type");
if (contentType.includes("application/json")) {
return await response.json();
}

return await response.text();
}

Expand All @@ -110,7 +113,9 @@ async function getContent(url) {
}

async function getContentWithHeaders(url, headers) {
const response = await cacheFetch(url, { headers: headers });
const response = await cacheFetch(url, {
headers: headers
});
const result = await gatherResponse(response);
return result;
}
Expand Down Expand Up @@ -193,34 +198,35 @@ async function fetchFiles(path, fileName, passwd, viewExposePathPassword) {
}
}

let parent = body.children.length
? body.children[0].parentReference.path
: body.parentReference.path;
let parent = body.children.length ?
body.children[0].parentReference.path :
body.parentReference.path;
parent = parent.split(":").pop().replace(EXPOSE_PATH, "") || "/";
parent = decodeURIComponent(parent);

if (authState === PATH_AUTH_STATES.NO_PW_FILE && parent.split("/").length <= PROTECTED_LAYERS) {
const upperPasswd = EXPOSE_PASSWD ? EXPOSE_PASSWD : (
(!relativePath || relativePath === "/") ? "" : await fetchFiles("", null, null, true)
);
if (upperPasswd !== passwd) authState = PATH_AUTH_STATES.PW_ERROR;
if (upperPasswd !== passwd) {
authState = PATH_AUTH_STATES.PW_ERROR;
}
}

// Auth failed
if (authState === PATH_AUTH_STATES.PW_ERROR) {
return JSON.stringify({ parent, files: [], encrypted: true });
return JSON.stringify({
parent,
files: [],
encrypted: true
});
}

// Download file
if (fileName) {
let thisFile = null;
body.children.forEach((file) => {
if (file.name === decodeURIComponent(fileName)) {
thisFile = file["@microsoft.graph.downloadUrl"];
return;
}
});
return thisFile;
return body
.children
.filter(file => file.name === decodeURIComponent(fileName))[0]?.["@microsoft.graph.downloadUrl"];
}

// List folder
Expand Down

0 comments on commit 5e5a18b

Please sign in to comment.