2025-12-01 17:02:01 +03:00

69 lines
2.2 KiB
PHP

<?php
namespace App\Http\Resources\File;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class FileResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'file_name' => $this->file_name,
'mime_type' => $this->mime_type,
'size_bytes' => $this->size_bytes,
'size_formatted' => $this->formatted_size,
'extension' => $this->extension,
'storage_uri' => $this->storage_uri,
'organized_path' => $this->organized_path,
'sha256' => $this->sha256,
'uploaded_by' => $this->uploaded_by,
'uploader_name' => $this->uploader_name,
'uploaded_at' => $this->uploaded_at?->format('Y-m-d H:i:s'),
'created_at' => $this->created_at?->format('Y-m-d H:i:s'),
'updated_at' => $this->updated_at?->format('Y-m-d H:i:s'),
// File type helpers
'is_image' => $this->is_image,
'is_pdf' => $this->is_pdf,
// URL for accessing the file (if public)
'url' => $this->when(
$this->is_public ?? false,
asset('storage/' . $this->storage_uri)
),
// Relations
'user' => [
'id' => $this->user?->id,
'name' => $this->user?->name,
'email' => $this->user?->email,
],
// Additional metadata from the file's path structure
'category' => $this->when(
$this->storage_uri,
function () {
$pathParts = explode('/', $this->storage_uri);
return $pathParts[count($pathParts) - 3] ?? 'general';
}
),
'subcategory' => $this->when(
$this->storage_uri,
function () {
$pathParts = explode('/', $this->storage_uri);
return $pathParts[count($pathParts) - 2] ?? 'general';
}
),
];
}
}