1- import sharp , { type ResizeOptions } from "sharp"
1+ import sharp from "sharp"
22
33/**
44 * Creates an image asset based off a file object
@@ -10,19 +10,17 @@ import sharp, { type ResizeOptions } from "sharp"
1010 * const id = // Load from database
1111 * save(id)
1212 */
13- export async function imageAsset ( file : File , sharpOptions ?: ResizeOptions ) {
14- const { buffer } = await sharp ( await file . arrayBuffer ( ) )
15- . resize ( 256 , 256 , {
16- fit : "contain" ,
17- ...sharpOptions ,
18- } )
19- . png ( )
20- . toBuffer ( )
21- . catch ( ( ) => {
22- throw new Error ( "Image asset failed to upload" )
23- } )
24-
25- return ( id : number ) => Bun . write ( `../data/assets/${ id } ` , buffer )
13+ export async function imageAsset ( file : File ) {
14+ try {
15+ const img = new Bun . Image ( file )
16+ . resize ( 256 , 256 , { fit : "inside" } )
17+ . png ( )
18+ return ( id : number ) => img . write ( `../data/assets/${ id } ` )
19+ } catch ( err ) {
20+ const e = err as Error
21+ console . error ( e )
22+ throw new Error ( "Image asset failed to upload" )
23+ }
2624}
2725
2826/**
@@ -35,19 +33,15 @@ export async function imageAsset(file: File, sharpOptions?: ResizeOptions) {
3533 * const id = // Load from database
3634 * save(id)
3735 */
38- export async function clothingAsset ( file : File , sharpOptions ?: ResizeOptions ) {
39- const { buffer } = await sharp ( await file . arrayBuffer ( ) )
40- . resize ( 585 , 559 , {
41- fit : "fill" ,
42- ...sharpOptions ,
43- } )
44- . png ( )
45- . toBuffer ( )
46- . catch ( ( ) => {
47- throw new Error ( "Image asset failed to upload" )
48- } )
49-
50- return ( id : number ) => Bun . write ( `../data/assets/${ id } ` , buffer )
36+ export async function clothingAsset ( file : File ) {
37+ try {
38+ const img = new Bun . Image ( file ) . resize ( 585 , 559 , { fit : "fill" } ) . png ( )
39+ return ( id : number ) => img . write ( `../data/assets/${ id } ` )
40+ } catch ( err ) {
41+ const e = err as Error
42+ console . error ( e )
43+ throw new Error ( "Clothing asset failed to upload" )
44+ }
5145}
5246
5347/**
@@ -60,19 +54,15 @@ export async function clothingAsset(file: File, sharpOptions?: ResizeOptions) {
6054 * const id = // Load from database
6155 * save(id)
6256 */
63- export async function thumbnail ( b : ArrayBuffer , sharpOptions ?: ResizeOptions ) {
64- const { buffer } = await sharp ( b )
65- . resize ( 420 , 420 , {
66- fit : "fill" ,
67- ...sharpOptions ,
68- } )
69- . webp ( ) // sorry avif, but webp is just magic (just here)
70- . toBuffer ( )
71- . catch ( ( ) => {
72- throw new Error ( "Thumbnail failed to upload" )
73- } )
74-
75- return ( id : number ) => Bun . write ( `../data/thumbnails/${ id } ` , buffer )
57+ export async function thumbnail ( b : ArrayBuffer ) {
58+ try {
59+ const img = new Bun . Image ( b ) . resize ( 420 , 420 , { fit : "fill" } ) . webp ( )
60+ return ( id : number ) => img . write ( `../data/thumbnails/${ id } ` )
61+ } catch ( err ) {
62+ const e = err as Error
63+ console . error ( e )
64+ throw new Error ( "Thumbnail failed to upload" )
65+ }
7666}
7767
7868const tShirtOpts = Object . freeze ( {
0 commit comments