Post Montzar-covered buy/sell to Zarin/Tahesab/Pargar using montzar_account_* settings, and harden public storage URL/MIME handling for uploads. Co-authored-by: Cursor <cursoragent@cursor.com>
103 lines
3.4 KiB
PHP
103 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\File;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class FileController extends Controller
|
|
{
|
|
/**
|
|
* make directory in Public Storage By Date
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function createDir(): string
|
|
{
|
|
$now = Verta();
|
|
$path = preg_replace('/-/', '/', $now->formatDate());
|
|
$disk = Storage::disk('public');
|
|
|
|
if (! $disk->exists($path)) {
|
|
$disk->makeDirectory($path);
|
|
$disk->makeDirectory($path.'/thumb');
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$uploaded = $request->file('file');
|
|
if (! $uploaded || ! $uploaded->isValid()) {
|
|
return response()->json(['message' => 'فایل بارگذاری نشد یا خالی است'], 422);
|
|
}
|
|
if ($uploaded->getSize() < 1) {
|
|
return response()->json(['message' => 'فایل خالی است'], 422);
|
|
}
|
|
|
|
$path = $this->createDir();
|
|
$originalName = $uploaded->getClientOriginalName();
|
|
$ext = $this->resolveUploadExtension($uploaded);
|
|
$title = $request->input('title') ?: $originalName;
|
|
$file_name_src = md5(time().$originalName);
|
|
$file_name = $file_name_src.'.'.$ext;
|
|
$stored = Storage::disk('public')->putFileAs($path, $uploaded, $file_name);
|
|
if (! $stored || ! Storage::disk('public')->exists($stored)) {
|
|
return response()->json(['message' => 'ذخیره فایل روی سرور ناموفق بود'], 500);
|
|
}
|
|
|
|
$relativePath = 'storage/'.$stored;
|
|
$file_create = File::create([
|
|
'original_name' => $originalName,
|
|
'ext' => $ext,
|
|
'title' => $title,
|
|
'has_thumb' => false,
|
|
'size' => $uploaded->getSize(),
|
|
'src' => $relativePath,
|
|
'user_id' => auth()->id(),
|
|
]);
|
|
$fullSrc = File::publicUrl($relativePath);
|
|
|
|
$payload = $file_create->fresh()->toArray();
|
|
$payload['path'] = $relativePath;
|
|
$payload['full_src'] = $fullSrc;
|
|
$payload['src'] = $fullSrc;
|
|
|
|
return response()->json($payload);
|
|
}
|
|
|
|
/**
|
|
* پسوند واقعی فایل (MIME)، نه اسم اصلی مرورگر — آپلود بنر WebP است.
|
|
*/
|
|
private function resolveUploadExtension(\Illuminate\Http\UploadedFile $uploaded): string
|
|
{
|
|
$mime = strtolower((string) ($uploaded->getMimeType() ?: $uploaded->getClientMimeType()));
|
|
$fromMime = match (true) {
|
|
str_contains($mime, 'webp') => 'webp',
|
|
str_contains($mime, 'png') => 'png',
|
|
str_contains($mime, 'jpeg'), str_contains($mime, 'jpg') => 'jpg',
|
|
str_contains($mime, 'gif') => 'gif',
|
|
str_contains($mime, 'svg') => 'svg',
|
|
default => null,
|
|
};
|
|
if ($fromMime) {
|
|
return $fromMime;
|
|
}
|
|
|
|
$client = strtolower((string) $uploaded->getClientOriginalExtension());
|
|
if (in_array($client, ['webp', 'png', 'jpg', 'jpeg', 'gif', 'svg'], true)) {
|
|
return $client === 'jpeg' ? 'jpg' : $client;
|
|
}
|
|
|
|
$guessed = strtolower((string) $uploaded->extension());
|
|
if (in_array($guessed, ['webp', 'png', 'jpg', 'jpeg', 'gif', 'svg'], true)) {
|
|
return $guessed === 'jpeg' ? 'jpg' : $guessed;
|
|
}
|
|
|
|
return 'jpg';
|
|
}
|
|
}
|