Add configurable field separator

This commit is contained in:
Alan Grainger
2026-10-19 17:33:47 +13:00
parent a620d0b3ae
commit ac34a03482
8 changed files with 118 additions and 32 deletions
+11 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, afterEach } from 'vitest'
import { askName, byteBudget, filenamePrefix, maxFileSize, notifyConfig, uploadNotice } from '../src/config'
import { askName, byteBudget, fieldSeparator, filenamePrefix, maxFileSize, notifyConfig, uploadNotice } from '../src/config'
import { resetConfig, setConfig } from './helpers'
afterEach(resetConfig)
@@ -14,6 +14,16 @@ describe('upload options', () => {
expect(notifyConfig()).toBeUndefined()
expect(uploadNotice()).toBe('Uploaded photos may not appear in the gallery immediately.')
expect(askName()).toBe(true)
expect(fieldSeparator()).toBe(',')
})
it('takes a usable field separator, and falls back for one that could be part of a field', () => {
setConfig({ ipp: { upload: { fieldSeparator: '^' } } })
expect(fieldSeparator()).toBe('^')
for (const bad of ['', '-', 'a', '.', ' ', '/', 7, null]) {
setConfig({ ipp: { upload: { fieldSeparator: bad } } })
expect(fieldSeparator()).toBe(',')
}
})
it('turns the name field off only on an explicit false', () => {
+26 -3
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { uploaderSlug, uploadFilename } from '../src/filename'
import { isUsableSeparator, uploaderSlug, uploadFilename } from '../src/filename'
describe('uploadFilename', () => {
it('keeps an ordinary filename', () => {
@@ -56,9 +56,17 @@ describe('uploadFilename', () => {
expect(uploadFilename('IMG_0042.jpg', 'ipp_upload_', '')).toBe('ipp_upload_IMG_0042.jpg')
})
it('removes commas from the visitor filename, so a comma only ever ends a field', () => {
it('removes the separator from the visitor filename, so it only ever ends a field', () => {
expect(uploadFilename('Paris, France.jpg', 'ipp_upload_')).toBe('ipp_upload_Paris France.jpg')
expect(uploadFilename('a,b,c.jpg', 'ipp_upload_', 'jane-smith')).toBe('ipp_upload_jane-smith,abc.jpg')
// Any other punctuation is an ordinary character and stays
expect(uploadFilename('best^photo.jpg', 'ipp_upload_', 'jane-smith')).toBe('ipp_upload_jane-smith,best^photo.jpg')
})
it('uses the configured separator, and removes that one instead', () => {
expect(uploadFilename('IMG_0042.jpg', 'ipp_upload_', 'jane-smith', '^')).toBe('ipp_upload_jane-smith^IMG_0042.jpg')
expect(uploadFilename('Paris, France^2.jpg', 'ipp_upload_', 'jane-smith', '^')).toBe('ipp_upload_jane-smith^Paris, France2.jpg')
expect(uploadFilename('IMG_0042.jpg', 'ipp_upload_', 'jane-smith', '~~')).toBe('ipp_upload_jane-smith~~IMG_0042.jpg')
})
it('stays within 254 characters with the extension intact', () => {
@@ -80,8 +88,9 @@ describe('uploaderSlug', () => {
expect(uploaderSlug('山田 太郎')).toBe('山田-太郎')
})
it('never keeps a comma or an underscore', () => {
it('never keeps a separator, punctuation or an underscore', () => {
expect(uploaderSlug('Smith, Jane')).toBe('smith-jane')
expect(uploaderSlug('jane^smith')).toBe('jane-smith')
expect(uploaderSlug('jane_smith')).toBe('jane-smith')
})
@@ -101,3 +110,17 @@ describe('uploaderSlug', () => {
expect(uploaderSlug('a'.repeat(31) + ' b')).toBe('a'.repeat(31))
})
})
describe('isUsableSeparator', () => {
it('accepts punctuation that sanitize keeps', () => {
for (const separator of ['^', ',', '~', '#', '+', '^^', '~,']) expect(isUsableSeparator(separator)).toBe(true)
})
it('rejects anything that could be part of a name or a filename, or is not a string', () => {
for (const separator of ['', 'a', '1', '-', '.', ' ', 'x^', '^ ', '/', ':', '*', '"', '\u0000', 'ñ', '٣']) {
expect(isUsableSeparator(separator)).toBe(false)
}
expect(isUsableSeparator(undefined)).toBe(false)
expect(isUsableSeparator(1)).toBe(false)
})
})