feat: better extension names parsing

It will now preserve upper/lower/mixed case.

It will now properly preserve all tarballs (#370).
This commit is contained in:
Bobby Wibowo 2021-01-29 23:15:24 +07:00
parent a752bb89e8
commit 631d14d887
No known key found for this signature in database
GPG Key ID: 51C3A1E1E22D26CF
3 changed files with 18 additions and 12 deletions

View File

@ -168,7 +168,9 @@ self.isExtensionFiltered = extname => {
// If there are extensions that have to be filtered
if (extname && extensionsFilter) {
const match = config.extensionsFilter.some(extension => extname === extension.toLowerCase())
const match = config.extensionsFilter.some(extension =>
extname.localeCompare(extension, undefined, { sensitivity: 'accent' }) === 0
)
const whitelist = config.extensionsFilterMode === 'whitelist'
if ((!whitelist && match) || (whitelist && !match)) return true
}
@ -366,7 +368,9 @@ self.actuallyUploadUrls = async (req, res, user, albumid, age) => {
// Extensions filter
let filtered = false
if (urlExtensionsFilter && ['blacklist', 'whitelist'].includes(config.uploads.urlExtensionsFilterMode)) {
const match = config.uploads.urlExtensionsFilter.some(extension => extname === extension.toLowerCase())
const match = config.uploads.urlExtensionsFilter.some(extension =>
extname.localeCompare(extension, undefined, { sensitivity: 'accent' }) === 0
)
const whitelist = config.uploads.urlExtensionsFilterMode === 'whitelist'
filtered = ((!whitelist && match) || (whitelist && !match))
} else {

View File

@ -81,34 +81,36 @@ self.mayGenerateThumb = extname => {
(config.uploads.generateThumbs.video && self.videoExts.includes(extname))
}
// Expand if necessary (must be lower case); for now only preserves some known tarballs
const extPreserves = ['.tar.gz', '.tar.z', '.tar.bz2', '.tar.lzma', '.tar.lzo', '.tar.xz']
// Expand if necessary (must be lower case)
const extPreserves = [
/\.tar\.\w+/i // tarballs
]
self.extname = filename => {
// Always return blank string if the filename does not seem to have a valid extension
// Files such as .DS_Store (anything that starts with a dot, without any extension after) will still be accepted
if (!/\../.test(filename)) return ''
let lower = filename.toLowerCase() // due to this, the returned extname will always be lower case
let multi = ''
let extname = ''
// check for multi-archive extensions (.001, .002, and so on)
if (/\.\d{3}$/.test(lower)) {
multi = lower.slice(lower.lastIndexOf('.') - lower.length)
lower = lower.slice(0, lower.lastIndexOf('.'))
if (/\.\d{3}$/.test(filename)) {
multi = filename.slice(filename.lastIndexOf('.') - filename.length)
filename = filename.slice(0, filename.lastIndexOf('.'))
}
// check against extensions that must be preserved
for (const extPreserve of extPreserves) {
if (lower.endsWith(extPreserve)) {
extname = extPreserve
const match = filename.match(extPreserve)
if (match && match[0]) {
extname = match[0]
break
}
}
if (!extname) {
extname = lower.slice(lower.lastIndexOf('.') - lower.length) // path.extname(lower)
extname = filename.slice(filename.lastIndexOf('.') - filename.length)
}
return extname + multi

View File

@ -125,7 +125,7 @@
{%- endif %}{{ ' ' -}}
{% set comma = joiner(', ') %}
{%- for extension in urlExtensionsFilter -%}
{{ comma() }}{{ extension | replace('.', '') | upper }}
{{ comma() }}{{ extension | replace('.', '', 1) | upper }}
{%- endfor %}.
{{- ' ' }}{% endif -%}