mirror of
https://github.com/mollersuite/monofile.git
synced 2024-11-22 05:46:26 -08:00
switch to streams
This commit is contained in:
parent
c4fce06f7a
commit
c0cd0a1292
10
src/index.ts
10
src/index.ts
|
@ -59,7 +59,7 @@ app.get("/", function(req,res) {
|
||||||
.replace(/\$UploadButtonText/g,"Upload file")
|
.replace(/\$UploadButtonText/g,"Upload file")
|
||||||
.replace(/\$otherPath/g,"/clone")
|
.replace(/\$otherPath/g,"/clone")
|
||||||
.replace(/\$otherText/g,"clone from url...")
|
.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(/\$UploadButtonText/g,"Input a URL")
|
||||||
.replace(/\$otherPath/g,"/")
|
.replace(/\$otherPath/g,"/")
|
||||||
.replace(/\$otherText/g,"upload file...")
|
.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")
|
res.send("[err] invalid url")
|
||||||
}
|
}
|
||||||
axios.get(j.url,{responseType:"arraybuffer"}).then((data:AxiosResponse) => {
|
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))
|
.then((uID) => res.send(uID))
|
||||||
.catch((stat) => {res.status(stat.status);res.send(`[err] ${stat.message}`)})
|
.catch((stat) => {res.status(stat.status);res.send(`[err] ${stat.message}`)})
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
|
console.log(err)
|
||||||
res.status(400)
|
res.status(400)
|
||||||
res.send(`[err] failed to fetch data`)
|
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)
|
let f = await files.readFileStream(req.params.fileId)
|
||||||
|
|
||||||
res.setHeader("Content-Type",f.contentType)
|
res.setHeader("Content-Type",f.contentType)
|
||||||
f.pipe(res)
|
res.status(200)
|
||||||
|
f.dataStream.pipe(res)
|
||||||
})
|
})
|
||||||
|
|
||||||
app.get("/server",(req,res) => {
|
app.get("/server",(req,res) => {
|
||||||
|
|
|
@ -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}
|
if (err) {console.log(err);return}
|
||||||
this.files = JSON.parse(buf.toString() || "{}")
|
this.files = JSON.parse(buf.toString() || "{}")
|
||||||
})
|
})
|
||||||
|
@ -143,21 +143,22 @@ export default class Files {
|
||||||
}
|
}
|
||||||
|
|
||||||
// save
|
// save
|
||||||
|
console.log("writing")
|
||||||
|
|
||||||
return this.writeFile(
|
resolve(await this.writeFile(
|
||||||
uploadId,
|
uploadId,
|
||||||
{
|
{
|
||||||
filename:settings.name,
|
filename:settings.name,
|
||||||
messageids:msgIds,
|
messageids:msgIds,
|
||||||
mime:settings.mime
|
mime:settings.mime
|
||||||
}
|
}
|
||||||
)
|
))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// fs
|
// fs
|
||||||
|
|
||||||
writeFile(uploadId: string, file: FilePointer):Promise<string|StatusCodeError> {
|
writeFile(uploadId: string, file: FilePointer):Promise<string> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
|
|
||||||
this.files[uploadId] = file
|
this.files[uploadId] = file
|
||||||
|
@ -169,6 +170,8 @@ export default class Files {
|
||||||
delete this.files[uploadId];
|
delete this.files[uploadId];
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log("written")
|
||||||
|
|
||||||
resolve(uploadId)
|
resolve(uploadId)
|
||||||
|
|
||||||
|
@ -189,7 +192,9 @@ export default class Files {
|
||||||
if (this.files[uploadId]) {
|
if (this.files[uploadId]) {
|
||||||
let file = this.files[uploadId]
|
let file = this.files[uploadId]
|
||||||
|
|
||||||
let dataStream = new Readable()
|
let dataStream = new Readable({
|
||||||
|
read(){}
|
||||||
|
})
|
||||||
|
|
||||||
resolve({
|
resolve({
|
||||||
contentType: file.mime,
|
contentType: file.mime,
|
||||||
|
@ -206,11 +211,14 @@ export default class Files {
|
||||||
dataStream.push(d.data)
|
dataStream.push(d.data)
|
||||||
} else {
|
} else {
|
||||||
reject({status:500,message:"internal server error"})
|
reject({status:500,message:"internal server error"})
|
||||||
|
dataStream.destroy(new Error("file read error"))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dataStream.push(null)
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
reject({status:404,message:"not found"})
|
reject({status:404,message:"not found"})
|
||||||
|
@ -234,4 +242,8 @@ export default class Files {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getFilePointer(uploadId:string):FilePointer {
|
||||||
|
return this.files[uploadId]
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in a new issue