190 lines
4.2 KiB
PHP
Raw Normal View History

2017-09-28 18:10:13 +03:00
<?php
namespace App\Http\Controllers\Common;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\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
{
2019-02-27 15:42:24 +03:00
try {
$media = Media::find($id);
} catch (\Exception $e) {
2019-11-16 10:21:14 +03:00
return response(null, 204);
2019-02-27 15:42:24 +03:00
}
2018-01-02 16:22:30 +03:00
2017-09-28 18:10:13 +03:00
// Get file path
2018-01-02 16:22:30 +03:00
if (!$path = $this->getPath($media)) {
2019-11-16 10:21:14 +03:00
return response(null, 204);
2017-09-28 18:10:13 +03:00
}
return response()->file($path);
}
2019-02-02 11:15:24 +03:00
/**
* Get the specified resource.
*
* @param $id
* @return mixed
*/
2019-02-02 14:02:26 +03:00
public function show($id, Request $request)
2019-02-02 11:15:24 +03:00
{
$file = false;
$options = false;
2019-02-02 14:02:26 +03:00
$column_name = 'attachment';
if ($request->has('column_name')) {
$column_name = $request->get('column_name');
}
2019-02-02 11:15:24 +03:00
if ($request->has('page')) {
$options = [
'page' => $request->get('page'),
'key' => $request->get('key'),
];
}
2019-02-27 15:42:24 +03:00
try {
$media = Media::find($id);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => true,
'data' => [],
'message' => 'null',
'html' => '',
]);
}
2019-02-02 11:15:24 +03:00
// Get file path
if (!$path = $this->getPath($media)) {
return response()->json([
'success' => false,
'error' => true,
'data' => [],
'message' => 'null',
'html' => '',
]);
}
$file = $media;
$html = view('partials.media.file', compact('file', 'column_name', 'options'))->render();
2019-02-02 11:15:24 +03:00
return response()->json([
'success' => true,
'error' => false,
'data' => [],
'message' => 'null',
'html' => $html,
]);
}
2017-09-28 18:10:13 +03:00
/**
* 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
{
2019-02-27 15:42:24 +03:00
try {
$media = Media::find($id);
} catch (\Exception $e) {
return false;
}
2018-01-02 16:22:30 +03:00
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
{
2019-02-27 15:42:24 +03:00
try {
$media = Media::find($id);
} catch (\Exception $e) {
return back();
}
2017-12-29 19:56:56 +03:00
// 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;
}
}
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-11-14 13:51:45 +03:00
if (!is_object($media)) {
return false;
}
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;
}
}