Add image upload and edit functions
This commit is contained in:
9
src/utils/formatFileSize.ts
Normal file
9
src/utils/formatFileSize.ts
Normal file
@ -0,0 +1,9 @@
|
||||
export function formatFileSize(bytes: number): string {
|
||||
if (bytes < 1024) return `${bytes} B`;
|
||||
const kb = bytes / 1024;
|
||||
if (kb < 1024) return `${kb.toFixed(1)} KB`;
|
||||
const mb = kb / 1024;
|
||||
if (mb < 1024) return `${mb.toFixed(1)} MB`;
|
||||
const gb = mb / 1024;
|
||||
return `${gb.toFixed(2)} GB`;
|
||||
}
|
||||
22
src/utils/getImageBufferFromS3.ts
Normal file
22
src/utils/getImageBufferFromS3.ts
Normal file
@ -0,0 +1,22 @@
|
||||
import { s3 } from "@/lib/s3";
|
||||
import { GetObjectCommand } from "@aws-sdk/client-s3";
|
||||
import { Readable } from "stream";
|
||||
|
||||
export async function getImageBufferFromS3(fileKey: string, fileType?: string): Promise<Buffer> {
|
||||
// const type = fileType ? fileType.split("/")[1] : "webp";
|
||||
|
||||
const command = new GetObjectCommand({
|
||||
Bucket: "gaertan",
|
||||
Key: `original/${fileKey}.${fileType}`,
|
||||
});
|
||||
|
||||
const response = await s3.send(command);
|
||||
const stream = response.Body as Readable;
|
||||
|
||||
const chunks: Uint8Array[] = [];
|
||||
for await (const chunk of stream) {
|
||||
chunks.push(chunk);
|
||||
}
|
||||
|
||||
return Buffer.concat(chunks);
|
||||
}
|
||||
96
src/utils/uploadHelper.ts
Normal file
96
src/utils/uploadHelper.ts
Normal file
@ -0,0 +1,96 @@
|
||||
import crypto from 'crypto';
|
||||
|
||||
// export function generatePaletteName(tones: Tone[]): string {
|
||||
// const hexString = tones.map(t => t.hex.toLowerCase()).join('');
|
||||
// const hash = crypto.createHash('sha256').update(hexString).digest('hex');
|
||||
// return `palette-${hash.slice(0, 8)}`;
|
||||
// }
|
||||
|
||||
export function generateColorName(hex: string): string {
|
||||
const hash = crypto.createHash("sha256").update(hex.toLowerCase()).digest("hex");
|
||||
return `color-${hash.slice(0, 8)}`;
|
||||
}
|
||||
|
||||
// export function generateExtractColorName(hex: string, hue?: number, sat?: number, area?: number): string {
|
||||
// const data = `${hex.toLowerCase()}-${hue ?? 0}-${sat ?? 0}-${area ?? 0}`;
|
||||
// const hash = crypto.createHash("sha256").update(data).digest("hex");
|
||||
// return `extract-${hash.slice(0, 8)}`;
|
||||
// }
|
||||
|
||||
export function rgbToHex(rgb: number[]): string {
|
||||
return `#${rgb
|
||||
.map((val) => Math.round(val).toString(16).padStart(2, "0"))
|
||||
.join("")}`;
|
||||
}
|
||||
|
||||
export function argbToHex(argb: number): string {
|
||||
return `#${argb.toString(16).slice(2).padStart(6, "0")}`;
|
||||
}
|
||||
|
||||
// export function extractPaletteTones(
|
||||
// palette: TonalPalette, tones: number[] = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
|
||||
// ) {
|
||||
// return tones.map((t) => ({
|
||||
// tone: t,
|
||||
// hex: argbToHex(palette.tone(t)),
|
||||
// }));
|
||||
// }
|
||||
|
||||
// export async function upsertPalettes(tones: Tone[], imageId: string, type: string) {
|
||||
// const paletteName = generatePaletteName(tones);
|
||||
|
||||
// const existingPalette = await prisma.colorPalette.findFirst({
|
||||
// where: { name: paletteName },
|
||||
// include: { items: true },
|
||||
// });
|
||||
|
||||
// //
|
||||
// const palette = existingPalette ?? await prisma.colorPalette.create({
|
||||
// data: {
|
||||
// name: paletteName,
|
||||
// items: {
|
||||
// create: tones.map(tone => ({
|
||||
// tone: tone.tone,
|
||||
// hex: tone.hex,
|
||||
// }))
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
|
||||
// await prisma.imagePalette.upsert({
|
||||
// where: {
|
||||
// imageId_type: {
|
||||
// imageId,
|
||||
// type,
|
||||
// }
|
||||
// },
|
||||
// update: {
|
||||
// paletteId: palette.id
|
||||
// },
|
||||
// create: {
|
||||
// imageId,
|
||||
// paletteId: palette.id,
|
||||
// type,
|
||||
// }
|
||||
// });
|
||||
|
||||
|
||||
// // const newPalette = await prisma.colorPalette.create({
|
||||
// // data: {
|
||||
// // name: paletteName,
|
||||
// // type: type,
|
||||
// // items: {
|
||||
// // create: tones.map(t => ({
|
||||
// // tone: t.tone,
|
||||
// // hex: t.hex,
|
||||
// // })),
|
||||
// // },
|
||||
// // images: {
|
||||
// // connect: { id: imageId },
|
||||
// // },
|
||||
// // },
|
||||
// // include: { items: true },
|
||||
// // });
|
||||
|
||||
// return palette;
|
||||
// }
|
||||
Reference in New Issue
Block a user