try to add image mapping

This commit is contained in:
jane 2022-04-11 13:33:16 -04:00
parent 49904865eb
commit 8ed0fa5de2
7 changed files with 71 additions and 3 deletions

4
.gitignore vendored
View File

@ -31,3 +31,7 @@ yarn-error.log*
# vercel
.vercel
# prisma data
/prisma/migrations
/prisma/borders.db

View File

@ -1,7 +1,14 @@
import { getServerSession } from "next-auth";
import { getSession } from "next-auth/react";
import prisma from "./prisma";
export const getBorderById = async (id) => {
return await prisma.borderImage.findFirst({
where: {
id: parseInt(id)
}
});
}
export const getUserBorders = async (req) => {
const session = await getSession({ req });
if (!session) {

View File

@ -6,7 +6,8 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"ingest": "node util/ingest.js"
},
"dependencies": {
"@next-auth/prisma-adapter": "^1.0.3",

View File

@ -14,7 +14,7 @@ export default NextAuth({
callbacks: {
async session({ session, token, user }) {
session.user.id = user.id;
console.log(JSON.stringify(user));
// console.log(JSON.stringify(user));
return session;
},
},

16
pages/api/border/[id].js Normal file
View File

@ -0,0 +1,16 @@
import { getBorderById } from "../../../lib/borders";
export default function handler(req, res) {
const id = req.query.id;
console.log(id);
getBorderById(id).then((result) => {
const imageName = result?.imageName ?? "default.png";
console.log(result, imageName);
return res.status(200).json(result);
// return res.redirect(301, `/images/${imageName}`)
});
}

Binary file not shown.

40
util/ingest.js Normal file
View File

@ -0,0 +1,40 @@
console.log("a");
const fs = require('fs');
const path = require('path');
const Prisma = require("@prisma/client");
const prisma = new Prisma.PrismaClient();
console.log("processing new border images");
const cwd = process.cwd();
const folder = path.join(cwd, cwd.includes("util") ? ".." : "", "public/images");
const list = fs.readdirSync(folder);
(async () => {
let numAdded = 0;
for (let item of list) {
// console.log(item);
const result = await prisma.borderImage.findFirst(
{
where: {
imageName: item
}
}
);
if (!result) {
const added = await prisma.borderImage.create(
{
data: {
imageName: item
},
}
);
numAdded++;
console.log(added);
}
}
console.log(`Processed ${numAdded} new images.`);
})();