This commit is contained in:
ct
2025-07-15 20:03:10 +08:00
parent b54e4f2092
commit 096f515f58
15 changed files with 1161 additions and 3 deletions

View File

@@ -63,3 +63,7 @@ AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false
VITE_APP_NAME="${APP_NAME}"
# Basic Auth for Admin Tools
BASIC_AUTH_USERNAME=admin
BASIC_AUTH_PASSWORD=changeme

View File

@@ -0,0 +1,124 @@
<?php
namespace App\Console\Commands;
use App\Helpers\FirstParty\ImageHash\ImageHashService;
use App\Models\MemeMedia;
use Illuminate\Console\Command;
class FindDuplicateImages extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:find-duplicate-images {--threshold=5 : Hamming distance threshold for duplicates}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Find duplicate images using Hamming distance comparison of WebP hashes';
private ImageHashService $imageHashService;
public function __construct(ImageHashService $imageHashService)
{
parent::__construct();
$this->imageHashService = $imageHashService;
}
/**
* Execute the console command.
*/
public function handle()
{
$threshold = (int) $this->option('threshold');
$records = MemeMedia::whereNotNull('image_hash')
->whereNotNull('webp_url')
->get(['id', 'name', 'image_hash', 'webp_url']);
if ($records->isEmpty()) {
$this->info('No records with image hashes found. Run app:generate-image-hashes first.');
return;
}
$this->info("Checking {$records->count()} records for duplicates with threshold: {$threshold}");
$duplicates = [];
$processed = [];
foreach ($records as $record) {
if (in_array($record->id, $processed)) {
continue;
}
$similarRecords = [];
foreach ($records as $compareRecord) {
if ($record->id === $compareRecord->id || in_array($compareRecord->id, $processed)) {
continue;
}
$distance = $this->imageHashService->calculateHammingDistance(
$record->image_hash,
$compareRecord->image_hash
);
if ($distance <= $threshold) {
$similarRecords[] = [
'id' => $compareRecord->id,
'name' => $compareRecord->name,
'distance' => $distance,
'url' => $compareRecord->webp_url,
];
$processed[] = $compareRecord->id;
}
}
if (! empty($similarRecords)) {
$duplicates[] = [
'original' => [
'id' => $record->id,
'name' => $record->name,
'url' => $record->webp_url,
],
'duplicates' => $similarRecords,
];
$processed[] = $record->id;
}
}
if (empty($duplicates)) {
$this->info('No duplicates found.');
return;
}
$this->info('Found '.count($duplicates).' duplicate groups:');
foreach ($duplicates as $group) {
$this->newLine();
$this->line("Original: [{$group['original']['id']}] {$group['original']['name']}");
$this->line("URL: {$group['original']['url']}");
foreach ($group['duplicates'] as $duplicate) {
$this->line(" → [{$duplicate['id']}] {$duplicate['name']} (distance: {$duplicate['distance']})");
$this->line(" URL: {$duplicate['url']}");
}
}
$this->newLine();
$this->info('Total duplicate groups: '.count($duplicates));
$totalDuplicates = array_sum(array_map(function ($group) {
return count($group['duplicates']);
}, $duplicates));
$this->info("Total duplicate records: {$totalDuplicates}");
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace App\Console\Commands;
use App\Helpers\FirstParty\ImageHash\ImageHashService;
use App\Models\MemeMedia;
use Illuminate\Console\Command;
class GenerateImageHashes extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:generate-image-hashes {--force : Force regeneration of existing hashes}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate image hashes for existing WebP URLs in MemeMedia records';
private ImageHashService $imageHashService;
public function __construct(ImageHashService $imageHashService)
{
parent::__construct();
$this->imageHashService = $imageHashService;
}
/**
* Execute the console command.
*/
public function handle()
{
$force = $this->option('force');
$query = MemeMedia::query();
if (! $force) {
$query->whereNull('image_hash');
}
$records = $query->whereNotNull('webp_url')->get();
if ($records->isEmpty()) {
$this->info('No records found to process.');
return;
}
$this->info("Processing {$records->count()} records...");
$progressBar = $this->output->createProgressBar($records->count());
$progressBar->start();
$processed = 0;
$failed = 0;
foreach ($records as $record) {
$hash = $this->imageHashService->generateHashFromUrl($record->webp_url);
if ($hash) {
$record->update(['image_hash' => $hash]);
$processed++;
} else {
$failed++;
$this->newLine();
$this->error("Failed to generate hash for ID: {$record->id} - {$record->webp_url}");
}
$progressBar->advance();
}
$progressBar->finish();
$this->newLine();
$this->info('Processing complete!');
$this->info("Processed: {$processed}");
$this->info("Failed: {$failed}");
}
}

