77 lines
2.6 KiB
PHP
77 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Core\File\Http\Controllers;
|
|
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Modules\Core\File\Services\FileService;
|
|
|
|
/**
|
|
* The generic "accept an upload, validate it, store it via FileService,
|
|
* return its id" flow — what varies per use case (which extensions/sizes
|
|
* are acceptable) is deliberately NOT configurable here, and NEVER trusts
|
|
* anything the request itself claims about its own limits: a client could
|
|
* simply lie about them. purpose()/validationRules() are protected hooks
|
|
* a concrete subclass overrides instead — an ordinary PHP method a caller
|
|
* writes once per upload policy, not request input, so the actual limit
|
|
* enforced is always whatever server-side code says it is. Different
|
|
* products can even need different limits (a 3D-print reference photo
|
|
* vs. a video upload, say) — that's still a subclass's own store()
|
|
* override deciding which rule set applies to a given request, not
|
|
* something this base class or a shared config file could express.
|
|
*/
|
|
abstract class UploadFileController extends Controller
|
|
{
|
|
/**
|
|
* The File row's `purpose` tag (see Models\File) — also the storage
|
|
* directory it lands under (FileService::store()'s single $purpose
|
|
* param doubles as both).
|
|
*/
|
|
abstract protected function purpose(): string;
|
|
|
|
/**
|
|
* Laravel validation rules for the incoming request, keyed exactly as
|
|
* $request->all() would be. Must include a 'file' rule accepting an
|
|
* uploaded file — this class always reads the file from that key.
|
|
*
|
|
* @return array<string, array<int, mixed>>
|
|
*/
|
|
abstract protected function validationRules(Request $request): array;
|
|
|
|
public function store(Request $request, FileService $files): JsonResponse
|
|
{
|
|
$validator = Validator::make(
|
|
$request->all(),
|
|
$this->validationRules($request),
|
|
$this->validationMessages($request),
|
|
$this->validationAttributes($request),
|
|
);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['error' => $validator->errors()->first('file')], 422);
|
|
}
|
|
|
|
$file = $files->store($request->file('file'), $this->purpose());
|
|
|
|
return response()->json(['file_id' => $file->id]);
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function validationMessages(Request $request): array
|
|
{
|
|
return [];
|
|
}
|
|
|
|
/**
|
|
* @return array<string, string>
|
|
*/
|
|
protected function validationAttributes(Request $request): array
|
|
{
|
|
return [];
|
|
}
|
|
}
|