feat: add mii data properties to database and move uploads directory paths

This commit is contained in:
trafficlunar 2025-04-05 17:13:34 +01:00
parent 5de6e924f2
commit 37af539911
5 changed files with 25 additions and 31 deletions

2
.gitignore vendored
View file

@ -41,4 +41,4 @@ yarn-error.log*
*.tsbuildinfo
next-env.d.ts
public/uploads/
public/mii/

View file

@ -1,3 +0,0 @@
-- AlterTable
ALTER TABLE "miis" ALTER COLUMN "qrCodeUrl" DROP NOT NULL,
ALTER COLUMN "studioUrl" DROP NOT NULL;

View file

@ -45,10 +45,12 @@ CREATE TABLE "miis" (
"id" SERIAL NOT NULL,
"userId" INTEGER NOT NULL,
"name" VARCHAR(64) NOT NULL,
"qrCodeUrl" TEXT NOT NULL,
"studioUrl" TEXT NOT NULL,
"images" TEXT[],
"tags" TEXT[],
"firstName" TEXT NOT NULL,
"lastName" TEXT NOT NULL,
"islandName" TEXT NOT NULL,
"allowedCopying" BOOLEAN NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "miis_pkey" PRIMARY KEY ("id")

View file

@ -61,13 +61,16 @@ model Session {
}
model Mii {
id Int @id @default(autoincrement())
userId Int
name String @db.VarChar(64)
qrCodeUrl String?
studioUrl String?
images String[]
tags String[]
id Int @id @default(autoincrement())
userId Int
name String @db.VarChar(64)
images String[]
tags String[]
firstName String
lastName String
islandName String
allowedCopying Boolean
createdAt DateTime @default(now())

View file

@ -13,7 +13,7 @@ import { nameSchema, tagsSchema } from "@/lib/schemas";
import Mii from "@/utils/mii.js/mii";
import TomodachiLifeMii from "@/utils/tomodachi-life-mii";
const uploadsDirectory = path.join(process.cwd(), "public", "uploads");
const uploadsDirectory = path.join(process.cwd(), "public", "mii");
export async function POST(request: Request) {
const session = await auth();
@ -76,14 +76,17 @@ export async function POST(request: Request) {
userId: Number(session.user.id),
name,
tags,
firstName: tomodachiLifeMii.firstName,
lastName: tomodachiLifeMii.lastName,
islandName: tomodachiLifeMii.islandName,
allowedCopying: mii.allowCopying,
},
});
// Ensure directories exist
await Promise.all([
fs.mkdir(path.join(uploadsDirectory, "studio"), { recursive: true }),
fs.mkdir(path.join(uploadsDirectory, "qr-code"), { recursive: true }),
]);
const miiUploadsDirectory = path.join(uploadsDirectory, miiRecord.id.toString());
await fs.mkdir(miiUploadsDirectory, { recursive: true });
// Download the image of the Mii
let studioBuffer: Buffer;
@ -107,7 +110,7 @@ export async function POST(request: Request) {
try {
// Compress and upload
const studioWebpBuffer = await sharp(studioBuffer).webp({ quality: 85 }).toBuffer();
const studioFileLocation = path.join(uploadsDirectory, "studio", `${miiRecord.id}.webp`);
const studioFileLocation = path.join(miiUploadsDirectory, "mii.webp");
await fs.writeFile(studioFileLocation, studioWebpBuffer);
@ -124,22 +127,11 @@ export async function POST(request: Request) {
// Compress and upload
const codeWebpBuffer = await sharp(codeBuffer).webp({ quality: 85 }).toBuffer();
const codeFileLocation = path.join(uploadsDirectory, "qr-code", `${miiRecord.id}.webp`);
const codeFileLocation = path.join(miiUploadsDirectory, "qr-code.webp");
await fs.writeFile(codeFileLocation, codeWebpBuffer);
// todo: upload user images
// Update database to use images
await prisma.mii.update({
where: {
id: miiRecord.id,
},
data: {
studioUrl: studioFileLocation,
qrCodeUrl: codeFileLocation,
},
});
return Response.json({ success: true, id: miiRecord.id });
} catch (error) {
await prisma.mii.delete({ where: { id: miiRecord.id } });