View File

@@ -0,0 +1,100 @@
<?php
namespace App\Helpers\FirstParty\ImageHash;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Jenssegers\ImageHash\ImageHash;
use Jenssegers\ImageHash\Implementations\DifferenceHash;
class ImageHashService
{
private ImageHash $hasher;
public function __construct()
{
$this->hasher = new ImageHash(new DifferenceHash);
}
public function generateHashFromUrl(string $url): ?string
{
try {
$response = Http::timeout(30)->get($url);
if (! $response->successful()) {
Log::warning("Failed to download image from URL: {$url}");
return null;
}
$imageData = $response->body();
return $this->generateHashFromData($imageData);
} catch (\Exception $e) {
Log::error("Error generating hash from URL {$url}: ".$e->getMessage());
return null;
}
}
public function generateHashFromData(string $imageData): ?string
{
try {
$tempFile = tempnam(sys_get_temp_dir(), 'imagehash_');
file_put_contents($tempFile, $imageData);
$hash = $this->hasher->hash($tempFile);
unlink($tempFile);
return $hash->toHex();
} catch (\Exception $e) {
Log::error('Error generating hash from image data: '.$e->getMessage());
return null;
}
}
public function calculateHammingDistance(string $hash1, string $hash2): int
{
// Validate hashes are not empty
if (empty($hash1) || empty($hash2)) {
return PHP_INT_MAX; // Return max distance for invalid hashes
}
// Pad shorter hash with zeros to make them equal length
$maxLength = max(strlen($hash1), strlen($hash2));
$hash1 = str_pad($hash1, $maxLength, '0', STR_PAD_LEFT);
$hash2 = str_pad($hash2, $maxLength, '0', STR_PAD_LEFT);
$distance = 0;
for ($i = 0; $i < $maxLength; $i++) {
if ($hash1[$i] !== $hash2[$i]) {
$distance++;
}
}
return $distance;
}
public function areHashesSimilar(string $hash1, string $hash2, int $threshold = 5): bool
{
return $this->calculateHammingDistance($hash1, $hash2) <= $threshold;
}
public function findSimilarHashes(string $targetHash, array $hashes, int $threshold = 5): array
{
$similar = [];
foreach ($hashes as $id => $hash) {
if ($this->areHashesSimilar($targetHash, $hash, $threshold)) {
$similar[$id] = $this->calculateHammingDistance($targetHash, $hash);
}
}
asort($similar);
return $similar;
}
}

View File

