1.2.2 complete (i hope)

This commit is contained in:
May 2023-01-26 21:09:26 -08:00
parent c0cd0a1292
commit b53152a602
2 changed files with 26 additions and 18 deletions

View file

@ -10,19 +10,15 @@ import fs from "fs"
import axios, { AxiosResponse } from "axios" import axios, { AxiosResponse } from "axios"
import Files from "./lib/files" import Files from "./lib/files"
require('dotenv').config() require("dotenv").config()
let pkg = require(`${process.cwd()}/package.json`)
let app = express()
const multerSetup = multer({storage:memoryStorage()}) const multerSetup = multer({storage:memoryStorage()})
let pkg = require(`${process.cwd()}/package.json`)
let app = express()
let config = require(`${process.cwd()}/config.json`) let config = require(`${process.cwd()}/config.json`)
app.use("/static",express.static("assets")) app.use("/static",express.static("assets"))
app.use(bodyParser.text({limit:(config.maxDiscordFileSize*config.maxDiscordFiles)+1048576,type:["application/json","text/plain"]})) app.use(bodyParser.text({limit:(config.maxDiscordFileSize*config.maxDiscordFiles)+1048576,type:["application/json","text/plain"]}))
//let files:{[key:string]:{filename:string,mime:string,messageids:string[]}} = {}
// funcs // funcs
function ThrowError(response:express.Response,code:number,errorMessage:string) { function ThrowError(response:express.Response,code:number,errorMessage:string) {
@ -48,6 +44,10 @@ let client = new Client({intents:[
let files = new Files(client,config) let files = new Files(client,config)
// routes (could probably make these use routers)
// index, clone
app.get("/", function(req,res) { app.get("/", function(req,res) {
fs.readFile(__dirname+"/../pages/base.html",(err,buf) => { fs.readFile(__dirname+"/../pages/base.html",(err,buf) => {
if (err) {res.sendStatus(500);console.log(err);return} if (err) {res.sendStatus(500);console.log(err);return}
@ -80,6 +80,8 @@ app.get("/clone", function(req,res) {
}) })
}) })
// upload handlers
app.post("/upload",multerSetup.single('file'),async (req,res) => { app.post("/upload",multerSetup.single('file'),async (req,res) => {
if (req.file) { if (req.file) {
try { try {
@ -118,6 +120,8 @@ app.post("/clone",(req,res) => {
} }
}) })
// serve files & download page
app.get("/download/:fileId",(req,res) => { app.get("/download/:fileId",(req,res) => {
if (files.getFilePointer(req.params.fileId)) { if (files.getFilePointer(req.params.fileId)) {
let file = files.getFilePointer(req.params.fileId) let file = files.getFilePointer(req.params.fileId)
@ -132,22 +136,26 @@ app.get("/download/:fileId",(req,res) => {
}) })
app.get("/file/:fileId",async (req,res) => { app.get("/file/:fileId",async (req,res) => {
let f = await files.readFileStream(req.params.fileId) files.readFileStream(req.params.fileId).then(f => {
res.setHeader("Content-Type",f.contentType) res.setHeader("Content-Type",f.contentType)
res.status(200) res.status(200)
f.dataStream.pipe(res) f.dataStream.pipe(res)
}) }).catch((err) => {
ThrowError(res,err.status,err.message)
app.get("/server",(req,res) => { })
res.send(JSON.stringify({...config,version:pkg.version}))
}) })
app.get("*",(req,res) => { app.get("*",(req,res) => {
ThrowError(res,404,"Page not found.") ThrowError(res,404,"Page not found.")
}) })
app.listen(3000,function() { app.get("/server",(req,res) => {
res.send(JSON.stringify({...config,version:pkg.version}))
})
// listen on 3000 or MONOFILE_PORT
app.listen(process.env.MONOFILE_PORT || 3000,function() {
console.log("Web OK!") console.log("Web OK!")
}) })

View file