@@ -2,7 +2,7 @@ import { createHash } from 'node:crypto';
2
2
import { createWriteStream } from 'node:fs' ;
3
3
import { mkdir , mkdtemp , open , readFile , writeFile } from 'node:fs/promises' ;
4
4
import { tmpdir } from 'node:os' ;
5
- import { sep } from 'node:path' ;
5
+ import { extname , sep } from 'node:path' ;
6
6
import { Readable } from 'node:stream' ;
7
7
import { finished } from 'node:stream/promises' ;
8
8
@@ -56,9 +56,10 @@ async function createDerivative(
56
56
) {
57
57
const outputDir = getSettings ( ) . outputDir ;
58
58
const outputPath = getSettings ( ) . outputPath ;
59
+ const ext = extname ( filename ) ;
59
60
const fn = `${ createHash ( 'md5' )
60
61
. update ( JSON . stringify ( { ...target , filename, focalPoint } ) )
61
- . digest ( 'hex' ) } .jpg`;
62
+ . digest ( 'hex' ) } .${ ext === '.png' ? 'png' : ' jpg' } `;
62
63
63
64
const derivative = `${ outputDir } /${ fn } ` ;
64
65
await mkdir ( outputDir , { recursive : true } ) ;
@@ -81,7 +82,9 @@ async function createDerivative(
81
82
fit : 'cover' ,
82
83
position : sharp . strategy . attention ,
83
84
} ) ;
84
- const resized = await pipeline . toFormat ( 'jpg' ) . toBuffer ( ) ;
85
+ const resized = await pipeline
86
+ . toFormat ( ext === '.png' ? 'png' : 'jpg' )
87
+ . toBuffer ( ) ;
85
88
await writeFile ( derivative , resized ) ;
86
89
}
87
90
return `${ outputPath } /${ fn } ` ;
0 commit comments