@@ -0,0 +1,170 @@
<?php
namespace App\Http\Controllers;
use App\Helpers\FirstParty\ImageHash\ImageHashService;
use App\Models\MemeMedia;
use Illuminate\Http\Request;
use Inertia\Inertia;
class AdminDuplicateController extends Controller
{
private ImageHashService $imageHashService;
public function __construct(ImageHashService $imageHashService)
{
$this->imageHashService = $imageHashService;
}
public function index()
{
return Inertia::render('admin/duplicate-management', [
'title' => 'Duplicate Management',
]);
}
public function scan(Request $request)
{
$threshold = $request->input('threshold', 5);
$records = MemeMedia::whereNotNull('image_hash')
->whereNotNull('webp_url')
->where('image_hash', '!=', '')
->get(['id', 'name', 'image_hash', 'webp_url', 'group']);
if ($records->isEmpty()) {
return response()->json([
'duplicates' => [],
'message' => 'No records with image hashes found.',
]);
}
$duplicates = [];
$processed = [];
foreach ($records as $record) {
if (in_array($record->id, $processed)) {
continue;
}
// Skip if hash is empty or invalid
if (empty($record->image_hash)) {
continue;
}
$similarRecords = [];
foreach ($records as $compareRecord) {
if ($record->id === $compareRecord->id || in_array($compareRecord->id, $processed)) {
continue;
}
// Skip if either hash is empty or invalid
if (empty($record->image_hash) || empty($compareRecord->image_hash)) {
continue;
}
$distance = $this->imageHashService->calculateHammingDistance(
$record->image_hash,
$compareRecord->image_hash
);
// Skip if distance calculation failed (returns PHP_INT_MAX)
if ($distance === PHP_INT_MAX) {
continue;
}
if ($distance <= $threshold) {
$similarRecords[] = [
'id' => $compareRecord->id,
'name' => $compareRecord->name,
'distance' => $distance,
'url' => $compareRecord->webp_url,
'group' => $compareRecord->group,
];
$processed[] = $compareRecord->id;
}
}
if (! empty($similarRecords)) {
// Sort similar records to prioritize group 2, then by ID
usort($similarRecords, function ($a, $b) {
if ($a['group'] == $b['group']) {
return $a['id'] <=> $b['id'];
}
return $b['group'] <=> $a['group']; // Higher group first
});
$duplicates[] = [
'original' => [
'id' => $record->id,
'name' => $record->name,
'url' => $record->webp_url,
'group' => $record->group,
],
'duplicates' => $similarRecords,
];
$processed[] = $record->id;
}
}
return response()->json([
'duplicates' => $duplicates,
'total_groups' => count($duplicates),
'total_duplicates' => array_sum(array_map(function ($group) {
return count($group['duplicates']);
}, $duplicates)),
]);
}
public function delete(Request $request)
{
$request->validate([
'id' => 'required|exists:meme_medias,id',
]);
$record = MemeMedia::findOrFail($request->id);
// Soft delete the record
$record->delete();
return response()->json([
'success' => true,
'message' => "Deleted '{$record->name}' (Group {$record->group})",
]);
}
public function regenerateHash(Request $request)
{
$request->validate([
'id' => 'required|exists:meme_medias,id',
]);
$record = MemeMedia::findOrFail($request->id);
if (! $record->webp_url) {
return response()->json([
'success' => false,
'message' => 'No WebP URL found for this record',
], 400);
}
$hash = $this->imageHashService->generateHashFromUrl($record->webp_url);
if (! $hash) {
return response()->json([
'success' => false,
'message' => 'Failed to generate hash',
], 500);
}
$record->update(['image_hash' => $hash]);
return response()->json([
'success' => true,
'message' => 'Hash regenerated successfully',
'hash' => $hash,
]);
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class BasicAuthMiddleware
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
$username = env('BASIC_AUTH_USERNAME');
$password = env('BASIC_AUTH_PASSWORD');
// If credentials are not set, deny access
if (! $username || ! $password) {
return response('Unauthorized', 401, ['WWW-Authenticate' => 'Basic']);
}
// Check if Authorization header is present
if (! $request->header('Authorization')) {
return response('Unauthorized', 401, ['WWW-Authenticate' => 'Basic']);
}
// Extract credentials from Authorization header
$authHeader = $request->header('Authorization');
if (! str_starts_with($authHeader, 'Basic ')) {
return response('Unauthorized', 401, ['WWW-Authenticate' => 'Basic']);
}
$credentials = base64_decode(substr($authHeader, 6));
[$inputUsername, $inputPassword] = explode(':', $credentials, 2);
// Verify credentials
if ($inputUsername !== $username || $inputPassword !== $password) {
return response('Unauthorized', 401, ['WWW-Authenticate' => 'Basic']);
}
return $next($request);
}
}

View File

