Add endpoint to newSingle.ts, add index

This commit is contained in:
Khushraj Rathod 2020-10-31 00:13:35 +05:30
parent 9cacef61da
commit 5d229106f3
No known key found for this signature in database
GPG key ID: B77B2A40E7702F19
3 changed files with 29 additions and 10 deletions

6
.vscode/settings.json vendored Normal file
View file

@ -0,0 +1,6 @@
{
"deno.enable": true,
"deno.import_intellisense_origins": {
"https://deno.land": true
},
}

View file

@ -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)

View file

@ -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
}
}