akaunting/app/Http/Controllers/Common/Uploads.php

169 lines
3.9 KiB
PHP
Raw Normal View History

2017-09-28 15:10:13 +00:00
<?php
namespace App\Http\Controllers\Common;
2019-11-16 07:21:14 +00:00
use App\Abstracts\Http\Controller;
2017-12-29 16:56:56 +00:00
use App\Models\Common\Media;
2021-05-08 15:18:00 +00:00
use App\Traits\Uploads as Helper;
2021-05-11 20:43:19 +00:00
use Illuminate\Support\Facades\Storage;
2021-05-08 15:18:00 +00:00
use Illuminate\Http\Request;
2017-09-28 15:10:13 +00:00
class Uploads extends Controller
{
2021-05-08 15:18:00 +00:00
use Helper;
2017-09-28 15:10:13 +00:00
/**
2017-09-28 15:18:37 +00:00
* Get the specified resource.
2017-09-28 15:10:13 +00:00
*
2018-02-23 07:48:33 +00:00
* @param $id
* @return mixed
2017-09-28 15:10:13 +00:00
*/
2018-01-02 13:22:30 +00:00
public function get($id)
2017-09-28 15:10:13 +00:00
{
2019-02-27 12:42:24 +00:00
try {
$media = Media::find($id);
} catch (\Exception $e) {
2019-11-16 07:21:14 +00:00
return response(null, 204);
2019-02-27 12:42:24 +00:00
}
2018-01-02 13:22:30 +00:00
2017-09-28 15:10:13 +00:00
// Get file path
2021-06-17 13:40:12 +00:00
if (!$this->getMediaPathOnStorage($media)) {
2019-11-16 07:21:14 +00:00
return response(null, 204);
2017-09-28 15:10:13 +00:00
}
2021-06-17 13:40:12 +00:00
return $this->streamMedia($media);
2017-09-28 15:10:13 +00:00
}
2019-02-02 08:15:24 +00:00
/**
* Get the specified resource.
*
* @param $id
* @return mixed
*/
2019-02-02 11:02:26 +00:00
public function show($id, Request $request)
2019-02-02 08:15:24 +00:00
{
$file = false;
$options = false;
2019-02-02 11:02:26 +00:00
$column_name = 'attachment';
if ($request->has('column_name')) {
$column_name = $request->get('column_name');
}
2019-02-02 08:15:24 +00:00
if ($request->has('page')) {
$options = [
'page' => $request->get('page'),
'key' => $request->get('key'),
];
}
2019-02-27 12:42:24 +00:00
try {
$media = Media::find($id);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => true,
'data' => [],
'message' => 'null',
'html' => '',
]);
}
2019-02-02 08:15:24 +00:00
// Get file path
2021-06-17 13:40:12 +00:00
if (!$this->getMediaPathOnStorage($media)) {
2019-02-02 08:15:24 +00:00
return response()->json([
'success' => false,
'error' => true,
'data' => [],
'message' => 'null',
'html' => '',
]);
}
$file = $media;
2022-06-01 07:15:55 +00:00
$html = view('components.media.file', compact('file', 'column_name', 'options'))->render();
2019-02-02 08:15:24 +00:00
return response()->json([
'success' => true,
'error' => false,
'data' => [],
'message' => 'null',
'html' => $html,
]);
}
2017-09-28 15:10:13 +00:00
/**
* Download the specified resource.
*
2018-02-23 07:48:33 +00:00
* @param $id
* @return mixed
2017-09-28 15:10:13 +00:00
*/
2018-01-02 13:22:30 +00:00
public function download($id)
2017-09-28 15:10:13 +00:00
{
2019-02-27 12:42:24 +00:00
try {
$media = Media::find($id);
} catch (\Exception $e) {
return false;
}
2018-01-02 13:22:30 +00:00
2017-09-28 15:10:13 +00:00
// Get file path
2021-06-17 13:40:12 +00:00
if (!$this->getMediaPathOnStorage($media)) {
2017-09-28 15:10:13 +00:00
return false;
}
2021-06-17 13:40:12 +00:00
return $this->streamMedia($media);
2017-09-28 15:10:13 +00:00
}
2017-12-29 16:56:56 +00:00
/**
* Destroy the specified resource.
*
2018-02-23 07:48:33 +00:00
* @param $id
2017-12-29 16:56:56 +00:00
* @return callable
*/
public function destroy($id, Request $request)
2017-12-29 16:56:56 +00:00
{
$return = back();
if ($request->has('ajax') && $request->get('ajax')) {
$return = [
'success' => true,
'errors' => false,
'message' => '',
'redirect' => $request->get('redirect')
];
}
2019-02-27 12:42:24 +00:00
try {
$media = Media::find($id);
} catch (\Exception $e) {
return $return;
2019-02-27 12:42:24 +00:00
}
2017-12-29 16:56:56 +00:00
// Get file path
2021-05-08 15:18:00 +00:00
if (!$path = $this->getMediaPathOnStorage($media)) {
2018-01-02 13:22:30 +00:00
$message = trans('messages.warning.deleted', ['name' => $media->basename, 'text' => $media->basename]);
2017-12-29 16:56:56 +00:00
flash($message)->warning()->important();
2017-12-29 16:56:56 +00:00
return $return;
2018-01-02 13:22:30 +00:00
}
2017-12-29 16:56:56 +00:00
$media->delete(); //will not delete files
2021-05-11 20:43:19 +00:00
Storage::delete($path);
2018-01-02 13:22:30 +00: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 16:56:56 +00:00
}
2017-09-28 15:10:13 +00:00
}