From 5d229106f3bf6d2fd5e9f6c86072fd627608d15d Mon Sep 17 00:00:00 2001 From: Khushraj Rathod Date: Sat, 31 Oct 2020 00:13:35 +0530 Subject: [PATCH] Add endpoint to newSingle.ts, add index --- .vscode/settings.json | 6 ++++++ api/new.ts | 16 ++++++++++++---- api/newSingle.ts | 17 +++++++++++------ 3 files changed, 29 insertions(+), 10 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..7d61b6f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "deno.enable": true, + "deno.import_intellisense_origins": { + "https://deno.land": true + }, +} \ No newline at end of file diff --git a/api/new.ts b/api/new.ts index be596ce..9b48734 100644 --- a/api/new.ts +++ b/api/new.ts @@ -1,4 +1,5 @@ import { ServerRequest } from 'https://deno.land/std@0.75.0/http/server.ts' +import { urlParse } from 'https://deno.land/x/url_parse/mod.ts'; const endpoint = (path: string) => { // https://vercel.com/docs/api#api-basics/authentication/accessing-resources-owned-by-a-team @@ -56,15 +57,22 @@ export default async (req: ServerRequest) => { return req.respond({ status: 422, body: JSON.stringify({ error: 'Empty file array' }) }) } - let uploadedURLs = await Promise.all(fileURLs.map(async (url) => { - const res = await fetch("https://cdn.hackclub.com/api/newSingle", { + let uploadedURLs = await Promise.all(fileURLs.map(async (url, index) => { + const { pathname } = urlParse(url) + const filename = index + pathname.substr(pathname.lastIndexOf('/') + 1) + + const res = JSON.parse(await (await fetch("https://cdn.hackclub.com/api/newSingle", { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: url - }) - return JSON.parse(await res.json()) + })).json()) + + res.file = 'public/' + filename + res.path = filename + + return res })) const result = await deploy(uploadedURLs) diff --git a/api/newSingle.ts b/api/newSingle.ts index 9ef85e7..56abb51 100644 --- a/api/newSingle.ts +++ b/api/newSingle.ts @@ -2,14 +2,21 @@ import { ServerRequest } from 'https://deno.land/std@0.75.0/http/server.ts' import { Hash } from "https://deno.land/x/checksum@1.4.0/mod.ts" import { urlParse } from 'https://deno.land/x/url_parse/mod.ts'; +const endpoint = (path: string) => { + // https://vercel.com/docs/api#api-basics/authentication/accessing-resources-owned-by-a-team + let url = 'https://api.vercel.com/' + path + if (Deno.env.get('ZEIT_TEAM')) { + url += ('?teamId=' + Deno.env.get('ZEIT_TEAM')) + } + return url +} + const uploadFile = async (url: string) => { const req = await fetch(url) const data = new Uint8Array(await req.arrayBuffer()) const sha = new Hash('sha1').digest(data).hex() const size = data.byteLength - const { pathname } = urlParse(url) - const filename = pathname.substr(pathname.lastIndexOf('/') + 1) - + await fetch(endpoint('v2/now/files'), { method: 'POST', headers: { @@ -22,9 +29,7 @@ const uploadFile = async (url: string) => { return { sha, - size, - file: 'public/' + filename, - path: filename + size } }