feat: implement hard delete, email notifications, watermark, and fix bulk download
This commit is contained in:
1 parent
8dd1e10fea
commit
4667615ed0
15 files changed
+369
-65
No files matched your search
@@ -55,6 +55,13 @@ export async function archiveEditorialAction(editorialId: string): Promise<void>
|
||||
redirect("/admin/redaktionen");
|
||||
}
|
||||
|
||||
export async function hardDeleteEditorialAction(editorialId: string): Promise<void> {
|
||||
await requireAdmin(new Headers(await headers()));
|
||||
await editorialService.hardDeleteEditorial(editorialId);
|
||||
revalidatePath("/admin/redaktionen");
|
||||
redirect("/admin/redaktionen");
|
||||
}
|
||||
|
||||
export async function suspendEditorialAction(editorialId: string): Promise<void> {
|
||||
const admin = await requireAdmin(new Headers(await headers()));
|
||||
await editorialService.transitionEditorialStatus({
|
||||
|
||||
@@ -22,20 +22,20 @@ type AlbumGridProps = {
|
||||
editorialId: string;
|
||||
grantAction: AlbumGridAction;
|
||||
revokeAction: AlbumGridAction;
|
||||
notifyAction: AlbumGridAction;
|
||||
};
|
||||
|
||||
function AlbumCard({
|
||||
album,
|
||||
editorialId,
|
||||
grantAction,
|
||||
revokeAction,
|
||||
}: {
|
||||
album: AlbumGridProps["albums"][0];
|
||||
type AlbumCardProps = {
|
||||
album: AlbumGridProps["albums"][number];
|
||||
editorialId: string;
|
||||
grantAction: AlbumGridAction;
|
||||
revokeAction: AlbumGridAction;
|
||||
}) {
|
||||
notifyAction: AlbumGridAction;
|
||||
};
|
||||
|
||||
function AlbumCard({ album, editorialId, grantAction, revokeAction, notifyAction }: AlbumCardProps) {
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const [notifyStatus, setNotifyStatus] = useState<"idle" | "pending" | "success" | "error">("idle");
|
||||
|
||||
const handleAction = (formData: FormData) => {
|
||||
startTransition(async () => {
|
||||
@@ -75,7 +75,7 @@ function AlbumCard({
|
||||
{album.albumName}
|
||||
</h2>
|
||||
<p className="text-sm text-gray-500 mb-4">{album.assetCount} Fotos</p>
|
||||
<div className="mt-auto">
|
||||
<div className="mt-auto space-y-2">
|
||||
<form action={handleAction}>
|
||||
<input name="immichAlbumId" type="hidden" value={album.id} />
|
||||
<button
|
||||
@@ -90,13 +90,39 @@ function AlbumCard({
|
||||
{isPending ? "Wird verarbeitet..." : album.isGranted ? "Zuweisung aufheben" : "Album zuweisen"}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
{album.isGranted && (
|
||||
<form action={(formData) => {
|
||||
setNotifyStatus("pending");
|
||||
notifyAction(editorialId, { ok: true, message: "" }, formData).then((res) => {
|
||||
setNotifyStatus(res.ok ? "success" : "error");
|
||||
setTimeout(() => setNotifyStatus("idle"), 3000);
|
||||
});
|
||||
}}>
|
||||
<input name="albumName" type="hidden" value={album.albumName} />
|
||||
<button
|
||||
type="submit"
|
||||
className={`w-full py-2 px-4 rounded-lg font-semibold text-sm transition-colors duration-200 border ${
|
||||
notifyStatus === "success" ? "bg-green-50 text-green-700 border-green-200" :
|
||||
notifyStatus === "error" ? "bg-red-50 text-red-700 border-red-200" :
|
||||
"bg-white text-gray-700 hover:bg-gray-50 border-gray-200 focus:ring-2 focus:ring-gray-200 focus:outline-none"
|
||||
}`}
|
||||
disabled={notifyStatus === "pending" || notifyStatus === "success"}
|
||||
>
|
||||
{notifyStatus === "pending" ? "Wird gesendet..." :
|
||||
notifyStatus === "success" ? "E-Mail gesendet!" :
|
||||
notifyStatus === "error" ? "Fehler beim Senden" :
|
||||
"Redaktion benachrichtigen"}
|
||||
</button>
|
||||
</form>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
);
|
||||
}
|
||||
|
||||
export function AlbumGrid({ albums, editorialId, grantAction, revokeAction }: AlbumGridProps) {
|
||||
export function AlbumGrid({ albums, editorialId, grantAction, revokeAction, notifyAction }: AlbumGridProps) {
|
||||
const [search, setSearch] = useState("");
|
||||
|
||||
const filteredAlbums = albums.filter((album) =>
|
||||
@@ -125,6 +151,7 @@ export function AlbumGrid({ albums, editorialId, grantAction, revokeAction }: Al
|
||||
editorialId={editorialId}
|
||||
grantAction={grantAction}
|
||||
revokeAction={revokeAction}
|
||||
notifyAction={notifyAction}
|
||||
/>
|
||||
))}
|
||||
</ul>
|
||||
|
||||
@@ -5,6 +5,10 @@ import { headers } from "next/headers";
|
||||
|
||||
import { AlbumGrantError, albumGrantService } from "@/modules/albums/grants";
|
||||
import { requireAdmin } from "@/modules/auth/principal";
|
||||
import { emailService } from "@/modules/email/service";
|
||||
import { db } from "@/db/client";
|
||||
import { editorialAccounts } from "@/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
|
||||
// Returned to the client form so failures surface as inline German text
|
||||
// instead of an opaque Next.js error-digest page (a thrown server action
|
||||
@@ -55,6 +59,37 @@ export async function revokeAlbumAction(
|
||||
return INITIAL_RESULT;
|
||||
}
|
||||
|
||||
export async function sendAlbumNotificationAction(
|
||||
editorialId: string,
|
||||
_prevState: AlbumActionResult,
|
||||
formData: FormData,
|
||||
): Promise<AlbumActionResult> {
|
||||
await getAdminPrincipal();
|
||||
const safeEditorialId = requiredId(editorialId);
|
||||
const albumName = formData.get("albumName");
|
||||
|
||||
if (typeof albumName !== "string" || !albumName) {
|
||||
return { ok: false, message: "Ungültiger Albumname." };
|
||||
}
|
||||
|
||||
try {
|
||||
const [editorial] = await db
|
||||
.select({ loginEmail: editorialAccounts.loginEmail })
|
||||
.from(editorialAccounts)
|
||||
.where(eq(editorialAccounts.id, safeEditorialId));
|
||||
|
||||
if (!editorial) {
|
||||
return { ok: false, message: "Redaktion nicht gefunden." };
|
||||
}
|
||||
|
||||
await emailService.sendNewAlbumNotification(editorial.loginEmail, albumName);
|
||||
return { ok: true, message: "Benachrichtigung erfolgreich gesendet." };
|
||||
} catch (error) {
|
||||
console.error("Failed to send album notification:", error);
|
||||
return { ok: false, message: "E-Mail konnte nicht gesendet werden." };
|
||||
}
|
||||
}
|
||||
|
||||
async function getAdminPrincipal() {
|
||||
return requireAdmin(new Headers(await headers()));
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import { db } from "@/db/client";
|
||||
import { editorialAccounts } from "@/db/schema";
|
||||
import { albumGrantService } from "@/modules/albums/grants";
|
||||
|
||||
import { grantAlbumAction, revokeAlbumAction } from "./actions";
|
||||
import { grantAlbumAction, revokeAlbumAction, sendAlbumNotificationAction } from "./actions";
|
||||
import { loadAssignableAlbums } from "./page-content";
|
||||
import { AlbumGrid } from "./AlbumGrid";
|
||||
|
||||
@@ -66,6 +66,7 @@ export default async function EditorialAlbumsPage({
|
||||
editorialId={editorial.id}
|
||||
grantAction={grantAlbumAction}
|
||||
revokeAction={revokeAlbumAction}
|
||||
notifyAction={sendAlbumNotificationAction}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -5,6 +5,8 @@ import { albumGrantService } from "@/modules/albums/grants";
|
||||
import { immichClient } from "@/modules/immich/client";
|
||||
import type { ImmichBinary } from "@/modules/immich/types";
|
||||
import sharp from "sharp";
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
type ThumbnailRouteContext = Readonly<{ params: Promise<{ assetId: string }> }>;
|
||||
|
||||
@@ -128,27 +130,48 @@ async function watermarkedThumbnailResponse(thumbnail: ImmichBinary, assetId: st
|
||||
}
|
||||
|
||||
const buffer = Buffer.concat(chunks);
|
||||
const metadata = await sharp(buffer).metadata();
|
||||
const width = metadata.width ?? 400;
|
||||
const height = metadata.height ?? 300;
|
||||
|
||||
const svgText = `
|
||||
<svg width="${width}" height="${height}" viewBox="0 0 ${width} ${height}" xmlns="http://www.w3.org/2000/svg">
|
||||
<text x="50%" y="50%" font-family="Arial, sans-serif" font-size="${Math.max(24, width / 8)}" font-weight="bold" fill="white" opacity="0.5" text-anchor="middle" alignment-baseline="middle" transform="rotate(-30 ${width/2} ${height/2})">
|
||||
PREVIEW
|
||||
</text>
|
||||
</svg>
|
||||
`;
|
||||
|
||||
const watermarked = await sharp(buffer)
|
||||
.composite([
|
||||
{
|
||||
input: Buffer.from(svgText),
|
||||
gravity: "center",
|
||||
},
|
||||
])
|
||||
.webp({ quality: 80 })
|
||||
|
||||
// Skaliere das Bild auf 480px Breite (wie vom User gewünscht)
|
||||
const targetWidth = 480;
|
||||
const resizedBuffer = await sharp(buffer)
|
||||
.resize({ width: targetWidth, withoutEnlargement: true })
|
||||
.toBuffer();
|
||||
|
||||
const resizedMetadata = await sharp(resizedBuffer).metadata();
|
||||
const width = resizedMetadata.width ?? targetWidth;
|
||||
const height = resizedMetadata.height ?? 300;
|
||||
|
||||
let watermarked = resizedBuffer;
|
||||
try {
|
||||
const logoPath = path.join(process.cwd(), "public", "brand", "logo-white.png");
|
||||
const logoBuffer = await fs.readFile(logoPath);
|
||||
|
||||
// Logo auf 60% der Bildbreite skalieren
|
||||
const logoTargetWidth = Math.round(width * 0.6);
|
||||
const resizedLogo = await sharp(logoBuffer)
|
||||
.resize({ width: logoTargetWidth, withoutEnlargement: true })
|
||||
.toBuffer();
|
||||
|
||||
const logoBase64 = resizedLogo.toString("base64");
|
||||
const logoMetadata = await sharp(resizedLogo).metadata();
|
||||
const logoWidth = logoMetadata.width ?? logoTargetWidth;
|
||||
const logoHeight = logoMetadata.height ?? (logoTargetWidth / 2);
|
||||
|
||||
const svgOverlay = `
|
||||
<svg width="${width}" height="${height}" xmlns="http://www.w3.org/2000/svg">
|
||||
<image href="data:image/png;base64,${logoBase64}" x="${(width - logoWidth) / 2}" y="${(height - logoHeight) / 2}" width="${logoWidth}" height="${logoHeight}" opacity="0.6"/>
|
||||
</svg>
|
||||
`;
|
||||
|
||||
watermarked = await sharp(resizedBuffer)
|
||||
.composite([{ input: Buffer.from(svgOverlay), gravity: "center" }])
|
||||
.webp({ quality: 80 })
|
||||
.toBuffer();
|
||||
} catch (err) {
|
||||
console.error("Watermark logo error:", err);
|
||||
// Fallback falls logo-white.png nicht existiert
|
||||
watermarked = await sharp(resizedBuffer).webp({ quality: 80 }).toBuffer();
|
||||
}
|
||||
|
||||
const responseHeaders = new Headers({
|
||||
"cache-control": "private, max-age=86400, stale-while-revalidate=604800",
|
||||
|
||||
@@ -31,7 +31,28 @@ export async function POST(request: NextRequest) {
|
||||
const trimmedPurpose = purpose.trim();
|
||||
|
||||
const grantedAlbumIds = await albumGrantService.getGrantedAlbumIds(principal.editorialId);
|
||||
const grantedSet = new Set(grantedAlbumIds);
|
||||
|
||||
// Fetch all granted albums to build a map of assetId -> validAlbumId
|
||||
const assetToGrantedAlbum = new Map<string, string>();
|
||||
const grantedAlbums = await Promise.all(
|
||||
grantedAlbumIds.map(async (albumId) => {
|
||||
try {
|
||||
return await immichClient.getAlbum(albumId);
|
||||
} catch (err) {
|
||||
console.error("Failed to fetch granted album", albumId, err);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
for (const album of grantedAlbums) {
|
||||
if (!album) continue;
|
||||
for (const asset of album.assets) {
|
||||
if (!assetToGrantedAlbum.has(asset.id)) {
|
||||
assetToGrantedAlbum.set(asset.id, album.id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const downloadsToProcess: { assetId: string, downloadId: string, filename: string, intentKind: "new_usage" | "repeat" }[] = [];
|
||||
|
||||
@@ -53,8 +74,8 @@ export async function POST(request: NextRequest) {
|
||||
if (!asset) continue;
|
||||
|
||||
// Find the first album the editorial has access to
|
||||
const validAlbum = asset.albums.find((a: any) => grantedSet.has(a.id));
|
||||
if (!validAlbum) continue; // Editorial does not have access
|
||||
const validAlbumId = assetToGrantedAlbum.get(assetId);
|
||||
if (!validAlbumId) continue; // Editorial does not have access
|
||||
|
||||
// Check for prior usages to avoid double charging
|
||||
const priorUsages = await usageService.listPriorUsages(principal.editorialId, assetId);
|
||||
@@ -70,7 +91,7 @@ export async function POST(request: NextRequest) {
|
||||
const newUsage = await usageService.createNewUsage({
|
||||
editorialId: principal.editorialId,
|
||||
assetId,
|
||||
albumId: validAlbum.id,
|
||||
albumId: validAlbumId,
|
||||
purpose: trimmedPurpose,
|
||||
reference: undefined,
|
||||
idempotencyKey,
|
||||
|
||||
Reference in new issue
Block a user