mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
Centralize duplicated getToken() + Bearer header logic into a single formatHeaders() helper in lib/api.ts. When no token exists, the Authorization header is omitted entirely instead of sending an empty Bearer token, which breaks forward-auth proxies like Authelia behind Caddy. Changes: - Add formatHeaders() with try-catch around localStorage access - Replace 20+ duplicated getToken() definitions across tool components - Migrate all call sites including file-details, settings, change-password - Update tests to verify header omission on empty token Based on the fix proposed by @jules2689 in #6, with improvements: file placement (lib/api.ts vs components), localStorage error handling, simplified truthiness check, and complete call-site coverage. Co-Authored-By: Julian Nadeau <julian@jnadeau.ca>
105 lines
3.4 KiB
TypeScript
105 lines
3.4 KiB
TypeScript
import { Loader2 } from "lucide-react";
|
|
import { useState } from "react";
|
|
import { formatHeaders } from "@/lib/api";
|
|
import { useFileStore } from "@/stores/file-store";
|
|
|
|
interface DuplicateGroup {
|
|
files: Array<{ filename: string; similarity: number }>;
|
|
}
|
|
|
|
interface DuplicateResult {
|
|
totalImages: number;
|
|
duplicateGroups: DuplicateGroup[];
|
|
uniqueImages: number;
|
|
}
|
|
|
|
export function FindDuplicatesSettings() {
|
|
const { files, processing, error, setProcessing, setError } = useFileStore();
|
|
const [result, setResult] = useState<DuplicateResult | null>(null);
|
|
|
|
const handleProcess = async () => {
|
|
if (files.length < 2) return;
|
|
|
|
setProcessing(true);
|
|
setError(null);
|
|
setResult(null);
|
|
|
|
try {
|
|
const formData = new FormData();
|
|
for (const file of files) {
|
|
formData.append("file", file);
|
|
}
|
|
|
|
const res = await fetch("/api/v1/tools/find-duplicates", {
|
|
method: "POST",
|
|
headers: formatHeaders(),
|
|
body: formData,
|
|
});
|
|
|
|
if (!res.ok) {
|
|
const body = await res.json().catch(() => ({}));
|
|
throw new Error(body.error || `Failed: ${res.status}`);
|
|
}
|
|
|
|
const data: DuplicateResult = await res.json();
|
|
setResult(data);
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : "Detection failed");
|
|
} finally {
|
|
setProcessing(false);
|
|
}
|
|
};
|
|
|
|
const hasFiles = files.length >= 2;
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<p className="text-xs text-muted-foreground">
|
|
Upload 2 or more images to find near-duplicates using perceptual hashing.
|
|
</p>
|
|
|
|
<button
|
|
type="button"
|
|
data-testid="find-duplicates-submit"
|
|
onClick={handleProcess}
|
|
disabled={!hasFiles || processing}
|
|
className="w-full py-2.5 rounded-lg bg-primary text-primary-foreground font-medium disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
|
|
>
|
|
{processing && <Loader2 className="h-4 w-4 animate-spin" />}
|
|
{processing ? "Scanning..." : `Scan ${files.length} Images`}
|
|
</button>
|
|
|
|
{error && <p className="text-xs text-red-500">{error}</p>}
|
|
|
|
{result && (
|
|
<div className="space-y-3">
|
|
<div className="p-3 rounded-lg bg-muted text-xs space-y-1">
|
|
<p className="text-foreground">Total images: {result.totalImages}</p>
|
|
<p className="text-foreground">Unique images: {result.uniqueImages}</p>
|
|
<p className="text-foreground">Duplicate groups: {result.duplicateGroups.length}</p>
|
|
</div>
|
|
|
|
{result.duplicateGroups.length === 0 ? (
|
|
<p className="text-xs text-muted-foreground">No duplicates found.</p>
|
|
) : (
|
|
result.duplicateGroups.map((group, gi) => (
|
|
<div
|
|
key={group.files.map((f) => f.filename).join(",")}
|
|
className="p-2 rounded border border-border space-y-1"
|
|
>
|
|
<p className="text-xs font-medium text-foreground">Group {gi + 1}</p>
|
|
{group.files.map((f) => (
|
|
<div key={f.filename} className="flex justify-between text-xs">
|
|
<span className="text-foreground truncate">{f.filename}</span>
|
|
<span className="text-muted-foreground shrink-0 ml-2">{f.similarity}%</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
))
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|