refactor: [equipment] 사진 업로드를 R2(FileStorageSystem) 기반으로 전환
- GCS 스텁 코드를 Cloudflare R2 기반 실제 파일 업로드로 교체 - File 모델 import를 Boards\File에서 Commons\File로 수정 - StoreEquipmentPhotoRequest FormRequest 추가 (파일 검증) - 다중 파일 업로드 지원 (최대 10장 제한) - softDeleteFile 패턴 적용 (삭제 시 soft delete) - ItemsFileController 패턴 준용 (R2 저장, 랜덤 파일명)
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\V1\Equipment;
|
||||
|
||||
use App\Helpers\ApiResponse;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Requests\Equipment\StoreEquipmentPhotoRequest;
|
||||
use App\Services\Equipment\EquipmentPhotoService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class EquipmentPhotoController extends Controller
|
||||
{
|
||||
public function __construct(private readonly EquipmentPhotoService $service) {}
|
||||
|
||||
public function index(int $id): JsonResponse
|
||||
{
|
||||
return ApiResponse::handle(
|
||||
fn () => $this->service->index($id),
|
||||
__('message.fetched')
|
||||
);
|
||||
}
|
||||
|
||||
public function store(StoreEquipmentPhotoRequest $request, int $id): JsonResponse
|
||||
{
|
||||
return ApiResponse::handle(
|
||||
fn () => $this->service->store($id, $request->file('files')),
|
||||
__('message.equipment.photo_uploaded')
|
||||
);
|
||||
}
|
||||
|
||||
public function destroy(int $id, int $fileId): JsonResponse
|
||||
{
|
||||
return ApiResponse::handle(
|
||||
fn () => $this->service->destroy($id, $fileId),
|
||||
__('message.deleted')
|
||||
);
|
||||
}
|
||||
}
|
||||
53
app/Http/Requests/Equipment/StoreEquipmentPhotoRequest.php
Normal file
53
app/Http/Requests/Equipment/StoreEquipmentPhotoRequest.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests\Equipment;
|
||||
|
||||
use App\Models\Commons\File;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class StoreEquipmentPhotoRequest extends FormRequest
|
||||
{
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
public function rules(): array
|
||||
{
|
||||
$equipmentId = $this->route('id');
|
||||
$currentCount = File::where('document_id', $equipmentId)
|
||||
->where('document_type', 'equipment')
|
||||
->whereNull('deleted_at')
|
||||
->count();
|
||||
|
||||
$maxFiles = 10 - $currentCount;
|
||||
|
||||
return [
|
||||
'files' => ['required', 'array', 'min:1', "max:{$maxFiles}"],
|
||||
'files.*' => [
|
||||
'required',
|
||||
'file',
|
||||
'mimes:jpg,jpeg,png,gif,bmp,webp',
|
||||
'max:10240', // 10MB
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function attributes(): array
|
||||
{
|
||||
return [
|
||||
'files' => '사진 파일',
|
||||
'files.*' => '사진 파일',
|
||||
];
|
||||
}
|
||||
|
||||
public function messages(): array
|
||||
{
|
||||
return [
|
||||
'files.required' => __('error.file.required'),
|
||||
'files.max' => __('error.equipment.photo_limit_exceeded'),
|
||||
'files.*.mimes' => __('error.file.invalid_type'),
|
||||
'files.*.max' => __('error.file.size_exceeded'),
|
||||
];
|
||||
}
|
||||
}
|
||||
154
app/Models/Equipment/Equipment.php
Normal file
154
app/Models/Equipment/Equipment.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models\Equipment;
|
||||
|
||||
use App\Models\Commons\File;
|
||||
use App\Traits\Auditable;
|
||||
use App\Traits\BelongsToTenant;
|
||||
use App\Traits\ModelTrait;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class Equipment extends Model
|
||||
{
|
||||
use Auditable, BelongsToTenant, ModelTrait, SoftDeletes;
|
||||
|
||||
protected $table = 'equipments';
|
||||
|
||||
protected $fillable = [
|
||||
'tenant_id',
|
||||
'equipment_code',
|
||||
'name',
|
||||
'equipment_type',
|
||||
'specification',
|
||||
'manufacturer',
|
||||
'model_name',
|
||||
'serial_no',
|
||||
'location',
|
||||
'production_line',
|
||||
'purchase_date',
|
||||
'install_date',
|
||||
'purchase_price',
|
||||
'useful_life',
|
||||
'status',
|
||||
'disposed_date',
|
||||
'manager_id',
|
||||
'sub_manager_id',
|
||||
'photo_path',
|
||||
'memo',
|
||||
'options',
|
||||
'is_active',
|
||||
'sort_order',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'deleted_by',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'purchase_date' => 'date',
|
||||
'install_date' => 'date',
|
||||
'disposed_date' => 'date',
|
||||
'purchase_price' => 'decimal:2',
|
||||
'is_active' => 'boolean',
|
||||
'options' => 'array',
|
||||
];
|
||||
|
||||
public function getOption(string $key, mixed $default = null): mixed
|
||||
{
|
||||
return data_get($this->options, $key, $default);
|
||||
}
|
||||
|
||||
public function setOption(string $key, mixed $value): self
|
||||
{
|
||||
$options = $this->options ?? [];
|
||||
data_set($options, $key, $value);
|
||||
$this->options = $options;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function manager(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Models\User::class, 'manager_id');
|
||||
}
|
||||
|
||||
public function subManager(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(\App\Models\User::class, 'sub_manager_id');
|
||||
}
|
||||
|
||||
public function canInspect(?int $userId = null): bool
|
||||
{
|
||||
if (! $userId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->manager_id === $userId || $this->sub_manager_id === $userId;
|
||||
}
|
||||
|
||||
public function inspectionTemplates(): HasMany
|
||||
{
|
||||
return $this->hasMany(EquipmentInspectionTemplate::class, 'equipment_id')->orderBy('sort_order');
|
||||
}
|
||||
|
||||
public function inspections(): HasMany
|
||||
{
|
||||
return $this->hasMany(EquipmentInspection::class, 'equipment_id');
|
||||
}
|
||||
|
||||
public function repairs(): HasMany
|
||||
{
|
||||
return $this->hasMany(EquipmentRepair::class, 'equipment_id');
|
||||
}
|
||||
|
||||
public function photos(): HasMany
|
||||
{
|
||||
return $this->hasMany(File::class, 'document_id')
|
||||
->where('document_type', 'equipment')
|
||||
->orderBy('id');
|
||||
}
|
||||
|
||||
public function processes(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(\App\Models\Process::class, 'equipment_process')
|
||||
->withPivot('is_primary')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function scopeByLine($query, string $line)
|
||||
{
|
||||
return $query->where('production_line', $line);
|
||||
}
|
||||
|
||||
public function scopeByType($query, string $type)
|
||||
{
|
||||
return $query->where('equipment_type', $type);
|
||||
}
|
||||
|
||||
public function scopeByStatus($query, string $status)
|
||||
{
|
||||
return $query->where('status', $status);
|
||||
}
|
||||
|
||||
public static function getEquipmentTypes(): array
|
||||
{
|
||||
return ['포밍기', '미싱기', '샤링기', 'V컷팅기', '절곡기', '프레스', '드릴', '기타'];
|
||||
}
|
||||
|
||||
public static function getProductionLines(): array
|
||||
{
|
||||
return ['스라트', '스크린', '절곡', '기타'];
|
||||
}
|
||||
|
||||
public static function getStatuses(): array
|
||||
{
|
||||
return [
|
||||
'active' => '가동',
|
||||
'idle' => '유휴',
|
||||
'disposed' => '폐기',
|
||||
];
|
||||
}
|
||||
}
|
||||
124
app/Services/Equipment/EquipmentPhotoService.php
Normal file
124
app/Services/Equipment/EquipmentPhotoService.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Equipment;
|
||||
|
||||
use App\Models\Commons\File;
|
||||
use App\Models\Equipment\Equipment;
|
||||
use App\Services\Service;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
|
||||
class EquipmentPhotoService extends Service
|
||||
{
|
||||
private const MAX_PHOTOS = 10;
|
||||
|
||||
public function index(int $equipmentId): array
|
||||
{
|
||||
$equipment = $this->getEquipment($equipmentId);
|
||||
|
||||
return $equipment->photos->map(fn ($file) => $this->formatFileResponse($file))->values()->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param UploadedFile[] $files
|
||||
*/
|
||||
public function store(int $equipmentId, array $files): array
|
||||
{
|
||||
$equipment = $this->getEquipment($equipmentId);
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
$currentCount = File::where('document_id', $equipmentId)
|
||||
->where('document_type', 'equipment')
|
||||
->whereNull('deleted_at')
|
||||
->count();
|
||||
|
||||
if ($currentCount + count($files) > self::MAX_PHOTOS) {
|
||||
throw new \Exception(__('error.equipment.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/equipment/%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' => $equipmentId,
|
||||
'document_type' => 'equipment',
|
||||
'is_temp' => false,
|
||||
'uploaded_by' => $userId,
|
||||
'created_by' => $userId,
|
||||
]);
|
||||
|
||||
$uploaded[] = $this->formatFileResponse($file);
|
||||
}
|
||||
|
||||
return $uploaded;
|
||||
}
|
||||
|
||||
public function destroy(int $equipmentId, int $fileId): array
|
||||
{
|
||||
$tenantId = $this->tenantId();
|
||||
$userId = $this->apiUserId();
|
||||
|
||||
$file = File::where('tenant_id', $tenantId)
|
||||
->where('document_id', $equipmentId)
|
||||
->where('document_type', 'equipment')
|
||||
->where('id', $fileId)
|
||||
->first();
|
||||
|
||||
if (! $file) {
|
||||
throw new NotFoundHttpException(__('error.file.not_found'));
|
||||
}
|
||||
|
||||
$file->softDeleteFile($userId);
|
||||
|
||||
return [
|
||||
'file_id' => $fileId,
|
||||
'deleted' => true,
|
||||
];
|
||||
}
|
||||
|
||||
private function getEquipment(int $equipmentId): Equipment
|
||||
{
|
||||
$equipment = Equipment::find($equipmentId);
|
||||
|
||||
if (! $equipment) {
|
||||
throw new NotFoundHttpException(__('error.equipment.not_found'));
|
||||
}
|
||||
|
||||
return $equipment;
|
||||
}
|
||||
|
||||
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'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user