switch to streams

This commit is contained in:
May 2023-01-26 17:56:51 -08:00
parent c4fce06f7a
commit c0cd0a1292
2 changed files with 23 additions and 9 deletions

View file

@ -59,7 +59,7 @@ app.get("/", function(req,res) {
.replace(/\$UploadButtonText/g,"Upload file")
.replace(/\$otherPath/g,"/clone")
.replace(/\$otherText/g,"clone from url...")
.replace(/\$FileNum/g,Object.keys(files).length.toString())
.replace(/\$FileNum/g,Object.keys(files.files).length.toString())
)
})
})
@ -75,7 +75,7 @@ app.get("/clone", function(req,res) {
.replace(/\$UploadButtonText/g,"Input a URL")
.replace(/\$otherPath/g,"/")
.replace(/\$otherText/g,"upload file...")
.replace(/\$FileNum/g,Object.keys(files).length.toString())
.replace(/\$FileNum/g,Object.keys(files.files).length.toString())
)
})
})
@ -104,10 +104,11 @@ app.post("/clone",(req,res) => {
res.send("[err] invalid url")
}
axios.get(j.url,{responseType:"arraybuffer"}).then((data:AxiosResponse) => {
uploadFile({name:req.body.split("/")[req.body.split("/").length-1] || "generic",mime:data.headers["content-type"],uploadId:j.uploadId},Buffer.from(data.data))
files.uploadFile({name:req.body.split("/")[req.body.split("/").length-1] || "generic",mime:data.headers["content-type"],uploadId:j.uploadId},Buffer.from(data.data))
.then((uID) => res.send(uID))
.catch((stat) => {res.status(stat.status);res.send(`[err] ${stat.message}`)})
}).catch((err) => {
console.log(err)
res.status(400)
res.send(`[err] failed to fetch data`)
})
@ -134,7 +135,8 @@ app.get("/file/:fileId",async (req,res) => {
let f = await files.readFileStream(req.params.fileId)
res.setHeader("Content-Type",f.contentType)
f.pipe(res)
res.status(200)
f.dataStream.pipe(res)
})
app.get("/server",(req,res) => {

View file

@ -57,7 +57,7 @@ export default class Files {
})
})
readFile(__dirname+"/../.data/files.json",(err,buf) => {
readFile(process.cwd()+"/.data/files.json",(err,buf) => {
if (err) {console.log(err);return}
this.files = JSON.parse(buf.toString() || "{}")
})
@ -143,21 +143,22 @@ export default class Files {
}
// save
console.log("writing")
return this.writeFile(
resolve(await this.writeFile(
uploadId,
{
filename:settings.name,
messageids:msgIds,
mime:settings.mime
}
)
))
})
}
// fs
writeFile(uploadId: string, file: FilePointer):Promise<string|StatusCodeError> {
writeFile(uploadId: string, file: FilePointer):Promise<string> {
return new Promise((resolve, reject) => {
this.files[uploadId] = file
@ -169,6 +170,8 @@ export default class Files {
delete this.files[uploadId];
return
}
console.log("written")
resolve(uploadId)
@ -189,7 +192,9 @@ export default class Files {
if (this.files[uploadId]) {
let file = this.files[uploadId]
let dataStream = new Readable()
let dataStream = new Readable({
read(){}
})
resolve({
contentType: file.mime,
@ -206,11 +211,14 @@ export default class Files {
dataStream.push(d.data)
} else {
reject({status:500,message:"internal server error"})
dataStream.destroy(new Error("file read error"))
return
}
}
}
}
dataStream.push(null)
} else {
reject({status:404,message:"not found"})
@ -234,4 +242,8 @@ export default class Files {
})
}
getFilePointer(uploadId:string):FilePointer {
return this.files[uploadId]
}
}