improved mimetype check method

This commit is contained in:
srgooglo 2021-07-14 12:11:41 +02:00
parent 4dd10a538e
commit 7bdb555cd7

View File

@ -12,13 +12,16 @@ class FilesystemDriver {
this.defaultWriteMode = "0777"
this.defaultWriteFlags = 'w+'
this.matchMimetypes = String()
this.strictMimetypeCheck = this.strictMimetypeCheck ?? false
this.checkMimetypes = this.params.checkMimetypes ?? false
this.mimetypes = String()
if (Array.isArray(this.params.allowedMimetypes)) {
this.checkMimetypes = true
this.params.allowedMimetypes.forEach((type, index) => {
if (index == 0) {
return this.matchMimetypes = `${type}.*`
return this.mimetypes = `${type}.*`
}
return this.matchMimetypes = `${this.matchMimetypes}|${type}.*`
return this.mimetypes = `${this.mimetypes}|${type}.*`
})
}
@ -33,7 +36,10 @@ class FilesystemDriver {
}
checkMimetype(type) {
return type.match(this.matchMimetypes)
if (!this.checkMimetypes) {
return true
}
return type.match(this.mimetypes)
}
stream = (dir, options) => {
@ -49,11 +55,17 @@ class FilesystemDriver {
fs.mkdirSync(fileParent)
}
if (typeof file.mimetype === "undefined" && this.strictMimetypeCheck) {
throw new Error(`Missing mimetype`)
}
if (typeof file.mimetype !== "undefined") {
const validMIME = this.checkMimetype(file.mimetype) ? true : false
if (!validMIME) {
if (!validMIME && this.strictMimetypeCheck) {
throw new Error(`Invalid mimetype [${file.mimetype}]`)
}
}
const stream = this.stream(filePath, { mode: options?.mode ?? this.defaultWriteMode, flags: options?.flags ?? this.defaultWriteFlags })