Files
sam-api/app/Services/Finance/VehiclePhotoService.php

125 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Services\Finance;
use App\Models\Commons\File;
use App\Models\Finance\CorporateVehicle;
use App\Services\Service;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class VehiclePhotoService extends Service
{
private const MAX_PHOTOS = 10;
public function index(int $vehicleId): array
{
$vehicle = $this->getVehicle($vehicleId);
return $vehicle->photos->map(fn ($file) => $this->formatFileResponse($file))->values()->toArray();
}
/**
* @param UploadedFile[] $files
*/
public function store(int $vehicleId, array $files): array
{
$vehicle = $this->getVehicle($vehicleId);
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$currentCount = File::where('document_id', $vehicleId)
->where('document_type', 'corporate_vehicle')
->whereNull('deleted_at')
->count();
if ($currentCount + count($files) > self::MAX_PHOTOS) {
throw new \Exception(__('error.vehicle.photo_limit_exceeded'));
}
$uploaded = [];
foreach ($files as $uploadedFile) {
$extension = $uploadedFile->getClientOriginalExtension();
$storedName = bin2hex(random_bytes(8)).'.'.$extension;
$displayName = $uploadedFile->getClientOriginalName();
$year = date('Y');
$month = date('m');
$directory = sprintf('%d/corporate-vehicles/%s/%s', $tenantId, $year, $month);
$filePath = $directory.'/'.$storedName;
Storage::disk('r2')->putFileAs($directory, $uploadedFile, $storedName);
$mimeType = $uploadedFile->getMimeType();
$file = File::create([
'tenant_id' => $tenantId,
'display_name' => $displayName,
'stored_name' => $storedName,
'file_path' => $filePath,
'file_size' => $uploadedFile->getSize(),
'mime_type' => $mimeType,
'file_type' => 'image',
'document_id' => $vehicleId,
'document_type' => 'corporate_vehicle',
'is_temp' => false,
'uploaded_by' => $userId,
'created_by' => $userId,
]);
$uploaded[] = $this->formatFileResponse($file);
}
return $uploaded;
}
public function destroy(int $vehicleId, int $fileId): array
{
$tenantId = $this->tenantId();
$userId = $this->apiUserId();
$file = File::where('tenant_id', $tenantId)
->where('document_id', $vehicleId)
->where('document_type', 'corporate_vehicle')
->where('id', $fileId)
->first();
if (! $file) {
throw new NotFoundHttpException(__('error.file.not_found'));
}
$file->softDeleteFile($userId);
return [
'file_id' => $fileId,
'deleted' => true,
];
}
private function getVehicle(int $vehicleId): CorporateVehicle
{
$vehicle = CorporateVehicle::find($vehicleId);
if (! $vehicle) {
throw new NotFoundHttpException(__('error.vehicle.not_found'));
}
return $vehicle;
}
private function formatFileResponse(File $file): array
{
return [
'id' => $file->id,
'file_name' => $file->display_name,
'file_path' => $file->file_path,
'file_url' => url("/api/v1/files/{$file->id}/download"),
'file_size' => $file->file_size,
'mime_type' => $file->mime_type,
'created_at' => $file->created_at?->format('Y-m-d H:i:s'),
];
}
}