@@ -68,6 +68,7 @@ class MemeMedia extends Model
'action_keywords',
'emotion_keywords',
'misc_keywords',
'image_hash',
];
protected $hidden = [
@@ -86,6 +87,7 @@ class MemeMedia extends Model
// 'mov_url',
// 'webm_url',
'embedding',
'image_hash',
];
protected $appends = [

View File

@@ -0,0 +1,68 @@
<?php
namespace App\Observers;
use App\Helpers\FirstParty\ImageHash\ImageHashService;
use App\Models\MemeMedia;
class MemeMediaObserver
{
private ImageHashService $imageHashService;
public function __construct(ImageHashService $imageHashService)
{
$this->imageHashService = $imageHashService;
}
/**
* Handle the MemeMedia "created" event.
*/
public function created(MemeMedia $memeMedia): void
{
$this->generateHashIfNeeded($memeMedia);
}
/**
* Handle the MemeMedia "updated" event.
*/
public function updated(MemeMedia $memeMedia): void
{
if ($memeMedia->wasChanged('webp_url')) {
$this->generateHashIfNeeded($memeMedia);
}
}
private function generateHashIfNeeded(MemeMedia $memeMedia): void
{
if ($memeMedia->webp_url && ! $memeMedia->image_hash) {
$hash = $this->imageHashService->generateHashFromUrl($memeMedia->webp_url);
if ($hash) {
$memeMedia->updateQuietly(['image_hash' => $hash]);
}
}
}
/**
* Handle the MemeMedia "deleted" event.
*/
public function deleted(MemeMedia $memeMedia): void
{
//
}
/**
* Handle the MemeMedia "restored" event.
*/
public function restored(MemeMedia $memeMedia): void
{
//
}
/**
* Handle the MemeMedia "force deleted" event.
*/
public function forceDeleted(MemeMedia $memeMedia): void
{
//
}
}

View File

@@ -2,6 +2,8 @@
namespace App\Providers;
use App\Models\MemeMedia;
use App\Observers\MemeMediaObserver;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@@ -19,6 +21,6 @@ public function register(): void
*/
public function boot(): void
{
//
MemeMedia::observe(MemeMediaObserver::class);
}
}

View File

@@ -12,6 +12,7 @@
"php": "^8.2",
"artesaos/seotools": "^1.3",
"inertiajs/inertia-laravel": "^2.0",
"jenssegers/imagehash": "^0.10.0",
"kalnoy/nestedset": "^6.0",
"laravel/cashier": "^15.7",
"laravel/framework": "^12.0",

219
composer.lock generated
View File

