Fixed URL of image

This commit is contained in:
Fergal Moran
2024-09-18 14:21:38 +01:00
parent ed630b139f
commit 5ef620f55b
8 changed files with 31 additions and 12 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -18,6 +18,7 @@ CREATE TABLE IF NOT EXISTS "images" (
"title" varchar(256), "title" varchar(256),
"description" varchar, "description" varchar,
"tags" text[], "tags" text[],
"filepath" varchar(256),
"created_by" varchar(255) NOT NULL, "created_by" varchar(255) NOT NULL,
"created_at" timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL, "created_at" timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
"updated_at" timestamp with time zone "updated_at" timestamp with time zone

View File

@@ -1,5 +1,5 @@
{ {
"id": "c81af722-c72f-46d8-9457-c8ece996a058", "id": "f7541b9a-140c-4795-a672-4d0c80b56ef1",
"prevId": "00000000-0000-0000-0000-000000000000", "prevId": "00000000-0000-0000-0000-000000000000",
"version": "7", "version": "7",
"dialect": "postgresql", "dialect": "postgresql",
@@ -146,6 +146,12 @@
"primaryKey": false, "primaryKey": false,
"notNull": false "notNull": false
}, },
"filepath": {
"name": "filepath",
"type": "varchar(256)",
"primaryKey": false,
"notNull": false
},
"created_by": { "created_by": {
"name": "created_by", "name": "created_by",
"type": "varchar(255)", "type": "varchar(255)",

View File

@@ -5,8 +5,8 @@
{ {
"idx": 0, "idx": 0,
"version": "7", "version": "7",
"when": 1726515307439, "when": 1726662474693,
"tag": "0000_watery_carnage", "tag": "0000_wandering_steve_rogers",
"breakpoints": true "breakpoints": true
} }
] ]

View File

@@ -4,6 +4,9 @@ import { env } from "@/env";
import { type NextRequest, NextResponse } from "next/server"; import { type NextRequest, NextResponse } from "next/server";
import fs from "fs"; import fs from "fs";
import path from "path"; import path from "path";
import { db } from "@/server/db";
import { images } from "@/server/db/schema";
import { eq } from "drizzle-orm";
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {
const session = await getServerSession(); const session = await getServerSession();
@@ -32,6 +35,9 @@ export async function POST(request: NextRequest) {
const filePath = `${id}${extension}`; const filePath = `${id}${extension}`;
fs.writeFileSync(path.resolve(env.UPLOAD_PATH, filePath), buffer); fs.writeFileSync(path.resolve(env.UPLOAD_PATH, filePath), buffer);
await db.update(images).set({ filePath }).where(eq(images.id, id));
return NextResponse.json({ return NextResponse.json({
success: true, success: true,
url: env.NEXT_PUBLIC_SITE_URL + `/i/${filePath}`, url: env.NEXT_PUBLIC_SITE_URL + `/i/${filePath}`,

View File

@@ -34,5 +34,8 @@ const params = (url: string) => {
}; };
export async function middleware(request: NextRequest, response: NextResponse) { export async function middleware(request: NextRequest, response: NextResponse) {
const { id } = params(request.url) as { id?: string }; const { id } = params(request.url) as { id?: string };
if (!id) {
return NextResponse.next();
}
return NextResponse.rewrite(new URL(`/uploads/${id}`, request.url)); return NextResponse.rewrite(new URL(`/uploads/${id}`, request.url));
} }

View File

@@ -18,15 +18,17 @@ export const imageRouter = createTRPCRouter({
const trending = await ctx.db.query.images.findMany({ const trending = await ctx.db.query.images.findMany({
orderBy: (images, { desc }) => [desc(images.createdAt)], orderBy: (images, { desc }) => [desc(images.createdAt)],
}); });
return trending.map(t => { return (
return { trending.map((t) => {
id: t.id, return {
title: t.title, id: t.id,
description: t.description, title: t.title,
tags: t.tags, description: t.description,
url: `${env.IMAGE_BASE_URL}/${t.id}`, tags: t.tags,
}; url: `${env.IMAGE_BASE_URL}/${t.filePath}`,
}) ?? null; };
}) ?? null
);
}), }),
create: protectedProcedure create: protectedProcedure
.input(z.object(imageCreateType)) .input(z.object(imageCreateType))

View File

@@ -21,6 +21,7 @@ export const images = createTable("images", {
title: varchar("title", { length: 256 }), title: varchar("title", { length: 256 }),
description: varchar("description"), description: varchar("description"),
tags: text("tags").array(), tags: text("tags").array(),
filePath: varchar("filepath", { length: 256 }),
createdById: varchar("created_by", { length: 255 }) createdById: varchar("created_by", { length: 255 })
.notNull() .notNull()
.references(() => users.id), .references(() => users.id),