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),
"description" varchar,
"tags" text[],
"filepath" varchar(256),
"created_by" varchar(255) NOT NULL,
"created_at" timestamp with time zone DEFAULT CURRENT_TIMESTAMP NOT NULL,
"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",
"version": "7",
"dialect": "postgresql",
@@ -146,6 +146,12 @@
"primaryKey": false,
"notNull": false
},
"filepath": {
"name": "filepath",
"type": "varchar(256)",
"primaryKey": false,
"notNull": false
},
"created_by": {
"name": "created_by",
"type": "varchar(255)",

View File

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

View File

@@ -4,6 +4,9 @@ import { env } from "@/env";
import { type NextRequest, NextResponse } from "next/server";
import fs from "fs";
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) {
const session = await getServerSession();
@@ -32,6 +35,9 @@ export async function POST(request: NextRequest) {
const filePath = `${id}${extension}`;
fs.writeFileSync(path.resolve(env.UPLOAD_PATH, filePath), buffer);
await db.update(images).set({ filePath }).where(eq(images.id, id));
return NextResponse.json({
success: true,
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) {
const { id } = params(request.url) as { id?: string };
if (!id) {
return NextResponse.next();
}
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({
orderBy: (images, { desc }) => [desc(images.createdAt)],
});
return trending.map(t => {
return {
id: t.id,
title: t.title,
description: t.description,
tags: t.tags,
url: `${env.IMAGE_BASE_URL}/${t.id}`,
};
}) ?? null;
return (
trending.map((t) => {
return {
id: t.id,
title: t.title,
description: t.description,
tags: t.tags,
url: `${env.IMAGE_BASE_URL}/${t.filePath}`,
};
}) ?? null
);
}),
create: protectedProcedure
.input(z.object(imageCreateType))

View File

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