116 lines
2.4 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;
use Illuminate\Http\Request;
2017-12-29 19:56:56 +03:00
use App\Models\Common\Media;
2018-01-02 16:22:30 +03:00
use File;
2018-02-23 10:48:33 +03:00
use Storage;
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
*
2018-02-23 10:48:33 +03:00
* @param $id
* @return mixed
2017-09-28 18:10:13 +03:00
*/
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.
*
2018-02-23 10:48:33 +03:00
* @param $id
* @return mixed
2017-09-28 18:10:13 +03:00
*/
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.
*
2018-02-23 10:48:33 +03:00
* @param $id
2017-12-29 19:56:56 +03:00
* @return callable
*/
public function destroy($id, Request $request)
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);
if (!empty($request->input('page'))) {
switch ($request->input('page')) {
case 'setting':
setting()->set($request->input('key'), '');
setting()->save();
break;
default;
}
}
2017-12-29 19:56:56 +03:00
return back();
}
2017-09-28 18:10:13 +03:00
/**
* Get the full path of resource.
*
2018-02-23 10:48:33 +03:00
* @param $media
2017-09-28 18:10:13 +03:00
* @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)) {
2018-02-23 10:48:33 +03:00
$folders = explode('/', $media->directory);
// Check if company can access media
if ($folders[0] != session('company_id')) {
return false;
}
2018-01-02 16:22:30 +03:00
$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;
}
}