add etags ill do ore laterr

This commit is contained in:
May 2024-03-11 16:20:37 -07:00
parent d0c1c7be9c
commit a062a98996
3 changed files with 13 additions and 2 deletions

View file

@ -87,7 +87,7 @@ apiRouter.loadAPIMethods().then(() => {
console.log("API OK!") console.log("API OK!")
// moved here to ensure it's matched last // moved here to ensure it's matched last
app.get("/:fileId", (ctx) => app.fetch(ctx.req.raw, ctx.env, ctx.executionCtx)) app.get("/:fileId", async (ctx) => app.fetch(ctx.req.raw, ctx.env))
// listen on 3000 or MONOFILE_PORT // listen on 3000 or MONOFILE_PORT
// moved here to prevent a crash if someone manages to access monofile before api routes are mounted // moved here to prevent a crash if someone manages to access monofile before api routes are mounted

View file

@ -70,6 +70,8 @@ export interface FilePointer {
visibility?: FileVisibility visibility?: FileVisibility
reserved?: boolean reserved?: boolean
chunkSize?: number chunkSize?: number
lastModified?: number
md5?: string
} }
export interface StatusCodeError { export interface StatusCodeError {
@ -315,6 +317,8 @@ export class UploadStream extends Writable {
error?: Error error?: Error
hash: crypto.Hash = crypto.createHash("md5")
constructor(files: Files, owner?: string) { constructor(files: Files, owner?: string) {
super() super()
this.owner = owner this.owner = owner
@ -328,6 +332,8 @@ export class UploadStream extends Writable {
if (this.filled + data.byteLength > (this.files.config.maxDiscordFileSize*this.files.config.maxDiscordFiles)) if (this.filled + data.byteLength > (this.files.config.maxDiscordFileSize*this.files.config.maxDiscordFiles))
return this.destroy(new WebError(413, "maximum file size exceeded")) return this.destroy(new WebError(413, "maximum file size exceeded"))
this.hash.update(data)
// cut up the buffer into message sized chunks // cut up the buffer into message sized chunks
let position = 0 let position = 0
@ -431,7 +437,10 @@ export class UploadStream extends Writable {
// so that json.stringify doesnt include tag:undefined // so that json.stringify doesnt include tag:undefined
...((ogf||{}).tag ? {tag:ogf.tag} : {}), ...((ogf||{}).tag ? {tag:ogf.tag} : {}),
chunkSize: this.files.config.maxDiscordFileSize chunkSize: this.files.config.maxDiscordFileSize,
md5: this.hash.digest("hex"),
lastModified: Date.now()
} }
delete this.files.locks[this.uploadId!] delete this.files.locks[this.uploadId!]

View file

@ -32,6 +32,8 @@ export default function (files: Files) {
ctx.header("Access-Control-Allow-Origin", "*") ctx.header("Access-Control-Allow-Origin", "*")
ctx.header("Content-Security-Policy", "sandbox allow-scripts") ctx.header("Content-Security-Policy", "sandbox allow-scripts")
ctx.header("Content-Disposition", `${ctx.req.query("attachment") == "1" ? "attachment" : "inline"}; filename="${encodeURI(file.filename.replaceAll("\n","\\n"))}"`) ctx.header("Content-Disposition", `${ctx.req.query("attachment") == "1" ? "attachment" : "inline"}; filename="${encodeURI(file.filename.replaceAll("\n","\\n"))}"`)
ctx.header("ETag", file.md5)
//if (file.lastModified) ctx.header("Last-Modified", new Date(file.lastModified).toTimeString())
if (file) { if (file) {
if (file.visibility == "private") { if (file.visibility == "private") {