port clone route

This commit is contained in:
May 2024-03-04 23:03:20 -08:00
parent 840fb2ab39
commit fb720bc4b7

View file

@ -11,6 +11,7 @@ import {Readable} from "node:stream"
import {ReadableStream as StreamWebReadable} from "node:stream/web" import {ReadableStream as StreamWebReadable} from "node:stream/web"
import formidable from "formidable" import formidable from "formidable"
import { HttpBindings } from "@hono/node-server" import { HttpBindings } from "@hono/node-server"
import pkg from "../../../../../package.json" assert {type: "json"}
export let primaryApi = new Hono<{ export let primaryApi = new Hono<{
Variables: { Variables: {
account: Accounts.Account account: Accounts.Account
@ -117,17 +118,11 @@ export default function (files: Files) {
let acc = ctx.get("account") as Accounts.Account | undefined let acc = ctx.get("account") as Accounts.Account | undefined
if (!ctx.req.header("Content-Type")?.startsWith("multipart/form-data")) { if (!ctx.req.header("Content-Type")?.startsWith("multipart/form-data"))
ctx.status(400) return resolve(ctx.body("must be multipart/form-data", 400))
resolve(ctx.body("[err] must be multipart/form-data"))
return
}
if (!ctx.req.raw.body) { if (!ctx.req.raw.body)
ctx.status(400) return resolve(ctx.body("body must be supplied", 400))
resolve(ctx.body("[err] body must be supplied"))
return
}
let file = files.createWriteStream(acc?.id) let file = files.createWriteStream(acc?.id)
let parser = formidable({ let parser = formidable({
@ -179,45 +174,57 @@ export default function (files: Files) {
})} })}
) )
/*
primaryApi.post( primaryApi.post(
"/clone", "/clone",
requiresPermissions("upload"), requiresPermissions("upload"),
async ctx => { ctx => new Promise(async resolve => {
let acc = ctx.get("account") as Accounts.Account let acc = ctx.get("account") as Accounts.Account
let requestParameters
try { try {
return axios requestParameters = await ctx.req.json()
.get(req.body.url, { responseType: "arraybuffer" }) } catch (err: any) {return ctx.text(err.toString(), 400)}
.then((data: AxiosResponse) => {
files let res = await fetch(requestParameters.url, {
.uploadFile( headers: {
{ "user-agent": `monofile ${pkg.version} (+https://${ctx.req.header("Host")})`
owner: acc?.id, }
filename: })
req.body.url.split("/")[ if (!res.ok) return ctx.text(`got ${res.status} ${res.statusText}`, 500)
req.body.url.split("/").length - 1 if (!res.body) return ctx.text(`Internal Server Error`, 500)
] || "generic", if (
mime: data.headers["content-type"], res.headers.has("Content-Length")
uploadId: req.body.uploadId, && !Number.isNaN(parseInt(res.headers.get("Content-Length")!,10))
}, && parseInt(res.headers.get("Content-Length")!,10) > files.config.maxDiscordFileSize*files.config.maxDiscordFiles
Buffer.from(data.data) )
) return ctx.text(`file reports to be too large`, 413)
.then((uID) => res.send(uID))
.catch((stat) => { let file = files.createWriteStream(acc?.id)
res.status(stat.status)
res.send(`[err] ${stat.message}`) Readable.fromWeb(res.body as StreamWebReadable)
}) .pipe(file)
}) .on("error", (err) => resolve(ctx.text(err.message, err instanceof WebError ? err.statusCode : 500)))
.catch((err) => {
console.log(err) file
return res.text(`[err] failed to fetch data`, 400) .setName(
}) requestParameters.url.split("/")[
} catch { requestParameters.url.split("/").length - 1
return ctx.text("[err] an error occured", 500) ] || "generic"
} )
}
if (res.headers.has("content-type")) file.setType(res.headers.get("content-type")!)
if (requestParameters.uploadId) file.setUploadId(requestParameters.uploadId)
file.once("finish", () => {
file.commit()
.then(id => resolve(ctx.text(id!)))
.catch((err) => resolve(ctx.text(err.message, err instanceof WebError ? err.statusCode : 500)))
})
})
) )
*/
return primaryApi return primaryApi
} }