feat(website): photos upright, without metadata, in responsive sizes
Every upload is turned upright, stripped of its metadata (EXIF, GPS), kept at most 2400 px wide and saved at 480/960/1600 px in WebP and JPEG; the pages pick the size with <picture> and srcset. Every file counts towards storage; a photo that can't be read is refused. sharp 0.35 (prebuilt, no build step on the servers). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,7 @@ import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import request from 'supertest';
|
||||
import sharp from 'sharp';
|
||||
import { loadDemo } from '../src/demo/demoChurch.js';
|
||||
import { hashToken } from '../src/lib/crypto.js';
|
||||
import { dimensions, sniff } from '../src/admin/media.js';
|
||||
@@ -181,17 +182,45 @@ describe('uploads and storage (§10, decision 12)', () => {
|
||||
assert.equal(sniff(Buffer.from('<svg>')), null);
|
||||
});
|
||||
|
||||
it('stores an upload, refuses what isn\'t a photo, and stops when storage is full', async () => {
|
||||
const { as, db, dir } = await signedIn('editor');
|
||||
const up = await as.upload('/media?alt=The%20hall', png);
|
||||
it('keeps a photo upright and without its location, in responsive sizes; refuses what isn\'t one', async () => {
|
||||
const { as, dir, app } = await signedIn('editor');
|
||||
// A 2000 × 1200 phone photo with its GPS position in the metadata.
|
||||
const photo = await sharp({ create: { width: 2000, height: 1200, channels: 3, background: '#1d5c63' } })
|
||||
.jpeg().withExifMerge({ IFD3: { GPSLatitudeRef: 'S', GPSLatitude: '25/1 44/1 46/1' } }).toBuffer();
|
||||
assert.ok((await sharp(photo).metadata()).exif, 'the test photo has EXIF');
|
||||
const up = await as.upload('/media?alt=The%20hall', photo);
|
||||
assert.equal(up.status, 200);
|
||||
assert.equal(up.body.width, 640);
|
||||
assert.ok(fs.existsSync(path.join(dir, up.body.filename)));
|
||||
const notPhoto = await as.upload('/media', Buffer.from('<svg onload=alert(1)>'));
|
||||
assert.equal(notPhoto.status, 415);
|
||||
// Full: the media library already holds the included 5 GB.
|
||||
assert.equal(up.body.width, 2000);
|
||||
assert.deepEqual(up.body.sizes.map((x: { w: number }) => x.w), [480, 960, 1600]);
|
||||
const file = path.join(dir, up.body.filename);
|
||||
assert.equal((await sharp(fs.readFileSync(file)).metadata()).exif, undefined, 'no metadata kept');
|
||||
const files = fs.readdirSync(path.dirname(file));
|
||||
assert.equal(files.length, 7, 'the photo and 3 sizes in WebP and JPEG');
|
||||
const total = files.reduce((t, n) => t + fs.statSync(path.join(path.dirname(file), n)).size, 0);
|
||||
assert.equal(up.body.bytes, total, 'every file counts towards storage');
|
||||
|
||||
// The site gets srcsets.
|
||||
const about = (await as.get('/pages')).body.find((p: { kind: string }) => p.kind === 'about').id;
|
||||
await as.put(`/pages/${about}`, { imageId: up.body.id });
|
||||
await as.post('/publish');
|
||||
const content = (await request(app).get('/api/v1/site/content')).body;
|
||||
const img = content.snapshot.pages.find((p: { kind: string }) => p.kind === 'about').image;
|
||||
assert.match(img.srcset.webp, /-480\.webp 480w, .*-960\.webp 960w, .*-1600\.webp 1600w, .*\.jpg 2000w$/);
|
||||
|
||||
const big = await sharp({ create: { width: 4000, height: 3000, channels: 3, background: '#e0a458' } }).png().toBuffer();
|
||||
const b = await as.upload('/media', big);
|
||||
assert.equal(b.body.width, 2400, 'kept at most 2400 wide');
|
||||
assert.equal(b.body.height, 1800);
|
||||
assert.equal((await as.upload('/media', Buffer.from('<svg onload=alert(1)>'))).status, 415);
|
||||
assert.equal((await as.upload('/media', png)).status, 415, 'a PNG header with nothing behind it');
|
||||
});
|
||||
|
||||
it('stops uploads when storage is full', async () => {
|
||||
const { as, db } = await signedIn('editor');
|
||||
const photo = await sharp({ create: { width: 800, height: 600, channels: 3, background: '#ffffff' } }).jpeg().toBuffer();
|
||||
// The media library already holds the included 5 GB.
|
||||
await db.mediaAsset.create({ data: { filename: 'big.jpg', mime: 'image/jpeg', bytes: 5 * 1024 ** 3 } });
|
||||
const full = await as.upload('/media', png);
|
||||
const full = await as.upload('/media', photo);
|
||||
assert.equal(full.status, 409);
|
||||
assert.equal(full.body.error.code, 'STORAGE_FULL');
|
||||
assert.equal((await as.get('/media')).body.storage.full, true);
|
||||
|
||||
Reference in New Issue
Block a user