99 lines
3.3 KiB
PHP
99 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace app\support;
|
|
|
|
use support\Request;
|
|
use function pathinfo;
|
|
use function parse_url;
|
|
use function strtolower;
|
|
|
|
class AppraisalEvidenceService
|
|
{
|
|
public function upload(Request $request, string $inputName = 'file', string $scene = 'appraisal_evidence'): array
|
|
{
|
|
return $this->fileUploadService()->upload($request, $scene, $inputName);
|
|
}
|
|
|
|
public function delete(string $fileUrl): void
|
|
{
|
|
$this->fileUploadService()->delete($fileUrl);
|
|
}
|
|
|
|
public function normalize(mixed $attachments, ?Request $request = null, bool $forStorage = false): array
|
|
{
|
|
if (is_string($attachments) && $attachments !== '') {
|
|
$decoded = json_decode($attachments, true);
|
|
$attachments = is_array($decoded) ? $decoded : [];
|
|
}
|
|
|
|
if (!is_array($attachments)) {
|
|
return [];
|
|
}
|
|
|
|
$list = [];
|
|
foreach ($attachments as $item) {
|
|
if (!is_array($item)) {
|
|
continue;
|
|
}
|
|
|
|
$fileUrl = trim((string)($item['file_url'] ?? ''));
|
|
if ($fileUrl === '') {
|
|
continue;
|
|
}
|
|
|
|
$name = trim((string)($item['name'] ?? ''));
|
|
$mimeType = trim((string)($item['mime_type'] ?? ''));
|
|
$fileType = trim((string)($item['file_type'] ?? ''));
|
|
|
|
if ($fileType === '') {
|
|
$path = parse_url('/' . $this->storage()->storagePath($fileUrl), PHP_URL_PATH) ?: '';
|
|
$extension = strtolower(pathinfo($path, PATHINFO_EXTENSION));
|
|
$fileType = $this->detectFileType($extension);
|
|
if ($mimeType === '') {
|
|
$mimeType = $this->mimeType($fileType, $extension);
|
|
}
|
|
}
|
|
|
|
$storedFileUrl = $this->storage()->storagePath($fileUrl);
|
|
$storedThumbnailUrl = $this->storage()->storagePath(trim((string)($item['thumbnail_url'] ?? ($fileType === 'image' ? $fileUrl : ''))));
|
|
|
|
$list[] = [
|
|
'file_id' => trim((string)($item['file_id'] ?? md5($storedFileUrl))),
|
|
'file_url' => $forStorage
|
|
? '/' . $storedFileUrl
|
|
: ($request ? $this->storage()->normalizeUrl($fileUrl, $request) : $fileUrl),
|
|
'thumbnail_url' => $forStorage
|
|
? ($storedThumbnailUrl !== '' ? '/' . $storedThumbnailUrl : '')
|
|
: ($request
|
|
? $this->storage()->normalizeUrl(trim((string)($item['thumbnail_url'] ?? ($fileType === 'image' ? $fileUrl : ''))), $request)
|
|
: trim((string)($item['thumbnail_url'] ?? ($fileType === 'image' ? $fileUrl : '')))),
|
|
'name' => $name,
|
|
'file_type' => $fileType ?: 'file',
|
|
'mime_type' => $mimeType,
|
|
];
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
public function detectFileType(string $extension): string
|
|
{
|
|
return $this->fileUploadService()->detectFileType($extension);
|
|
}
|
|
|
|
private function mimeType(string $fileType, string $extension): string
|
|
{
|
|
return $this->fileUploadService()->mimeType($fileType, $extension);
|
|
}
|
|
|
|
private function storage(): FileStorageService
|
|
{
|
|
return new FileStorageService();
|
|
}
|
|
|
|
private function fileUploadService(): FileUploadService
|
|
{
|
|
return new FileUploadService();
|
|
}
|
|
}
|