@@ -20,8 +20,11 @@ import type { AppContext } from '../context.js';
2020import { decryptServerFile } from './download.js' ;
2121import { fileEtag , type StreamableFile } from './stream.js' ;
2222
23- export const THUMB_PX = 320 ;
24- const QUALITY = 72 ;
23+ /** 320 px = grid tile; 1600 px = on-screen preview in the viewer (instead of a multi-MB original). */
24+ export const THUMB_SIZES = [ 320 , 1600 ] as const ;
25+ export type ThumbSize = ( typeof THUMB_SIZES ) [ number ] ;
26+ export const THUMB_PX : ThumbSize = 320 ;
27+ const QUALITY : Record < ThumbSize , number > = { 320 : 72 , 1600 : 80 } ;
2528/** Don't decode absurdly large sources just for a tile (falls back to the icon). */
2629const MAX_SOURCE_BYTES = 64 * 1024 * 1024 ;
2730const THUMB_MIME = / ^ i m a g e \/ ( j p e g | p j p e g | p n g | w e b p | g i f | a v i f | t i f f | h e i c | h e i f ) $ / i;
@@ -56,11 +59,15 @@ export function thumbCacheDir(ctx: AppContext): string {
5659 return resolve ( ctx . env . THUMB_CACHE_PATH ?? join ( dirname ( resolve ( ctx . env . STORAGE_PATH ) ) , 'thumbs' ) ) ;
5760}
5861
59- /** Cache file for one (file, content version). The version part changes when the bytes change. */
60- function cachePath ( ctx : AppContext , file : ThumbSource ) : string | null {
62+ export function parseThumbSize ( v : unknown ) : ThumbSize {
63+ return Number ( v ) === 1600 ? 1600 : 320 ;
64+ }
65+
66+ /** Cache file for one (file, content version, size). The version part changes with the bytes. */
67+ function cachePath ( ctx : AppContext , file : ThumbSource , px : ThumbSize ) : string | null {
6168 if ( ! / ^ [ a - z 0 - 9 ] + $ / i. test ( file . id ) ) return null ; // ids are cuids; refuse anything path-like
6269 const ver = fileEtag ( file ) . replace ( / " / g, '' ) ;
63- return join ( thumbCacheDir ( ctx ) , file . id . slice ( 0 , 2 ) , `${ file . id } -${ ver } ` ) ;
70+ return join ( thumbCacheDir ( ctx ) , file . id . slice ( 0 , 2 ) , `${ file . id } -${ ver } - ${ px } ` ) ;
6471}
6572
6673function encryptWith ( dek : Buffer , data : Buffer ) : Buffer {
@@ -91,22 +98,26 @@ async function readSource(ctx: AppContext, file: ThumbSource): Promise<Buffer> {
9198 return Buffer . concat ( parts ) ;
9299}
93100
94- async function render ( source : Buffer ) : Promise < Buffer > {
101+ async function render ( source : Buffer , px : ThumbSize ) : Promise < Buffer > {
95102 return sharp ( source , { failOn : 'none' , limitInputPixels : 120_000_000 , animated : false } )
96103 . rotate ( ) // honour EXIF orientation (phone photos)
97- . resize ( THUMB_PX , THUMB_PX , { fit : 'inside' , withoutEnlargement : true } )
98- . webp ( { quality : QUALITY , effort : 4 } )
104+ . resize ( px , px , { fit : 'inside' , withoutEnlargement : true } )
105+ . webp ( { quality : QUALITY [ px ] , effort : 4 } )
99106 . toBuffer ( ) ;
100107}
101108
102109/**
103110 * Return the WebP thumbnail for an image file, generating and caching it on first request.
104111 * `null` = not an image we can thumbnail (unsupported format, too big, corrupt): show the icon.
105112 */
106- export async function getThumbnail ( ctx : AppContext , file : ThumbSource ) : Promise < Buffer | null > {
113+ export async function getThumbnail (
114+ ctx : AppContext ,
115+ file : ThumbSource ,
116+ px : ThumbSize = THUMB_PX ,
117+ ) : Promise < Buffer | null > {
107118 if ( file . encMode === 'ZK' || ! isThumbable ( file . name , file . mimeType ) ) return null ;
108119 if ( Number ( file . sizeBytes ) > MAX_SOURCE_BYTES || Number ( file . sizeBytes ) === 0 ) return null ;
109- const path = cachePath ( ctx , file ) ;
120+ const path = cachePath ( ctx , file , px ) ;
110121 if ( ! path ) return null ;
111122 const isPlain = file . encMode === 'PUBLIC' ;
112123 const dek = isPlain ? null : file . wrappedKey ? unwrapKey ( file . wrappedKey , ctx . env . masterKey ) : null ;
@@ -127,7 +138,7 @@ export async function getThumbnail(ctx: AppContext, file: ThumbSource): Promise<
127138 const job = withSlot ( async ( ) => {
128139 let out : Buffer | null = null ;
129140 try {
130- out = await render ( await readSource ( ctx , file ) ) ;
141+ out = await render ( await readSource ( ctx , file ) , px ) ;
131142 } catch {
132143 out = null ;
133144 }
@@ -166,7 +177,7 @@ export async function sweepThumbnails(ctx: AppContext): Promise<number> {
166177 continue ;
167178 }
168179 const entries = names
169- . map ( ( n ) => ( { n, m : / ^ ( [ a - z 0 - 9 ] + ) - ( [ A - Z a - z 0 - 9 _ - ] + ) $ / i. exec ( n ) } ) )
180+ . map ( ( n ) => ( { n, m : / ^ ( [ a - z 0 - 9 ] + ) - ( . + ) - ( \d + ) $ / i. exec ( n ) } ) )
170181 . filter ( ( e ) => e . m || e . n . endsWith ( '.tmp' ) ) ;
171182 const ids = [ ...new Set ( entries . filter ( ( e ) => e . m ) . map ( ( e ) => e . m ! [ 1 ] ! ) ) ] ;
172183 const rows = ids . length
@@ -177,7 +188,8 @@ export async function sweepThumbnails(ctx: AppContext): Promise<number> {
177188 : [ ] ;
178189 const current = new Map ( rows . map ( ( r ) => [ r . id , fileEtag ( r ) . replace ( / " / g, '' ) ] ) ) ;
179190 for ( const e of entries ) {
180- const stale = ! e . m || current . get ( e . m [ 1 ] ! ) !== e . m [ 2 ] ;
191+ const stale =
192+ ! e . m || current . get ( e . m [ 1 ] ! ) !== e . m [ 2 ] || ! THUMB_SIZES . includes ( Number ( e . m [ 3 ] ) as ThumbSize ) ;
181193 if ( ! stale ) continue ;
182194 await rm ( join ( root , shard , e . n ) , { force : true } ) . catch ( ( ) => undefined ) ;
183195 removed += 1 ;
@@ -193,13 +205,15 @@ export async function sendThumbnail(
193205 req : import ( 'fastify' ) . FastifyRequest ,
194206 reply : import ( 'fastify' ) . FastifyReply ,
195207 file : ThumbSource ,
208+ px : ThumbSize = THUMB_PX ,
196209) {
197- const etag = `"t${ THUMB_PX } -${ fileEtag ( file ) . replace ( / " / g, '' ) } "` ;
210+ const etag = `"t${ px } -${ fileEtag ( file ) . replace ( / " / g, '' ) } "` ;
198211 const cache = 'private, max-age=31536000, immutable' ;
212+ reply . header ( 'Cross-Origin-Resource-Policy' , 'same-site' ) ; // see sendStoredFile
199213 if ( req . headers [ 'if-none-match' ] === etag ) {
200214 return reply . code ( 304 ) . header ( 'ETag' , etag ) . header ( 'Cache-Control' , cache ) . send ( ) ;
201215 }
202- const buf = await getThumbnail ( ctx , file ) ;
216+ const buf = await getThumbnail ( ctx , file , px ) ;
203217 if ( ! buf ) {
204218 return reply . code ( 415 ) . header ( 'Cache-Control' , 'private, max-age=600' ) . send ( { error : 'No thumbnail' } ) ;
205219 }
0 commit comments