mirror of
https://github.com/jorgev259/soc_site-astro.git
synced 2025-06-29 07:57:41 +00:00
Album edit endpoint
This commit is contained in:
parent
d3581eaeef
commit
6cef84a358
24 changed files with 936 additions and 76 deletions
22
src/utils/consts.ts
Normal file
22
src/utils/consts.ts
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
export enum DownloadProvider {
|
||||
MEGA = 'MEGA',
|
||||
MEDIAFIRE = 'MEDIAFIRE',
|
||||
RANOZ = 'RANOZ',
|
||||
TERABOX = 'TERABOX',
|
||||
MIRROR = 'MIRROR'
|
||||
}
|
||||
|
||||
export enum StoreProviders {
|
||||
AMAZON = 'amazon',
|
||||
AMAZON_JP = 'amazon_jp',
|
||||
PLAY_ASIA = 'play_asia',
|
||||
CD_JAPAN = 'cd_japan',
|
||||
SPOTIFY = 'spotify',
|
||||
GOOGLE_PLAY = 'google_play',
|
||||
STEAM = 'steam',
|
||||
MORA = 'mora',
|
||||
APPLE_MUSIC = 'apple_music',
|
||||
OTOTOY = 'ototoy',
|
||||
BANDCAMP = 'bandcamp',
|
||||
DEEZER = 'deezer'
|
||||
}
|
||||
|
|
@ -1,9 +1,10 @@
|
|||
import slugify from 'slugify'
|
||||
import { decode } from 'decode-formdata'
|
||||
|
||||
export const Status = (status: number, statusText?: string) => new Response(null, { status, statusText })
|
||||
export const slug = (text: string) => slugify(text, { lower: true, strict: true })
|
||||
|
||||
function formToObject(formData: FormData) {
|
||||
export function formToObject(formData: FormData) {
|
||||
const object: Record<string, any> = {}
|
||||
for (const entry of formData.entries()) {
|
||||
const [key, value] = entry
|
||||
|
|
@ -13,13 +14,13 @@ function formToObject(formData: FormData) {
|
|||
return object
|
||||
}
|
||||
|
||||
export async function parseForm(request: Request) {
|
||||
const formData = await request.formData()
|
||||
const formObject = formToObject(formData)
|
||||
const { data: dataInput, ...rest } = formObject
|
||||
export async function parseForm(formData: FormData) {
|
||||
const formObject = decode(formData, {
|
||||
arrays: ['animations', 'classifications', 'categories', 'platforms', 'related', 'games', 'downloads', 'discs'],
|
||||
dates: ['releaseDate']
|
||||
})
|
||||
|
||||
const data = JSON.parse(dataInput)
|
||||
return { ...data, ...rest }
|
||||
return formObject
|
||||
}
|
||||
|
||||
export function getRandom<T>(array: T[]): T {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
import path from 'node:path'
|
||||
import fs from 'node:fs/promises'
|
||||
import sharp from 'sharp'
|
||||
import type { PrismaClient } from '@prisma/client/extension'
|
||||
|
||||
function colorToHex(color: number) {
|
||||
const hexadecimal = color.toString(16)
|
||||
|
|
@ -16,11 +17,27 @@ export async function writeImg(file: File, folder: string, id: number | string)
|
|||
const fullPath = path.join(pathString, `${id}.png`)
|
||||
|
||||
const fileArray = Buffer.from(await file.arrayBuffer())
|
||||
await fs.writeFile(fullPath, fileArray)
|
||||
await fs.mkdir(pathString, { recursive: true })
|
||||
|
||||
if (await fs.stat(fullPath).catch(() => false)) {
|
||||
await fs.rm(fullPath)
|
||||
}
|
||||
|
||||
await fs.writeFile(fullPath, fileArray)
|
||||
return fullPath
|
||||
}
|
||||
|
||||
export async function handleImg(file: File, folder: string, id: number | string) {
|
||||
const coverPath = await writeImg(file, folder, id)
|
||||
const headerColor = await getImgColor(coverPath)
|
||||
return headerColor
|
||||
}
|
||||
|
||||
export async function handleCover(file: File, folder: string, id: number | string, tx: PrismaClient) {
|
||||
const headerColor = await handleImg(file, folder, id)
|
||||
await tx.albums.update({ where: { id: id }, data: { headerColor } })
|
||||
}
|
||||
|
||||
export async function getImgColor(filePath: string) {
|
||||
const { dominant } = await sharp(filePath).stats()
|
||||
const { r, g, b } = dominant
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { PrismaClient } from '@prisma/client'
|
||||
|
||||
const prismaClient = new PrismaClient()
|
||||
const prismaClient = new PrismaClient({ log: ['error'] })
|
||||
|
||||
export default prismaClient
|
||||
export default prismaClient
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue