101 lines
1.9 KiB
PHP
Raw Normal View History

2017-09-28 18:10:13 +03:00
<?php
namespace App\Http\Controllers\Common;
use App\Http\Controllers\Controller;
2017-12-29 19:56:56 +03:00
use App\Models\Common\Media;
2017-09-28 18:10:13 +03:00
use Storage;
2018-01-02 16:22:30 +03:00
use File;
2017-09-28 18:10:13 +03:00
class Uploads extends Controller
{
/**
2017-09-28 18:18:37 +03:00
* Get the specified resource.
2017-09-28 18:10:13 +03:00
*
* @param $folder
* @param $file
* @return boolean|Response
*/
2018-01-02 16:22:30 +03:00
public function get($id)
2017-09-28 18:10:13 +03:00
{
2018-01-02 16:22:30 +03:00
$media = Media::find($id);
2017-09-28 18:10:13 +03:00
// Get file path
2018-01-02 16:22:30 +03:00
if (!$path = $this->getPath($media)) {
2017-09-28 18:10:13 +03:00
return false;
}
return response()->file($path);
}
/**
* Download the specified resource.
*
* @param $folder
* @param $file
* @return boolean|Response
*/
2018-01-02 16:22:30 +03:00
public function download($id)
2017-09-28 18:10:13 +03:00
{
2018-01-02 16:22:30 +03:00
$media = Media::find($id);
2017-09-28 18:10:13 +03:00
// Get file path
2018-01-02 16:22:30 +03:00
if (!$path = $this->getPath($media)) {
2017-09-28 18:10:13 +03:00
return false;
}
return response()->download($path);
}
2017-12-29 19:56:56 +03:00
/**
* Destroy the specified resource.
*
* @param $folder
* @param $file
* @return callable
*/
2018-01-02 16:22:30 +03:00
public function destroy($id)
2017-12-29 19:56:56 +03:00
{
$media = Media::find($id);
// Get file path
2018-01-02 16:22:30 +03:00
if (!$path = $this->getPath($media)) {
$message = trans('messages.warning.deleted', ['name' => $media->basename, 'text' => $media->basename]);
2017-12-29 19:56:56 +03:00
flash($message)->warning();
return back();
2018-01-02 16:22:30 +03:00
}
2017-12-29 19:56:56 +03:00
$media->delete(); //will not delete files
2018-01-02 16:22:30 +03:00
File::delete($path);
2017-12-29 19:56:56 +03:00
return back();
}
2017-09-28 18:10:13 +03:00
/**
* Get the full path of resource.
*
* @param $folder
* @param $file
* @return boolean|string
*/
2018-01-02 16:22:30 +03:00
protected function getPath($media)
2017-09-28 18:10:13 +03:00
{
2018-01-02 16:22:30 +03:00
$path = $media->basename;
2017-09-28 18:10:13 +03:00
2018-01-02 16:22:30 +03:00
if (!empty($media->directory)) {
$path = $media->directory . '/' . $media->basename;
}
2017-09-28 18:10:13 +03:00
if (!Storage::exists($path)) {
return false;
}
$full_path = Storage::path($path);
return $full_path;
}
}