jsx working
This commit is contained in:
parent
0f0bc6c4e5
commit
a68046ecd6
19 changed files with 814 additions and 458 deletions
39
src/converters/main.ts
Normal file
39
src/converters/main.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import { properties, convert } from "./sharp";
|
||||
|
||||
export async function mainConverter(
|
||||
inputFilePath: string,
|
||||
fileType: string,
|
||||
convertTo: string,
|
||||
targetPath: string,
|
||||
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
|
||||
options?: any,
|
||||
) {
|
||||
// Check if the fileType and convertTo are supported by the sharp converter
|
||||
if (properties.from.includes(fileType) && properties.to.includes(convertTo)) {
|
||||
// Use the sharp converter
|
||||
try {
|
||||
await convert(inputFilePath, fileType, convertTo, targetPath, options);
|
||||
console.log(
|
||||
`Converted ${inputFilePath} from ${fileType} to ${convertTo} successfully.`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`Failed to convert ${inputFilePath} from ${fileType} to ${convertTo}.`,
|
||||
error,
|
||||
);
|
||||
}
|
||||
} else {
|
||||
console.log(
|
||||
`The sharp converter does not support converting from ${fileType} to ${convertTo}.`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function possibleConversions(fileType: string) {
|
||||
// Check if the fileType is supported by the sharp converter
|
||||
if (properties.from.includes(fileType)) {
|
||||
return properties.to;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
37
src/converters/sharp.ts
Normal file
37
src/converters/sharp.ts
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import sharp from "sharp";
|
||||
|
||||
// declare possible conversions
|
||||
export const properties = {
|
||||
from: ["jpeg", "png", "webp", "gif", "avif", "tiff", "svg"],
|
||||
to: ["jpeg", "png", "webp", "gif", "avif", "tiff"],
|
||||
options: {
|
||||
svg: {
|
||||
scale: {
|
||||
description: "Scale the image up or down",
|
||||
type: "number",
|
||||
default: 1,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function convert(filePath: string, fileType: string, convertTo: string, targetPath: string, options?: any) {
|
||||
if (fileType === "svg") {
|
||||
const scale = options.scale || 1;
|
||||
const metadata = await sharp(filePath).metadata();
|
||||
|
||||
if (!metadata || !metadata.width || !metadata.height) {
|
||||
throw new Error("Could not get metadata from image");
|
||||
}
|
||||
|
||||
const newWidth = Math.round(metadata.width * scale)
|
||||
const newHeight = Math.round(metadata.height * scale)
|
||||
|
||||
return await sharp(filePath)
|
||||
.resize(newWidth, newHeight)
|
||||
.toFormat(convertTo)
|
||||
.toFile(targetPath);
|
||||
}
|
||||
|
||||
return await sharp(filePath).toFormat(convertTo).toFile(targetPath);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue