diff --git a/workspace/website/src/lib/content/media.ts b/workspace/website/src/lib/content/media.ts index 4605f9fd..5b686dab 100644 --- a/workspace/website/src/lib/content/media.ts +++ b/workspace/website/src/lib/content/media.ts @@ -6,16 +6,40 @@ const ROOT = `${process.cwd()}/static/uploads`; export function assemble(buffer: Buffer, output: `/${string}`) { const dir = ROOT + output.slice(0, output.lastIndexOf('/')); - if (/\.(mp4)$/.test(output)) { - mkdirSync(dir, { recursive: true }); - writeFileSync(ROOT + output, buffer); - return '/uploads' + output; - } + switch (true) { + case /\.(mp4)$/.test(output): { + mkdirSync(dir, { recursive: true }); + writeFileSync(ROOT + output, buffer); + return '/uploads' + output; + } + case /\.(jpe?g)$/.test(output): { + mkdirSync(dir, { recursive: true }); + const path = output.replace(/\.[^/.]+$/, '.jpeg'); + + const image = sharp(buffer); + image.metadata().then(({ orientation = 0, width, height }) => { + const original = orientation >= 5 ? height : width; + image.resize(Math.min(original || 1080, 1080)); + image.jpeg({ mozjpeg: true }).toFile(ROOT + path); + }); + + return '/uploads' + path; + } + case /\.(png|svg)$/.test(output): { + mkdirSync(dir, { recursive: true }); + const path = output.replace(/\.[^/.]+$/, '.png'); - if (!/\.(jpe?g|png|svg)$/.test(output)) return; + const image = sharp(buffer); + image.metadata().then(({ orientation = 0, width, height }) => { + const original = orientation >= 5 ? height : width; + image.resize(Math.min(original || 1080, 1080)); + image.png({ quality: 85 }).toFile(ROOT + path); + }); - mkdirSync(dir, { recursive: true }); - const webp = sharp(buffer).webp(); - const path = output.replace(/\.[^/.]+$/, '.webp'); - return webp.toFile(ROOT + path), '/uploads' + path; + return '/uploads' + path; + } + + default: + return; + } }