diff --git a/package-lock.json b/package-lock.json index ee8a8f4..df425ed 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,6 @@ "": { "name": "file-converter", "version": "0.1.0", - "hasInstallScript": true, "dependencies": { "@prisma/client": "^6.19.3", "docx": "^9.7.1", @@ -16,6 +15,7 @@ "express": "^5.2.1", "express-rate-limit": "^8.6.1", "file-type": "^22.0.1", + "icojs": "^1.0.0", "mammoth": "^1.12.0", "markdown-it": "^15.0.0", "multer": "^2.2.0", @@ -47,6 +47,12 @@ "url": "https://github.com/sponsors/Borewit" } }, + "node_modules/@canvas/image-data": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/@canvas/image-data/-/image-data-1.1.0.tgz", + "integrity": "sha512-QdObRRjRbcXGmM1tmJ+MrHcaz1MftF2+W7YI+MsphnsCrmtyfS0d5qJbk0MeSbUeyM/jCb0hmnkXPsy026L7dA==", + "license": "MIT" + }, "node_modules/@emnapi/core": { "version": "2.0.0-alpha.3", "resolved": "https://registry.npmjs.org/@emnapi/core/-/core-2.0.0-alpha.3.tgz", @@ -2056,6 +2062,12 @@ "integrity": "sha512-iD3898SR7sWVRHbiQv+sHUtHnMvC1o3nW5rAcqnq3uOn07DSAppZYUkIGslDz6gXC7HfunPe7YVBgoEJASPcHA==", "license": "MIT" }, + "node_modules/bmp-ts": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/bmp-ts/-/bmp-ts-1.0.9.tgz", + "integrity": "sha512-cTEHk2jLrPyi+12M3dhpEbnnPOsaZuq7C45ylbbQIiWgDFZq4UVYPEY5mlqjvsj/6gJv9qX5sa+ebDzLXT28Vw==", + "license": "MIT" + }, "node_modules/body-parser": { "version": "2.3.0", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-2.3.0.tgz", @@ -2690,6 +2702,33 @@ } } }, + "node_modules/decode-bmp": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/decode-bmp/-/decode-bmp-0.2.1.tgz", + "integrity": "sha512-NiOaGe+GN0KJqi2STf24hfMkFitDUaIoUU3eKvP/wAbLe8o6FuW5n/x7MHPR0HKvBokp6MQY/j7w8lewEeVCIA==", + "license": "MIT", + "dependencies": { + "@canvas/image-data": "^1.0.0", + "to-data-view": "^1.1.0" + }, + "engines": { + "node": ">=8.6.0" + } + }, + "node_modules/decode-ico": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/decode-ico/-/decode-ico-0.4.1.tgz", + "integrity": "sha512-69NZfbKIzux1vBOd31al3XnMnH+2mqDhEgLdpygErm4d60N+UwA5Sq5WFjmEDQzumgB9fElojGwWG0vybVfFmA==", + "license": "MIT", + "dependencies": { + "@canvas/image-data": "^1.0.0", + "decode-bmp": "^0.2.0", + "to-data-view": "^1.1.0" + }, + "engines": { + "node": ">=8.6" + } + }, "node_modules/deepmerge-ts": { "version": "7.1.5", "resolved": "https://registry.npmjs.org/deepmerge-ts/-/deepmerge-ts-7.1.5.tgz", @@ -3637,6 +3676,22 @@ "url": "https://opencollective.com/express" } }, + "node_modules/icojs": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/icojs/-/icojs-1.0.0.tgz", + "integrity": "sha512-Ksd2dSS9PlN/KX2iymij7cBCa5aWOhqlZ0GMamRInCHMBlWYxyQQMfr/aURywO7psO49w8vrJGnHkySfxVKmVw==", + "license": "MIT", + "dependencies": { + "bmp-ts": "^1.0.9", + "decode-ico": "^0.4.1", + "file-type": "^22.0.1", + "jpeg-js": "^0.4.4", + "pngjs": "^7.0.0" + }, + "engines": { + "node": ">=22.22.2" + } + }, "node_modules/iconv-lite": { "version": "0.7.3", "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.7.3.tgz", @@ -3778,6 +3833,12 @@ "jiti": "lib/jiti-cli.mjs" } }, + "node_modules/jpeg-js": { + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/jpeg-js/-/jpeg-js-0.4.4.tgz", + "integrity": "sha512-WZzeDOEtTOBK4Mdsar0IqEU5sMr3vSV2RqkAIzUEV2BHnUfKGyswWFPFwK5EeDo93K3FohSHbLAjj0s1Wzd+dg==", + "license": "BSD-3-Clause" + }, "node_modules/js-yaml": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-4.3.0.tgz", @@ -5048,6 +5109,15 @@ } } }, + "node_modules/pngjs": { + "version": "7.0.0", + "resolved": "https://registry.npmjs.org/pngjs/-/pngjs-7.0.0.tgz", + "integrity": "sha512-LKWqWJRhstyYo9pGvgor/ivk2w94eSjE3RGVuzLGlr3NmD8bf7RcYGze1mNdEHRP6TRP6rMuDHk5t44hnTRyow==", + "license": "MIT", + "engines": { + "node": ">=14.19.0" + } + }, "node_modules/postcss": { "version": "8.5.24", "resolved": "https://registry.npmjs.org/postcss/-/postcss-8.5.24.tgz", @@ -5937,6 +6007,12 @@ "node": ">=14.14" } }, + "node_modules/to-data-view": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/to-data-view/-/to-data-view-1.1.0.tgz", + "integrity": "sha512-1eAdufMg6mwgmlojAx3QeMnzB/BTVp7Tbndi3U7ftcT2zCZadjxkkmLmd97zmaxWi+sgGcgWrokmpEoy0Dn0vQ==", + "license": "MIT" + }, "node_modules/to-regex-range": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", diff --git a/package.json b/package.json index 2443c8e..b22a841 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "express": "^5.2.1", "express-rate-limit": "^8.6.1", "file-type": "^22.0.1", + "icojs": "^1.0.0", "mammoth": "^1.12.0", "markdown-it": "^15.0.0", "multer": "^2.2.0", diff --git a/src/converters/ico.js b/src/converters/ico.js new file mode 100644 index 0000000..aacd30b --- /dev/null +++ b/src/converters/ico.js @@ -0,0 +1,45 @@ +import fs from 'node:fs/promises'; +import sharp from 'sharp'; +import { decodeIco, encodeIco } from 'icojs'; +import { register } from './registry.js'; +import { sharpFormatName, buildFormatOptions } from './image.js'; + +const IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'webp', 'gif', 'tiff', 'avif']; +export const DEFAULT_ICON_SIZE = 256; + +export async function encodeIcoFromInput(input, size = DEFAULT_ICON_SIZE) { + const pngBuffer = await sharp(input).resize(size, size, { fit: 'contain' }).png().toBuffer(); + return Buffer.from(await encodeIco([{ buffer: pngBuffer }])); +} + +async function decodeIcoLargestImage(inputPath) { + const buffer = await fs.readFile(inputPath); + const images = await decodeIco(buffer, 'image/png'); + return images.reduce((a, b) => (a.width * a.height >= b.width * b.height ? a : b)); +} + +export function registerIcoConverter() { + for (const format of IMAGE_FORMATS) { + register({ + family: 'image', + sourceFormat: 'ico', + targetFormat: format, + convert: async (inputPath, outputPath, { quality } = {}) => { + const largest = await decodeIcoLargestImage(inputPath); + await sharp(Buffer.from(largest.buffer)) + .toFormat(sharpFormatName(format), buildFormatOptions(format, quality)) + .toFile(outputPath); + }, + }); + + register({ + family: 'image', + sourceFormat: format, + targetFormat: 'ico', + convert: async (inputPath, outputPath, { iconSize } = {}) => { + const icoBuffer = await encodeIcoFromInput(inputPath, iconSize ?? DEFAULT_ICON_SIZE); + await fs.writeFile(outputPath, icoBuffer); + }, + }); + } +} diff --git a/src/converters/image.js b/src/converters/image.js index 7895de6..85115b0 100644 --- a/src/converters/image.js +++ b/src/converters/image.js @@ -3,7 +3,7 @@ import { register } from './registry.js'; const IMAGE_FORMATS = ['jpg', 'jpeg', 'png', 'webp', 'gif', 'tiff', 'avif']; -function sharpFormatName(format) { +export function sharpFormatName(format) { return format === 'jpg' ? 'jpeg' : format; } diff --git a/test/converters/ico.test.js b/test/converters/ico.test.js new file mode 100644 index 0000000..1014690 --- /dev/null +++ b/test/converters/ico.test.js @@ -0,0 +1,99 @@ +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; +import fs from 'node:fs/promises'; +import path from 'node:path'; +import os from 'node:os'; +import sharp from 'sharp'; +import { registerIcoConverter, encodeIcoFromInput, DEFAULT_ICON_SIZE } from '../../src/converters/ico.js'; +import { resolve, listTargetFormats } from '../../src/converters/registry.js'; +import { detectInputMime } from '../../src/mime.js'; + +let tmpDir; + +beforeAll(async () => { + registerIcoConverter(); + tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'converter-ico-')); +}); + +afterAll(async () => { + await fs.rm(tmpDir, { recursive: true, force: true }); +}); + +describe('ICO converter registration', () => { + it('registers ico as both a source and a target for every image format', () => { + expect(listTargetFormats('ico')).toContain('png'); + expect(listTargetFormats('ico')).toContain('jpg'); + expect(listTargetFormats('png')).toContain('ico'); + expect(listTargetFormats('jpg')).toContain('ico'); + }); +}); + +describe('encodeIcoFromInput', () => { + it('defaults to a 256x256 icon when no size is given', async () => { + const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.png'); + const icoBuffer = await encodeIcoFromInput(inputPath); + + const outputPath = path.join(tmpDir, 'default-size.ico'); + await fs.writeFile(outputPath, icoBuffer); + const detected = await detectInputMime(outputPath); + expect(detected.mime).toBe('image/x-icon'); + }); + + it('produces an icon at the requested size', async () => { + const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.png'); + const icoBuffer = await encodeIcoFromInput(inputPath, 32); + + const { decodeIco } = await import('icojs'); + const [image] = await decodeIco(icoBuffer, 'image/png'); + expect(image.width).toBe(32); + expect(image.height).toBe(32); + }); +}); + +describe('ico -> image conversion', () => { + it('converts an ICO fixture to PNG, picking the largest embedded image', async () => { + const source = path.join(import.meta.dirname, '..', 'fixtures', 'sample.png'); + const icoPath = path.join(tmpDir, 'multi.ico'); + + const small = await sharp(source).resize(16, 16).png().toBuffer(); + const large = await sharp(source).resize(48, 48).png().toBuffer(); + const { encodeIco } = await import('icojs'); + await fs.writeFile(icoPath, Buffer.from(await encodeIco([{ buffer: small }, { buffer: large }]))); + + const outputPath = path.join(tmpDir, 'from-ico.png'); + const entry = resolve('ico', 'png'); + await entry.convert(icoPath, outputPath); + + const meta = await sharp(outputPath).metadata(); + expect(meta.width).toBe(48); + expect(meta.height).toBe(48); + }); +}); + +describe('image -> ico conversion', () => { + it('converts a PNG fixture to a valid ICO at the requested size', async () => { + const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.png'); + const outputPath = path.join(tmpDir, 'output.ico'); + const entry = resolve('png', 'ico'); + + await entry.convert(inputPath, outputPath, { iconSize: 48 }); + + const detected = await detectInputMime(outputPath); + expect(detected.mime).toBe('image/x-icon'); + + const { decodeIco } = await import('icojs'); + const [image] = await decodeIco(await fs.readFile(outputPath), 'image/png'); + expect(image.width).toBe(48); + }); + + it('defaults to 256px when no iconSize is given', async () => { + const inputPath = path.join(import.meta.dirname, '..', 'fixtures', 'sample.png'); + const outputPath = path.join(tmpDir, 'output-default.ico'); + const entry = resolve('png', 'ico'); + + await entry.convert(inputPath, outputPath); + + const { decodeIco } = await import('icojs'); + const [image] = await decodeIco(await fs.readFile(outputPath), 'image/png'); + expect(image.width).toBe(DEFAULT_ICON_SIZE); + }); +});