Seven search call sites used the `ilike` operator, which only PostgreSQL understands. On MySQL they raise a syntax error, so post, asset, label, signature and workspace-member search — plus the MCP list-posts tool — were unusable on an engine `config/database.php` has always supported and the docs advertise. Replace them with `whereLike($column, $value)`, which the query grammars translate per driver: PostgresGrammar emits `ilike` and MySqlGrammar emits `like`. The generated SQL on PostgreSQL is therefore unchanged. Verified by running the full suite on both engines: PostgreSQL 16 3888 passed, 0 failed MySQL 8.0.46 one pre-existing failure fixed, none introduced Also adds case-insensitivity assertions to the five affected suites that lacked them, and search coverage for ListPostsTool, which had none. Note for MySQL installs: `like` is case-insensitive by virtue of the column collation, not the operator. Under the default `utf8mb4_unicode_ci` it is also accent-insensitive, so a search for "cafe" matches a stored "café" — PostgreSQL's `ilike` does not. That difference comes from the collation rather than this change. A `_bin` or `_cs` collation would make search case-sensitive on both. Co-authored-by: Paulo Castellano <paulo@castellanos.llc>
167 lines
5.4 KiB
PHP
167 lines
5.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\App;
|
|
|
|
use App\Http\Requests\App\Asset\StoreAssetFromUrlRequest;
|
|
use App\Http\Requests\App\Asset\StoreAssetRequest;
|
|
use App\Http\Requests\App\Asset\StoreChunkedAssetRequest;
|
|
use App\Http\Resources\App\MediaResource;
|
|
use App\Models\Media;
|
|
use App\Services\Brand\SafeHttpFetcher;
|
|
use App\Services\Media\ChunkedAssetReceiver;
|
|
use App\Services\UnsplashService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Str;
|
|
use Inertia\Inertia;
|
|
use Inertia\Response;
|
|
use RuntimeException;
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
|
|
|
class AssetController extends Controller
|
|
{
|
|
public function index(Request $request): Response|RedirectResponse
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
if (! $workspace) {
|
|
return redirect()->route('app.workspaces.create');
|
|
}
|
|
|
|
$this->authorize('createPost', $workspace);
|
|
|
|
return Inertia::render('assets/Index');
|
|
}
|
|
|
|
public function search(Request $request): AnonymousResourceCollection
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
$this->authorize('createPost', $workspace);
|
|
|
|
$term = trim((string) $request->input('search', ''));
|
|
$type = $request->input('type');
|
|
|
|
$assets = $workspace->getMedia('assets')
|
|
->when($term !== '', fn ($query) => $query->whereLike('original_filename', '%'.$term.'%'))
|
|
->when(in_array($type, ['image', 'video'], true), fn ($query) => $query->where('type', $type))
|
|
->latest()
|
|
->paginate(config('app.pagination.default'));
|
|
|
|
return MediaResource::collection($assets);
|
|
}
|
|
|
|
public function store(StoreAssetRequest $request): MediaResource
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
$this->authorize('createPost', $workspace);
|
|
|
|
$clientMeta = (array) $request->input('meta', []);
|
|
|
|
$media = $workspace->addMedia($request->file('media'), 'assets', $clientMeta);
|
|
|
|
return new MediaResource($media);
|
|
}
|
|
|
|
public function storeChunked(StoreChunkedAssetRequest $request, ChunkedAssetReceiver $receiver): JsonResponse
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
$this->authorize('createPost', $workspace);
|
|
|
|
return $receiver->receive(
|
|
$workspace,
|
|
$request->user(),
|
|
$request->validated('file_name'),
|
|
$request->getContent(),
|
|
(int) $request->validated('range_start'),
|
|
(int) $request->validated('range_end'),
|
|
(int) $request->validated('total_size'),
|
|
(string) $request->validated('upload_id'),
|
|
)->toResponse();
|
|
}
|
|
|
|
public function storeFromUrl(StoreAssetFromUrlRequest $request, UnsplashService $unsplash, SafeHttpFetcher $safeHttp): MediaResource
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
$this->authorize('createPost', $workspace);
|
|
|
|
$validated = $request->validated();
|
|
|
|
// Trigger Unsplash download tracking (required by API guidelines)
|
|
if ($downloadLocation = data_get($validated, 'download_location')) {
|
|
$unsplash->trackDownload($downloadLocation);
|
|
}
|
|
|
|
$url = data_get($validated, 'url');
|
|
|
|
try {
|
|
$response = $safeHttp->guardedRequest($url)->timeout(30)->get($url);
|
|
} catch (RuntimeException) {
|
|
abort(SymfonyResponse::HTTP_BAD_REQUEST, 'Failed to download image from URL');
|
|
}
|
|
|
|
if ($response->failed()) {
|
|
abort(SymfonyResponse::HTTP_BAD_REQUEST, 'Failed to download image from URL');
|
|
}
|
|
|
|
$mimeType = $response->header('Content-Type', 'image/jpeg');
|
|
$extension = match (true) {
|
|
str_contains($mimeType, 'png') => 'png',
|
|
str_contains($mimeType, 'gif') => 'gif',
|
|
str_contains($mimeType, 'webp') => 'webp',
|
|
default => 'jpg',
|
|
};
|
|
|
|
$filename = Str::uuid().'.'.$extension;
|
|
$path = "medias/{$filename}";
|
|
|
|
Storage::put($path, $response->body());
|
|
|
|
$meta = [];
|
|
$tempFile = tempnam(sys_get_temp_dir(), 'unsplash');
|
|
file_put_contents($tempFile, $response->body());
|
|
$imageInfo = @getimagesize($tempFile);
|
|
if ($imageInfo) {
|
|
$meta['width'] = $imageInfo[0];
|
|
$meta['height'] = $imageInfo[1];
|
|
}
|
|
@unlink($tempFile);
|
|
|
|
$media = $workspace->media()->create([
|
|
'group_id' => Str::uuid()->toString(),
|
|
'collection' => 'assets',
|
|
'type' => 'image',
|
|
'path' => $path,
|
|
'original_filename' => data_get($validated, 'filename'),
|
|
'mime_type' => $mimeType,
|
|
'size' => strlen($response->body()),
|
|
'order' => 0,
|
|
'meta' => $meta,
|
|
]);
|
|
|
|
return new MediaResource($media);
|
|
}
|
|
|
|
public function destroy(Request $request, Media $media): RedirectResponse
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
$this->authorize('createPost', $workspace);
|
|
|
|
if ($media->mediable_type !== $workspace->getMorphClass() || $media->mediable_id !== $workspace->id) {
|
|
abort(SymfonyResponse::HTTP_FORBIDDEN);
|
|
}
|
|
|
|
$media->delete();
|
|
|
|
return back();
|
|
}
|
|
}
|