mirror of
https://github.com/yusufipk/OpenFrame.git
synced 2026-09-11 09:36:08 +00:00
A comment held one image, and the paste handler took the first item off the clipboard and dropped the rest. Reviewing a cut usually means several screenshots about the same moment, which meant one comment per screenshot or one screenshot and a paragraph describing the others. Editing a comment could not attach anything at all: the edit box had no paste handler, no file picker and no way to remove what was already there. A comment now carries up to five images, in the composer, in a reply and in the editor. One paste stages every image on the clipboard, the file picker takes a multiple selection, and a drop lands on whichever editor is open. Over the cap the extras are refused out loud rather than dropped quietly. A single image still fills the width; several tile into a grid, and either opens full screen on click. The images move into their own table. `comments.imageUrl` stays and follows the first of them, so a reader that has not been updated keeps working, and the migration copies the existing attachments across so the new table is complete from the first read. Every path that resolves a URL back to a comment now asks the new table: R2 cleanup, the orphan sweep, the storage accounting and the reference checks that decide whether an object can be deleted. Left on the old column they would have treated images two through five as unreferenced and swept them. Detaching an image while editing only breaks the link. The file stays in R2 and in the assets pane, which is where it is deleted from and where its bytes are already billed.
147 lines
4.5 KiB
TypeScript
147 lines
4.5 KiB
TypeScript
import { NextRequest } from 'next/server';
|
|
import { DeleteObjectCommand } from '@aws-sdk/client-s3';
|
|
import { auth } from '@/lib/auth';
|
|
import { db } from '@/lib/db';
|
|
import { apiErrors, successResponse } from '@/lib/api-response';
|
|
import { rateLimit } from '@/lib/rate-limit';
|
|
import { r2Client, R2_BUCKET_NAME } from '@/lib/r2';
|
|
import { logError } from '@/lib/logger';
|
|
|
|
type RouteParams = { params: Promise<{ feedbackId: string }> };
|
|
|
|
function extractImageFilenameFromProxyUrl(url: string): string | null {
|
|
const match = url.match(/^\/api\/upload\/image\/([0-9a-f-]+\.[a-z0-9]+)$/i);
|
|
return match ? match[1] : null;
|
|
}
|
|
|
|
// DELETE /api/admin/feedback/[feedbackId]
|
|
export async function DELETE(request: NextRequest, { params }: RouteParams) {
|
|
try {
|
|
const limited = await rateLimit(request, 'mutate');
|
|
if (limited) return limited;
|
|
|
|
const session = await auth();
|
|
if (!session?.user?.isAdmin) {
|
|
return apiErrors.forbidden('Admin access required');
|
|
}
|
|
|
|
const { feedbackId } = await params;
|
|
const userFeedbackDelegate = (
|
|
db as unknown as {
|
|
userFeedback?: {
|
|
findUnique: (args: unknown) => Promise<{
|
|
id: string;
|
|
screenshotUrl: string | null;
|
|
screenshots?: Array<{ url: string }>;
|
|
} | null>;
|
|
delete: (args: { where: { id: string } }) => Promise<{ id: string }>;
|
|
findFirst: (args: {
|
|
where: { screenshotUrl: string };
|
|
select: { id: true };
|
|
}) => Promise<{ id: string } | null>;
|
|
};
|
|
userFeedbackScreenshot?: {
|
|
findFirst: (args: {
|
|
where: { url: string };
|
|
select: { id: true };
|
|
}) => Promise<{ id: string } | null>;
|
|
};
|
|
}
|
|
).userFeedback;
|
|
const userFeedbackScreenshotDelegate = (
|
|
db as unknown as {
|
|
userFeedbackScreenshot?: {
|
|
findFirst: (args: {
|
|
where: { url: string };
|
|
select: { id: true };
|
|
}) => Promise<{ id: string } | null>;
|
|
};
|
|
}
|
|
).userFeedbackScreenshot;
|
|
|
|
if (!userFeedbackDelegate) {
|
|
return apiErrors.internalError('Feedback model is not available yet');
|
|
}
|
|
|
|
let feedbackRecord = await userFeedbackDelegate
|
|
.findUnique({
|
|
where: { id: feedbackId },
|
|
include: {
|
|
screenshots: {
|
|
select: { url: true },
|
|
},
|
|
},
|
|
})
|
|
.catch((error) => {
|
|
const message = error instanceof Error ? error.message : '';
|
|
if (message.includes('Unknown field `screenshots`')) return null;
|
|
throw error;
|
|
});
|
|
|
|
if (!feedbackRecord) {
|
|
feedbackRecord = await userFeedbackDelegate.findUnique({
|
|
where: { id: feedbackId },
|
|
});
|
|
}
|
|
|
|
if (!feedbackRecord) {
|
|
return apiErrors.notFound('Feedback');
|
|
}
|
|
|
|
const mediaUrls = new Set<string>();
|
|
if (feedbackRecord.screenshotUrl) mediaUrls.add(feedbackRecord.screenshotUrl);
|
|
(feedbackRecord.screenshots ?? []).forEach((item) => {
|
|
if (item.url) mediaUrls.add(item.url);
|
|
});
|
|
|
|
await userFeedbackDelegate.delete({
|
|
where: { id: feedbackId },
|
|
});
|
|
|
|
await Promise.all(
|
|
Array.from(mediaUrls).map(async (url) => {
|
|
const filename = extractImageFilenameFromProxyUrl(url);
|
|
if (!filename) return;
|
|
|
|
const [commentReferenced, feedbackReferenced, feedbackAttachmentReferenced] =
|
|
await Promise.all([
|
|
db.commentImage.findFirst({
|
|
where: { url },
|
|
select: { id: true },
|
|
}),
|
|
userFeedbackDelegate.findFirst({
|
|
where: { screenshotUrl: url },
|
|
select: { id: true },
|
|
}),
|
|
userFeedbackScreenshotDelegate
|
|
? userFeedbackScreenshotDelegate.findFirst({
|
|
where: { url },
|
|
select: { id: true },
|
|
})
|
|
: Promise.resolve(null),
|
|
]);
|
|
|
|
if (commentReferenced || feedbackReferenced || feedbackAttachmentReferenced) return;
|
|
|
|
await r2Client
|
|
.send(
|
|
new DeleteObjectCommand({
|
|
Bucket: R2_BUCKET_NAME,
|
|
Key: `images/${filename}`,
|
|
})
|
|
)
|
|
.catch(() => undefined);
|
|
})
|
|
);
|
|
|
|
return successResponse({ id: feedbackId });
|
|
} catch (error) {
|
|
const message = error instanceof Error ? error.message : '';
|
|
if (message.includes('Record to delete does not exist')) {
|
|
return apiErrors.notFound('Feedback');
|
|
}
|
|
logError('Error deleting feedback:', error);
|
|
return apiErrors.internalError('Failed to delete feedback');
|
|
}
|
|
}
|