@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "2ec5e47e5246683315eb1f220ccd493b",
"content-hash": "dbe9b4012c67d84fc322f58ddb9af791",
"packages": [
{
"name": "artesaos/seotools",
@@ -1814,6 +1814,223 @@
},
"time": "2025-04-10T15:08:36+00:00"
},
{
"name": "intervention/gif",
"version": "4.2.2",
"source": {
"type": "git",
"url": "https://github.com/Intervention/gif.git",
"reference": "5999eac6a39aa760fb803bc809e8909ee67b451a"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Intervention/gif/zipball/5999eac6a39aa760fb803bc809e8909ee67b451a",
"reference": "5999eac6a39aa760fb803bc809e8909ee67b451a",
"shasum": ""
},
"require": {
"php": "^8.1"
},
"require-dev": {
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^10.0 || ^11.0 || ^12.0",
"slevomat/coding-standard": "~8.0",
"squizlabs/php_codesniffer": "^3.8"
},
"type": "library",
"autoload": {
"psr-4": {
"Intervention\\Gif\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@intervention.io",
"homepage": "https://intervention.io/"
}
],
"description": "Native PHP GIF Encoder/Decoder",
"homepage": "https://github.com/intervention/gif",
"keywords": [
"animation",
"gd",
"gif",
"image"
],
"support": {
"issues": "https://github.com/Intervention/gif/issues",
"source": "https://github.com/Intervention/gif/tree/4.2.2"
},
"funding": [
{
"url": "https://paypal.me/interventionio",
"type": "custom"
},
{
"url": "https://github.com/Intervention",
"type": "github"
},
{
"url": "https://ko-fi.com/interventionphp",
"type": "ko_fi"
}
],
"time": "2025-03-29T07:46:21+00:00"
},
{
"name": "intervention/image",
"version": "3.11.3",
"source": {
"type": "git",
"url": "https://github.com/Intervention/image.git",
"reference": "d0f097b8a3fa8fb758efc9440b513aa3833cda17"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/Intervention/image/zipball/d0f097b8a3fa8fb758efc9440b513aa3833cda17",
"reference": "d0f097b8a3fa8fb758efc9440b513aa3833cda17",
"shasum": ""
},
"require": {
"ext-mbstring": "*",
"intervention/gif": "^4.2",
"php": "^8.1"
},
"require-dev": {
"mockery/mockery": "^1.6",
"phpstan/phpstan": "^2.1",
"phpunit/phpunit": "^10.0 || ^11.0 || ^12.0",
"slevomat/coding-standard": "~8.0",
"squizlabs/php_codesniffer": "^3.8"
},
"suggest": {
"ext-exif": "Recommended to be able to read EXIF data properly."
},
"type": "library",
"autoload": {
"psr-4": {
"Intervention\\Image\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Oliver Vogel",
"email": "oliver@intervention.io",
"homepage": "https://intervention.io/"
}
],
"description": "PHP image manipulation",
"homepage": "https://image.intervention.io/",
"keywords": [
"gd",
"image",
"imagick",
"resize",
"thumbnail",
"watermark"
],
"support": {
"issues": "https://github.com/Intervention/image/issues",
"source": "https://github.com/Intervention/image/tree/3.11.3"
},
"funding": [
{
"url": "https://paypal.me/interventionio",
"type": "custom"
},
{
"url": "https://github.com/Intervention",
"type": "github"
},
{
"url": "https://ko-fi.com/interventionphp",
"type": "ko_fi"
}
],
"time": "2025-05-22T17:26:23+00:00"
},
{
"name": "jenssegers/imagehash",
"version": "v0.10.0",
"source": {
"type": "git",
"url": "https://github.com/jenssegers/imagehash.git",
"reference": "643d8d676c5cbe637199206476015e36548841f0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/jenssegers/imagehash/zipball/643d8d676c5cbe637199206476015e36548841f0",
"reference": "643d8d676c5cbe637199206476015e36548841f0",
"shasum": ""
},
"require": {
"intervention/image": "^3.3",
"php": "^8.1"
},
"require-dev": {
"php-coveralls/php-coveralls": "^2.0",
"phpunit/phpunit": "^10"
},
"suggest": {
"ext-gd": "GD or ImageMagick is required",
"ext-gmp": "GMP results in faster comparisons",
"ext-imagick": "GD or ImageMagick is required"
},
"type": "library",
"autoload": {
"psr-4": {
"Jenssegers\\ImageHash\\": "src"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Jens Segers",
"homepage": "https://jenssegers.com"
},
{
"name": "Kenneth Rapp"
}
],
"description": "Perceptual image hashing for PHP",
"homepage": "https://github.com/jenssegers/imagehash",
"keywords": [
"ahash",
"dhash",
"hash",
"image hash",
"imagehash",
"perceptual",
"phash"
],
"support": {
"issues": "https://github.com/jenssegers/imagehash/issues",
"source": "https://github.com/jenssegers/imagehash/tree/v0.10.0"
},
"funding": [
{
"url": "https://github.com/jenssegers",
"type": "github"
},
{
"url": "https://tidelift.com/funding/github/packagist/jenssegers/imagehash",
"type": "tidelift"
}
],
"time": "2024-06-17T09:53:53+00:00"
},
{
"name": "kalnoy/nestedset",
"version": "v6.0.6",

View File

@@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('meme_medias', function (Blueprint $table) {
$table->string('image_hash', 64)->nullable()->index();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('meme_medias', function (Blueprint $table) {
$table->dropColumn('image_hash');
});
}
};

View File

@@ -0,0 +1,295 @@
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { useAxios } from '@/plugins/AxiosContext';
import { Head, usePage } from '@inertiajs/react';
import { AlertCircle, Loader2, RotateCw, Search, Trash2 } from 'lucide-react';
import { useState } from 'react';
import { toast } from 'sonner';
interface DuplicateItem {
id: number;
name: string;
distance: number;
url: string;
group: number;
}
interface DuplicateGroup {
original: {
id: number;
name: string;
url: string;
group: number;
} | null;
duplicates: DuplicateItem[];
}
interface ScanResult {
duplicates: DuplicateGroup[];
total_groups: number;
total_duplicates: number;
}
export default function DuplicateManagement() {
const { title } = usePage().props;
const axios = useAxios();
const [threshold, setThreshold] = useState(5);
const [isScanning, setIsScanning] = useState(false);
const [scanResult, setScanResult] = useState<ScanResult | null>(null);
const [deletingIds, setDeletingIds] = useState<number[]>([]);
const handleScan = async () => {
setIsScanning(true);
try {
const response = await axios.post(route('admin.duplicates.scan'), {
threshold,
});
if (response.data.duplicates.length === 0) {
toast.info('No duplicate images were found with the current threshold.');
}
setScanResult(response.data);
} catch (error) {
console.error('Scan error:', error);
toast.error('Failed to scan for duplicates');
} finally {
setIsScanning(false);
}
};
const handleDelete = async (id: number) => {
setDeletingIds((prev) => [...prev, id]);
try {
const response = await axios.post(route('admin.duplicates.delete'), {
id,
});
if (response.data.success) {
toast.success(response.data.message);
// Remove the deleted item from results
setScanResult((prev) => {
if (!prev) return null;
return {
...prev,
duplicates: prev.duplicates.map((group) => {
// If the original was deleted, remove it from the group
if (group.original && group.original.id === id) {
return {
...group,
original: null, // Mark original as deleted
};
}
// Remove the deleted item from duplicates
return {
...group,
duplicates: group.duplicates.filter((item) => item.id !== id),
};
}),
};
});
}
} catch (error) {
console.error('Delete error:', error);
if (error.response?.data?.message) {
toast.error(error.response.data.message);
} else {
toast.error('Failed to delete image');
}
} finally {
setDeletingIds((prev) => prev.filter((deletingId) => deletingId !== id));
}
};
const handleRegenerateHash = async (id: number) => {
try {
const response = await axios.post(route('admin.duplicates.regenerate-hash'), {
id,
});
if (response.data.success) {
toast.success(response.data.message);
}
} catch (error) {
console.error('Regenerate hash error:', error);
toast.error('Failed to regenerate hash');
}
};
return (
<>
<Head title={title} />
<div className="mx-auto max-w-7xl p-6">
<div className="mb-8">
<h1 className="mb-2 text-3xl font-bold">Duplicate Management</h1>
<p className="text-gray-600">Find and manage duplicate meme images using Hamming distance comparison</p>
</div>
<Card className="mb-6">
<CardHeader>
<CardTitle>Scan for Duplicates</CardTitle>
</CardHeader>
<CardContent>
<div className="flex items-center gap-4">
<div className="flex-1 items-center">
<Label htmlFor="threshold">Hamming Distance Threshold</Label>
<Input
id="threshold"
type="text"
value={threshold}
onChange={(e) => setThreshold(Number(e.target.value))}
min="0"
max="64"
className="mt-1"
/>
<p className="mt-1 text-sm text-gray-500">Lower values = stricter matching (0-10 recommended)</p>
</div>
<Button onClick={handleScan} disabled={isScanning}>
{isScanning ? (
<>
<Loader2 className="mr-2 h-4 w-4 animate-spin" />
Scanning...
</>
) : (
<>
<Search className="mr-2 h-4 w-4" />
Scan for Duplicates
</>
)}
</Button>
</div>
</CardContent>
</Card>
{scanResult && (
<Card>
<CardHeader>
<CardTitle className="flex items-center gap-2">
<AlertCircle className="h-5 w-5" />
Scan Results
</CardTitle>
<div className="flex gap-4 text-sm text-gray-600">
<span>Groups: {scanResult.total_groups}</span>
<span>Total Duplicates: {scanResult.total_duplicates}</span>
</div>
</CardHeader>
<CardContent>
{scanResult.duplicates.length === 0 ? (
<p className="py-8 text-center text-gray-500">No duplicates found</p>
) : (
<div className="space-y-6">
{scanResult.duplicates.map((group, index) => (
<div key={index} className="rounded-lg border p-4">
<div className="mb-4 flex items-center justify-between">
<h3 className="text-lg font-semibold">Duplicate Group {index + 1}</h3>
<Badge variant="outline">{group.duplicates.length} duplicates</Badge>
</div>
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4">
{/* Original */}
{group.original && (
<div className="relative rounded-lg border-2 border-blue-200 p-3">
<div className="absolute top-2 right-2 flex flex-col gap-1">
<Badge variant="default">Original</Badge>
<Badge variant={group.original.group === 2 ? 'default' : 'secondary'}>
Group {group.original.group}
</Badge>
</div>
<img
src={group.original.url}
alt={group.original.name}
className="mb-2 h-32 w-full rounded object-cover"
/>
<p className="mb-1 text-sm font-medium">{group.original.name}</p>
<p className="mb-2 text-xs text-gray-500">ID: {group.original.id}</p>
<div className="flex gap-2">
<Button
variant="outline"
size="sm"
onClick={() => handleRegenerateHash(group.original.id)}
className="flex-1"
>
<RotateCw className="mr-1 h-3 w-3" />
Regenerate
</Button>
<Button
variant="destructive"
size="sm"
onClick={() => handleDelete(group.original.id)}
disabled={deletingIds.includes(group.original.id)}
className="flex-1"
>
{deletingIds.includes(group.original.id) ? (
<Loader2 className="mr-1 h-3 w-3 animate-spin" />
) : (
<Trash2 className="mr-1 h-3 w-3" />
)}
Delete
</Button>
</div>
</div>
)}
{/* Duplicates */}
{group.duplicates.map((duplicate) => (
<div key={duplicate.id} className="relative rounded-lg border p-3">
<div className="absolute top-2 right-2 flex flex-col gap-1">
<Badge variant="secondary">Distance: {duplicate.distance}</Badge>
<Badge variant={duplicate.group === 2 ? 'default' : 'secondary'}>
Group {duplicate.group}
</Badge>
</div>
<img
src={duplicate.url}
alt={duplicate.name}
className="mb-2 h-32 w-full rounded object-cover"
/>
<p className="mb-1 text-sm font-medium">{duplicate.name}</p>
<p className="mb-2 text-xs text-gray-500">ID: {duplicate.id}</p>
<div className="flex gap-2">
<Button
variant="outline"
size="sm"
onClick={() => handleRegenerateHash(duplicate.id)}
className="flex-1"
>
<RotateCw className="mr-1 h-3 w-3" />
Regenerate
</Button>
<Button
variant="destructive"
size="sm"
onClick={() => handleDelete(duplicate.id)}
disabled={deletingIds.includes(duplicate.id)}
className="flex-1"
>
{deletingIds.includes(duplicate.id) ? (
<Loader2 className="mr-1 h-3 w-3 animate-spin" />
) : (
<Trash2 className="mr-1 h-3 w-3" />
)}
Delete
</Button>
</div>
</div>
))}
</div>
</div>
))}
</div>
)}
</CardContent>
</Card>
)}
</div>
</>
);
}

File diff suppressed because one or more lines are too long

View File

@@ -2,12 +2,14 @@
use App\Http\Controllers\AdminBackgroundGenerationController;
use App\Http\Controllers\AdminDashboardController;
use App\Http\Controllers\AdminDuplicateController;
use App\Http\Controllers\FrontHomeController;
use App\Http\Controllers\FrontPagesController;
use App\Http\Controllers\SocialAuthController;
use App\Http\Controllers\UserDashboardController;
use App\Http\Controllers\UserPurchaseController;
use App\Http\Middleware\AdminMiddleware;
use App\Http\Middleware\BasicAuthMiddleware;
use Illuminate\Support\Facades\Route;
if (App::environment('local')) {
@@ -72,3 +74,17 @@
Route::get('/terms', [FrontPagesController::class, 'terms'])
->middleware('cacheResponse')
->name('terms');
// Admin Tools with Basic Auth
Route::prefix('duplicates')->middleware([BasicAuthMiddleware::class])->group(function () {
Route::get('/', [AdminDuplicateController::class, 'index'])->name('admin.duplicates');
Route::post('scan', [AdminDuplicateController::class, 'scan'])
->name('admin.duplicates.scan');
Route::post('delete', [AdminDuplicateController::class, 'delete'])
->name('admin.duplicates.delete');
Route::post('regenerate-hash', [AdminDuplicateController::class, 'regenerateHash'])
->name('admin.duplicates.regenerate-hash');
});