update, formatting and vercel

This commit is contained in:
2021-01-02 00:21:29 +01:00
parent 4ee9c1bb8b
commit 801e1b6710
23 changed files with 1456 additions and 2986 deletions

View File

@@ -1,83 +1,77 @@
import { createReadStream, statSync, unlinkSync, writeFileSync } from 'fs'
import { tmpdir } from 'os'
import { NextApiRequest, NextApiResponse } from 'next'
import getConfig from 'next/config'
import type { NextApiRequest, NextApiResponse } from 'next'
import Formidable from 'formidable'
import axios from 'axios'
const { RECAPTCHA_SERVER, NEXTCLOUD_TOKEN } = getConfig().serverRuntimeConfig
const sendFileAndDelete = async (name: string, path: string) => {
const stat = statSync(path)
const stream = createReadStream(path)
const stat = statSync(path)
const stream = createReadStream(path)
await axios({
url: 'https://cloud.nicco.io/public.php/webdav/' + name,
method: 'put',
auth: {
username: NEXTCLOUD_TOKEN,
password: '',
},
headers: {
'Content-Length': stat.size
},
data: stream
})
await axios({
url: 'https://cloud.nicco.io/public.php/webdav/' + name,
method: 'put',
auth: {
username: process.env.NEXTCLOUD_TOKEN || '',
password: '',
},
headers: {
'Content-Length': stat.size,
},
data: stream,
maxContentLength: Infinity,
})
unlinkSync(path)
unlinkSync(path)
}
export default async (req: NextApiRequest, res: NextApiResponse) => {
try {
try {
// @ts-ignore
const form = new Formidable()
// @ts-ignore
const form = new Formidable();
form.maxFileSize = 300 * 1024 * 1024 // 300MiB
form.maxFileSize = 300 * 1024 * 1024 // 300MiB
const body = await new Promise<any>((resolve, reject) => {
form.parse(req, (err: any, fields: any, files: any) => {
if (err) reject()
else
resolve({
fields: JSON.parse(fields.json),
files: Object.values(files),
})
})
})
const body = await new Promise<any>((resolve, reject) => {
form.parse(req, (err: any, fields: any, files: any) => {
if (err) reject()
else resolve({
fields: JSON.parse(fields.json),
files: Object.values(files)
})
});
})
const { token, ...rest } = body.fields
const { data } = await axios({
url: 'https://www.google.com/recaptcha/api/siteverify',
method: 'post',
params: {
secret: process.env.RECAPTCHA_SERVER,
response: token,
},
})
if (!data.success) throw new Error()
const { token, ...rest } = body.fields
const now = Date.now()
for (const file of body.files) await sendFileAndDelete(`${now}_${file.name}`, file.path)
const { data } = await axios({
url: 'https://www.google.com/recaptcha/api/siteverify',
method: 'post',
params: {
secret: RECAPTCHA_SERVER,
response: token,
},
})
const txtFile = `${tmpdir()}/text`
writeFileSync(txtFile, `${rest.contact}\n${rest.description}`)
await sendFileAndDelete(`${now}_details.txt`, txtFile)
if (!data.success) throw new Error()
const now = Date.now()
for (const file of body.files)
sendFileAndDelete(`${now}_${file.name}`, file.path)
const txtFile = `${tmpdir()}/text`
writeFileSync(txtFile, `${rest.contact}\n${rest.description}`)
sendFileAndDelete(`${now}_details.txt`, txtFile)
res.status(200).end()
} catch {
res.status(400).end()
}
res.status(200).end()
} catch {
res.status(400).end()
}
}
export const config = {
api: {
bodyParser: false,
},
}
api: {
bodyParser: false,
},
}