#302 add name field for uploads

This commit is contained in:
Alan Grainger
2026-10-19 15:43:29 +13:00
parent b1e053426f
commit a620d0b3ae
19 changed files with 257 additions and 24 deletions
+9 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect, afterEach } from 'vitest'
import { byteBudget, filenamePrefix, maxFileSize, notifyConfig, uploadNotice } from '../src/config'
import { askName, byteBudget, filenamePrefix, maxFileSize, notifyConfig, uploadNotice } from '../src/config'
import { resetConfig, setConfig } from './helpers'
afterEach(resetConfig)
@@ -13,6 +13,14 @@ describe('upload options', () => {
expect(filenamePrefix()).toBe('ipp_upload_')
expect(notifyConfig()).toBeUndefined()
expect(uploadNotice()).toBe('Uploaded photos may not appear in the gallery immediately.')
expect(askName()).toBe(true)
})
it('turns the name field off only on an explicit false', () => {
setConfig({ ipp: { upload: { askName: false } } })
expect(askName()).toBe(false)
setConfig({ ipp: { upload: { askName: 0 } } })
expect(askName()).toBe(true)
})
it('shows the owner\'s notice, and hides one that is not a string', () => {
+49 -1
View File
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
import { uploadFilename } from '../src/filename'
import { uploaderSlug, uploadFilename } from '../src/filename'
describe('uploadFilename', () => {
it('keeps an ordinary filename', () => {
@@ -46,6 +46,21 @@ describe('uploadFilename', () => {
expect(uploadFilename('IMG_0042.jpg', '../ev"il/')).toBe('..evilIMG_0042.jpg')
})
it('puts the uploader after the prefix, and leaves a file that already carries the prefix alone', () => {
expect(uploadFilename('IMG_0042.jpg', 'ipp_upload_', 'jane-smith')).toBe('ipp_upload_jane-smith,IMG_0042.jpg')
expect(uploadFilename('IMG_0042.jpg', '', 'jane-smith')).toBe('jane-smith,IMG_0042.jpg')
expect(uploadFilename('ipp_upload_IMG_0042.jpg', 'ipp_upload_', 'jane-smith')).toBe('ipp_upload_IMG_0042.jpg')
})
it('writes no header without a name, so an anonymous file is unchanged from 4.0', () => {
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', () => {
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')
})
it('stays within 254 characters with the extension intact', () => {
const result = uploadFilename('x'.repeat(400) + '.jpg', 'ipp_upload_') || ''
expect(result.startsWith('ipp_upload_')).toBe(true)
@@ -53,3 +68,36 @@ describe('uploadFilename', () => {
expect(result.length).toBeLessThanOrEqual(254)
})
})
describe('uploaderSlug', () => {
it('makes a lower-case hyphenated slug', () => {
expect(uploaderSlug('Jane Smith')).toBe('jane-smith')
expect(uploaderSlug(" O'Brien, Pat ")).toBe('o-brien-pat')
})
it('keeps letters and digits in any script', () => {
expect(uploaderSlug('Zoë Müller 2')).toBe('zoë-müller-2')
expect(uploaderSlug('山田 太郎')).toBe('山田-太郎')
})
it('never keeps a comma or an underscore', () => {
expect(uploaderSlug('Smith, Jane')).toBe('smith-jane')
expect(uploaderSlug('jane_smith')).toBe('jane-smith')
})
it('removes zero-width and direction-changing characters', () => {
expect(uploaderSlug('ja\u200Bne\u202E')).toBe('jane')
})
it('is empty for nothing usable or non-string input', () => {
expect(uploaderSlug('')).toBe('')
expect(uploaderSlug('!!!')).toBe('')
expect(uploaderSlug(undefined)).toBe('')
expect(uploaderSlug(['jane'])).toBe('')
})
it('is at most 32 characters, with no hyphen left at the end', () => {
expect(uploaderSlug('a'.repeat(40))).toBe('a'.repeat(32))
expect(uploaderSlug('a'.repeat(31) + ' b')).toBe('a'.repeat(31))
})
})
+9
View File
@@ -30,6 +30,15 @@ describe('buildNotification', () => {
expect(payload.message).toBe('ipp_upload_IMG_0042.jpg (3.0 MB) was uploaded to Holiday 2025.')
})
it('names the uploader in the message and the data, and leaves both plain without one', () => {
const named = buildNotification(share(), { ...file, name: 'ipp_upload_jane-smith,IMG_0042.jpg' }, asset, 'jane-smith')
expect(named.message).toBe('ipp_upload_jane-smith,IMG_0042.jpg (3.0 MB) was uploaded to Holiday 2025 by jane-smith.')
expect(named.data.uploader).toBe('jane-smith')
const anonymous = buildNotification(share(), file, asset)
expect(anonymous.message).toBe('ipp_upload_IMG_0042.jpg (3.0 MB) was uploaded to Holiday 2025.')
expect(anonymous.data.uploader).toBe('')
})
it('never carries the share key, slug or password', () => {
const payload = JSON.stringify(buildNotification(share({ key: 'secret-key', slug: 'secret-slug', password: 'hunter2' }), file, asset))
expect(payload).not.toContain('secret-key')