mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-10 23:11:06 +03:30
120 lines
4.6 KiB
PHP
120 lines
4.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Intervention\Image\ImageManager;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use App\Media;
|
|
|
|
class UploadController extends Controller
|
|
{
|
|
public function uploadFile(Request $request)
|
|
{
|
|
$FileType = 'image';
|
|
$rules = [
|
|
"file" => "required|mimes:pdf,jpeg,png,jpg,doc,docx|max:2048"
|
|
];
|
|
$this->validate($request, $rules);
|
|
|
|
if(substr( $request->file('file')->getMimeType(), 0, 12 ) == 'application/'){
|
|
$FileType = 'document';
|
|
}
|
|
|
|
if ($request->model_id !== null && is_numeric($request->model_id)) {
|
|
$Model = 'App\\'.$request->model_type;
|
|
$Object = $Model::findOrFail($request->model_id);
|
|
if ($Object) {
|
|
if($FileType == 'image'){
|
|
$Image = $request->file('file');
|
|
$Manager = new ImageManager(array('driver' => 'imagick'));
|
|
$InterventionImage = $Manager->make($Image);
|
|
$Mime = explode('/', $InterventionImage->mime());
|
|
|
|
$FileName = time().'.'.$Mime[1];
|
|
$Path = 'media'.'/TestResults'.'/'.$Object->id .'/'.$FileName;
|
|
|
|
Storage::disk('public')->makeDirectory('TestResults'.'/'.$Object->id);
|
|
$InterventionImage->save(public_path($Path));
|
|
}
|
|
else if($FileType == 'document'){
|
|
$UploadedFile = $request->file('file');
|
|
$FileName = time().'_'.$request->file('file')->getClientOriginalName();
|
|
$Path = 'media'.'/TestResults'.'/'.$Object->id;
|
|
Storage::disk('public')->makeDirectory('TestResults'.'/'.$Object->id);
|
|
$UploadedFile->move($Path, $FileName);
|
|
$Path = $Path . '/' . $FileName;
|
|
}
|
|
|
|
$MediaData = [
|
|
'model_id' => $Object->id,
|
|
'batch_id' => $request->batch_id,
|
|
'model_type' => 'App\\'.$request->model_type,
|
|
'path' => $Path,
|
|
'user_id' => auth('api')->user()->id,
|
|
'file_name' => $FileName,
|
|
'file_type' => $FileType
|
|
];
|
|
$Media = Media::create($MediaData);
|
|
}
|
|
}
|
|
else {
|
|
if($FileType == 'image'){
|
|
$Image = $request->file('file');
|
|
$Manager = new ImageManager(array('driver' => 'imagick'));
|
|
$InterventionImage = $Manager->make($Image);
|
|
$Mime = explode('/', $InterventionImage->mime());
|
|
|
|
$FileName = time().'.'.$Mime[1];
|
|
$Path = 'media'.'/Temp'.'/'.$request->batch_id .'/'.$FileName;
|
|
|
|
Storage::disk('public')->makeDirectory('Temp'.'/'.$request->batch_id);
|
|
$InterventionImage->save(public_path($Path));
|
|
}
|
|
else if($FileType == 'document'){
|
|
$UploadedFile = $request->file('file');
|
|
$FileName = time().'_'.$request->file('file')->getClientOriginalName();
|
|
$Path = 'media'.'/Temp'.'/'.$request->batch_id;
|
|
Storage::disk('public')->makeDirectory('Temp'.'/'.$request->batch_id);
|
|
$UploadedFile->move($Path, $FileName);
|
|
$Path = $Path . '/' . $FileName;
|
|
}
|
|
|
|
$MediaData = [
|
|
'model_id' => null,
|
|
'batch_id' => $request->batch_id,
|
|
'model_type' => 'App\\'.$request->model_type,
|
|
'path' => $Path,
|
|
'user_id' => auth('api')->user()->id,
|
|
'file_name' => $FileName,
|
|
'file_type' => $FileType
|
|
];
|
|
$Media = Media::create($MediaData);
|
|
}
|
|
return \MessageResponse::success(trans('messages.success_message'), null, $Media);
|
|
}
|
|
|
|
public function deleteFile(Request $request)
|
|
{
|
|
if ($request->model_id != null && is_numeric($request->model_id)) {
|
|
$Media = Media::findOrFail($request->media_id);
|
|
if($Media){
|
|
if (\File::exists(public_path($Media->path))) {
|
|
\File::delete(public_path($Media->path));
|
|
}
|
|
$Media->delete();
|
|
}
|
|
}
|
|
else {
|
|
$Media = Media::findOrFail($request->media_id);
|
|
if($Media){
|
|
if(\File::exists(public_path($Media->path))){
|
|
\File::delete(public_path($Media->path));
|
|
}
|
|
$Media->delete();
|
|
}
|
|
}
|
|
return \MessageResponse::success(trans('messages.success_message'));
|
|
}
|
|
}
|