fix pictures 5
This commit is contained in:
@@ -51,7 +51,21 @@ class FileController extends Controller
|
||||
$file_name = $file_name_src.'.'.$data['ext'];
|
||||
$data['has_thumb'] = false;
|
||||
$data['size'] = $uploaded->getSize();
|
||||
Storage::disk('public')->putFileAs($path, $uploaded, $file_name);
|
||||
$stored = Storage::disk('public')->putFileAs($path, $uploaded, $file_name);
|
||||
if (! $stored) {
|
||||
return response()->json(['message' => 'ذخیره فایل روی سرور ناموفق بود'], 500);
|
||||
}
|
||||
|
||||
$storageFile = storage_path('app/public/'.$path.'/'.$file_name);
|
||||
$publicFile = public_path('storage/'.$path.'/'.$file_name);
|
||||
if (! is_file($publicFile) && is_file($storageFile)) {
|
||||
$publicDir = dirname($publicFile);
|
||||
if (! is_dir($publicDir)) {
|
||||
mkdir($publicDir, 0755, true);
|
||||
}
|
||||
@copy($storageFile, $publicFile);
|
||||
}
|
||||
|
||||
$data['src'] = 'storage/'.$path.'/'.$file_name;
|
||||
$data['user_id'] = auth()->id();
|
||||
$data['sts'] = 1;
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
class PublicStorageController extends Controller
|
||||
{
|
||||
public function show(Request $request, string $path): BinaryFileResponse
|
||||
{
|
||||
$path = $this->sanitizeRelativePath($path);
|
||||
if ($path === '') {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
$fullPath = $this->resolveReadablePath($path);
|
||||
if ($fullPath === null) {
|
||||
abort(404);
|
||||
}
|
||||
|
||||
return response()->file($fullPath);
|
||||
}
|
||||
|
||||
private function sanitizeRelativePath(string $path): string
|
||||
{
|
||||
$path = str_replace('\\', '/', $path);
|
||||
$path = ltrim($path, '/');
|
||||
|
||||
if ($path === '' || str_contains($path, '..')) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return $path;
|
||||
}
|
||||
|
||||
private function resolveReadablePath(string $path): ?string
|
||||
{
|
||||
foreach ($this->candidatePaths($path) as $fullPath) {
|
||||
if (is_file($fullPath) && is_readable($fullPath)) {
|
||||
return $fullPath;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function candidatePaths(string $path): array
|
||||
{
|
||||
return [
|
||||
storage_path('app/public/'.$path),
|
||||
public_path('storage/'.$path),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user