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 *.tsbuildinfo
next-env.d.ts 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, "id" SERIAL NOT NULL,
"userId" INTEGER NOT NULL, "userId" INTEGER NOT NULL,
"name" VARCHAR(64) NOT NULL, "name" VARCHAR(64) NOT NULL,
"qrCodeUrl" TEXT NOT NULL,
"studioUrl" TEXT NOT NULL,
"images" TEXT[], "images" TEXT[],
"tags" 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, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
CONSTRAINT "miis_pkey" PRIMARY KEY ("id") CONSTRAINT "miis_pkey" PRIMARY KEY ("id")

View file

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

View file

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