هپشلث عحمخشی
This commit is contained in:
@@ -1,83 +1,73 @@
|
||||
<?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();
|
||||
$data = $request->all();
|
||||
$data['original_name'] = $uploaded->getClientOriginalName();
|
||||
$data['ext'] = $uploaded->getClientOriginalExtension()
|
||||
?: $uploaded->extension()
|
||||
?: 'jpg';
|
||||
if (! isset($data['title']) || is_null($data['title'])) {
|
||||
$data['title'] = $data['original_name'];
|
||||
}
|
||||
$file_name_src = md5(time().$data['original_name']);
|
||||
$file_name = $file_name_src.'.'.$data['ext'];
|
||||
$data['has_thumb'] = false;
|
||||
$data['size'] = $uploaded->getSize();
|
||||
$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;
|
||||
$relativePath = $data['src'];
|
||||
$file_create = File::create($data);
|
||||
$fullSrc = File::publicUrl($relativePath);
|
||||
|
||||
$payload = $file_create->fresh()->toArray();
|
||||
$payload['path'] = $relativePath;
|
||||
$payload['full_src'] = $fullSrc;
|
||||
$payload['src'] = $fullSrc;
|
||||
|
||||
return response()->json($payload);
|
||||
}
|
||||
}
|
||||
<?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();
|
||||
$data = $request->all();
|
||||
$data['original_name'] = $uploaded->getClientOriginalName();
|
||||
$data['ext'] = $uploaded->getClientOriginalExtension()
|
||||
?: $uploaded->extension()
|
||||
?: 'jpg';
|
||||
if (! isset($data['title']) || is_null($data['title'])) {
|
||||
$data['title'] = $data['original_name'];
|
||||
}
|
||||
$file_name_src = md5(time().$data['original_name']);
|
||||
$file_name = $file_name_src.'.'.$data['ext'];
|
||||
$data['has_thumb'] = false;
|
||||
$data['size'] = $uploaded->getSize();
|
||||
$stored = Storage::disk('public')->putFileAs($path, $uploaded, $file_name);
|
||||
if (! $stored || ! Storage::disk('public')->exists($stored)) {
|
||||
return response()->json(['message' => 'ذخیره فایل روی سرور ناموفق بود'], 500);
|
||||
}
|
||||
|
||||
$data['src'] = 'storage/'.$stored;
|
||||
$data['user_id'] = auth()->id();
|
||||
$data['sts'] = 1;
|
||||
$relativePath = $data['src'];
|
||||
$file_create = File::create($data);
|
||||
$fullSrc = File::publicUrl($relativePath);
|
||||
|
||||
$payload = $file_create->fresh()->toArray();
|
||||
$payload['path'] = $relativePath;
|
||||
$payload['full_src'] = $fullSrc;
|
||||
$payload['src'] = $fullSrc;
|
||||
|
||||
return response()->json($payload);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user