169 lines
3.8 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;
2017-12-29 19:56:56 +03:00
use App\Models\Common\Media;
2021-05-08 18:18:00 +03:00
use App\Traits\Uploads as Helper;
2021-05-11 23:43:19 +03:00
use Illuminate\Support\Facades\Storage;
2021-05-08 18:18:00 +03:00
use Illuminate\Http\Request;
2017-09-28 18:10:13 +03:00
class Uploads extends Controller
{
2021-05-08 18:18:00 +03:00
use Helper;
2017-09-28 18:10:13 +03:00
/**
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
2021-06-17 16:40:12 +03:00
if (!$this->getMediaPathOnStorage($media)) {
2019-11-16 10:21:14 +03:00
return response(null, 204);
2017-09-28 18:10:13 +03:00
}
2021-06-17 16:40:12 +03:00
return $this->streamMedia($media);
2017-09-28 18:10:13 +03:00
}
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
2021-06-17 16:40:12 +03:00
if (!$this->getMediaPathOnStorage($media)) {
2019-02-02 11:15:24 +03:00
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
2021-06-17 16:40:12 +03:00
if (!$this->getMediaPathOnStorage($media)) {
2017-09-28 18:10:13 +03:00
return false;
}
2021-06-17 16:40:12 +03:00
return $this->streamMedia($media);
2017-09-28 18:10:13 +03:00
}
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
{
$return = back();
if ($request->has('ajax') && $request->get('ajax')) {
$return = [
'success' => true,
'errors' => false,
'message' => '',
'redirect' => $request->get('redirect')
];
}
2019-02-27 15:42:24 +03:00
try {
$media = Media::find($id);
} catch (\Exception $e) {
return $return;
2019-02-27 15:42:24 +03:00
}
2017-12-29 19:56:56 +03:00
// Get file path
2021-05-08 18:18:00 +03:00
if (!$path = $this->getMediaPathOnStorage($media)) {
2018-01-02 16:22:30 +03:00
$message = trans('messages.warning.deleted', ['name' => $media->basename, 'text' => $media->basename]);
2017-12-29 19:56:56 +03:00
flash($message)->warning()->important();
2017-12-29 19:56:56 +03:00
return $return;
2018-01-02 16:22:30 +03:00
}
2017-12-29 19:56:56 +03:00
$media->delete(); //will not delete files
2021-05-11 23:43:19 +03:00
Storage::delete($path);
2018-01-02 16:22:30 +03:00
if (!empty($request->input('page'))) {
switch ($request->input('page')) {
case 'setting':
setting()->set($request->input('key'), '');
setting()->save();
break;
}
}
return $return;
2017-12-29 19:56:56 +03:00
}
2017-09-28 18:10:13 +03:00
}