Attempt to add authorization pass through

This commit is contained in:
Max Wofford 2021-10-08 03:05:36 -04:00
parent a4e7cfaea6
commit 6112297d37
2 changed files with 18 additions and 9 deletions

View file

@ -19,7 +19,9 @@ export default async (req: ServerRequest) => {
); );
} }
const uploadArray = await Promise.all(fileURLs.map(uploadEndpoint)); const authorization = req.headers.get('Authorization')
const uploadArray = await Promise.all(fileURLs.map(f => uploadEndpoint(f, authorization)));
const deploymentFiles = uploadArray.map( const deploymentFiles = uploadArray.map(
(file: { url: string; sha: string; size: number }, index: number) => { (file: { url: string; sha: string; size: number }, index: number) => {

View file

@ -3,18 +3,25 @@ import { Hash } from "https://deno.land/x/checksum@1.4.0/hash.ts";
import { endpoint, ensurePost, parseBody } from "./utils.ts"; import { endpoint, ensurePost, parseBody } from "./utils.ts";
// Other functions can import this function to call this serverless endpoint // Other functions can import this function to call this serverless endpoint
export const uploadEndpoint = async (url: string) => { export const uploadEndpoint = async (url: string, authorization: string | null) => {
const response = await fetch("https://cdn.hackclub.com/api/v2/upload", { const options = { method: 'POST', body: url, headers: {} }
method: "POST", if (authorization) {
body: url, options.headers = { 'Authorization': authorization }
}); }
console.log({ options})
const response = await fetch("https://cdn.hackclub.com/api/v2/upload", options);
const result = await response.json(); const result = await response.json();
console.log({result})
return result; return result;
}; };
const upload = async (url: string) => { const upload = async (url: string, authorization: string | null) => {
const req = await fetch(url); const options = { headers: {} }
if (authorization) {
options.headers = { 'Authorization': authorization }
}
const req = await fetch(url, options);
const reqArrayBuffer = await req.arrayBuffer(); const reqArrayBuffer = await req.arrayBuffer();
const data = new Uint8Array(reqArrayBuffer); const data = new Uint8Array(reqArrayBuffer);
const sha = new Hash("sha1").digest(data).hex(); const sha = new Hash("sha1").digest(data).hex();
@ -41,7 +48,7 @@ export default async (req: ServerRequest) => {
if (!ensurePost(req)) return null; if (!ensurePost(req)) return null;
const body = await parseBody(req.body); const body = await parseBody(req.body);
const uploadedFileUrl = await upload(body); const uploadedFileUrl = await upload(body, req.headers.get("Authorization"));
req.respond({ body: JSON.stringify(uploadedFileUrl) }); req.respond({ body: JSON.stringify(uploadedFileUrl) });
}; };