CDN v1 uses Auth passthrough

This commit is contained in:
Max Wofford 2021-10-12 15:11:21 -04:00
parent de82e5b371
commit 85312ea613
2 changed files with 20 additions and 7 deletions

View file

@ -87,15 +87,22 @@ export default async (req: ServerRequest) => {
);
}
const authorization = req.headers.get("Authorization");
const uploadedURLs = await Promise.all(fileURLs.map(async (url, index) => {
const { pathname } = urlParse(url);
const filename = index + pathname.substr(pathname.lastIndexOf("/") + 1);
const res = await (await fetch("https://cdn.hackclub.com/api/newSingle", {
method: "POST",
headers: {
const headers = {
"Content-Type": "application/json",
},
"Authorization": ""
}
if (authorization) {
headers['Authorization'] = authorization;
}
const res = await (await fetch("http://localhost:3000/api/newSingle", {
method: "POST",
headers,
body: url,
})).json();

View file

@ -10,8 +10,14 @@ const endpoint = (path: string) => {
return url;
};
const uploadFile = async (url: string) => {
const req = await fetch(url);
const uploadFile = async (url: string, authorization: string|null) => {
const options = {
method: 'GET', headers: { 'Authorization': "" }
}
if (authorization) {
options.headers = { 'Authorization': authorization }
}
const req = await fetch(url, options);
const data = new Uint8Array(await req.arrayBuffer());
const sha = new Hash("sha1").digest(data).hex();
const size = data.byteLength;
@ -53,7 +59,7 @@ export default async (req: ServerRequest) => {
},
);
}
const uploadedFileURL = await uploadFile(singleFileURL);
const uploadedFileURL = await uploadFile(singleFileURL, req.headers.get("Authorization"));
req.respond({
body: JSON.stringify(uploadedFileURL),