Create Album endpoint

This commit is contained in:
Jorge Vargas 2025-03-08 22:24:22 -06:00
parent c3fca425cb
commit 967a6d1587
15 changed files with 388 additions and 85 deletions

21
src/utils/form.ts Normal file
View file

@ -0,0 +1,21 @@
import slugify from 'slugify'
export const Status = (status: number, statusText?: string) => new Response(null, { status, statusText })
export const slug = (text: string) => slugify(text, { lower: true, strict: true })
export function formToObject(formData: FormData) {
const object: Record<string, any> = {}
formData.forEach((value, key) => {
// Reflect.has in favor of: object.hasOwnProperty(key)
if (!Reflect.has(object, key)) {
object[key] = value
return
}
if (!Array.isArray(object[key])) {
object[key] = [object[key]]
}
object[key].push(value)
})
return object
}

29
src/utils/img.ts Normal file
View file

@ -0,0 +1,29 @@
import path from 'node:path'
import fs from 'node:fs/promises'
import sharp from 'sharp'
function colorToHex(color: number) {
const hexadecimal = color.toString(16)
return hexadecimal.length === 1 ? '0' + hexadecimal : hexadecimal
}
function convertRGBtoHex(red: number, green: number, blue: number) {
return '#' + colorToHex(red) + colorToHex(green) + colorToHex(blue)
}
export async function writeImg(file: File, folder: string, id: number | string) {
const pathString = path.join('/var/www/soc_img/img', folder)
const fullPath = path.join(pathString, `${id}.png`)
const fileArray = Buffer.from(await file.arrayBuffer())
await fs.writeFile(fullPath, fileArray)
return fullPath
}
export async function getImgColor(filePath: string) {
const { dominant } = await sharp(filePath).stats()
const { r, g, b } = dominant
return convertRGBtoHex(r, g, b)
}