69 lines
2.1 KiB
PHP
69 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Activity\app\Services;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class FileManagementService
|
|
{
|
|
//-- store file
|
|
public static function storeFile($file, $uploadedFolderName, $model)
|
|
{
|
|
try {
|
|
$originalFileName = $file->getClientOriginalName();
|
|
$modifiedFileName = date('YmdHis').'_'.uniqid().'.'.$originalFileName;
|
|
|
|
$file->storeAs($uploadedFolderName, $modifiedFileName, 'public_uploads'); // This line uses 'public_uploads' disk
|
|
|
|
$model->image = $modifiedFileName;
|
|
$model->image_path = $uploadedFolderName.'/'.$modifiedFileName;
|
|
$model->save();
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
toastr()->error('Something went wrong.');
|
|
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
//-- update file
|
|
public static function uploadFile($file, $uploadedFolderName, $filePath, $model)
|
|
{
|
|
try {
|
|
if ($filePath && Storage::disk('public_uploads')->exists($filePath)) {
|
|
Storage::disk('public_uploads')->delete($filePath);
|
|
}
|
|
|
|
$originalFileName = $file->getClientOriginalName();
|
|
$modifiedFileName = date('YmdHis').'_'.uniqid().'.'.$originalFileName;
|
|
|
|
$file->storeAs($uploadedFolderName, $modifiedFileName, 'public_uploads'); // This line uses 'public_uploads' disk
|
|
|
|
$model->image = $modifiedFileName;
|
|
$model->image_path = $uploadedFolderName.'/'.$modifiedFileName;
|
|
|
|
$model->save();
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
toastr()->error('Something went wrong.');
|
|
return back();
|
|
}
|
|
}
|
|
|
|
//-- delete file
|
|
public static function deleteFile($filePath)
|
|
{
|
|
try {
|
|
if ($filePath && Storage::disk('public_uploads')->exists($filePath)) {
|
|
Storage::disk('public_uploads')->delete($filePath);
|
|
} else {
|
|
toastr()->error('File Not wrong.');
|
|
}
|
|
} catch (\Throwable $th) {
|
|
report($th);
|
|
toastr()->error('Something went wrong.');
|
|
return back();
|
|
}
|
|
}
|
|
}
|