2018-04-02 07:02:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Controller;
|
2018-04-02 07:02:12 +08:00
|
|
|
use App\Models\File;
|
2018-04-03 00:04:32 +08:00
|
|
|
use App\Services\FileService;
|
2018-04-02 07:02:12 +08:00
|
|
|
use Flash;
|
|
|
|
use Illuminate\Http\Request;
|
2018-04-03 00:04:32 +08:00
|
|
|
use Illuminate\Support\Facades\Input;
|
2018-04-02 07:02:12 +08:00
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Log;
|
2018-04-03 00:04:32 +08:00
|
|
|
use Validator;
|
2018-04-02 07:02:12 +08:00
|
|
|
|
|
|
|
/**
|
2018-04-03 02:42:24 +08:00
|
|
|
* Class FileController
|
2018-04-02 07:02:12 +08:00
|
|
|
*/
|
2018-04-03 02:42:24 +08:00
|
|
|
class FileController extends Controller
|
2018-04-02 07:02:12 +08:00
|
|
|
{
|
2018-04-03 00:04:32 +08:00
|
|
|
private $fileSvc;
|
|
|
|
|
|
|
|
public function __construct(FileService $fileSvc)
|
|
|
|
{
|
|
|
|
$this->fileSvc = $fileSvc;
|
|
|
|
}
|
|
|
|
|
2018-04-02 07:02:12 +08:00
|
|
|
/**
|
|
|
|
* Store a newly file
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 00:04:32 +08:00
|
|
|
* @param Request $request
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 06:34:58 +08:00
|
|
|
* @throws \Hashids\HashidsException
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2018-04-02 07:02:12 +08:00
|
|
|
*/
|
2018-04-03 00:04:32 +08:00
|
|
|
public function store(Request $request)
|
2018-04-02 07:02:12 +08:00
|
|
|
{
|
|
|
|
$attrs = $request->post();
|
|
|
|
|
2018-04-03 00:04:32 +08:00
|
|
|
// Not using a form validation here because when it redirects,
|
|
|
|
// it leaves the parent forms all blank, even though it goes
|
|
|
|
// back to the right place. So just manually validate
|
|
|
|
$validator = Validator::make($request->all(), [
|
2018-04-03 00:46:33 +08:00
|
|
|
'filename' => 'required',
|
|
|
|
'file_description' => 'nullable',
|
2018-08-27 00:40:04 +08:00
|
|
|
'file' => 'required|file',
|
2018-04-03 00:04:32 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
if ($validator->fails()) {
|
|
|
|
return redirect()->back()->withInput(Input::all())->withErrors($validator);
|
|
|
|
}
|
|
|
|
|
2018-04-02 07:02:12 +08:00
|
|
|
Log::info('Uploading files', $attrs);
|
|
|
|
|
2018-04-03 06:34:58 +08:00
|
|
|
$file = $request->file('file');
|
|
|
|
$this->fileSvc->saveFile($file, 'files', [
|
|
|
|
'name' => $attrs['filename'],
|
2018-04-03 07:37:41 +08:00
|
|
|
'description' => $attrs['file_description'],
|
2018-04-03 06:34:58 +08:00
|
|
|
'ref_model' => $attrs['ref_model'],
|
|
|
|
'ref_model_id' => $attrs['ref_model_id'],
|
|
|
|
]);
|
2018-04-02 07:02:12 +08:00
|
|
|
|
|
|
|
Flash::success('Files uploaded successfully.');
|
2018-04-03 00:04:32 +08:00
|
|
|
return redirect()->back();
|
2018-04-02 07:02:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the file from storage.
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 00:04:32 +08:00
|
|
|
* @param $id
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-04-03 06:49:04 +08:00
|
|
|
* @throws \Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
2018-04-02 07:02:12 +08:00
|
|
|
*/
|
2018-04-03 00:04:32 +08:00
|
|
|
public function destroy($id)
|
2018-04-02 07:02:12 +08:00
|
|
|
{
|
|
|
|
$file = File::find($id);
|
|
|
|
if (!$file) {
|
|
|
|
Flash::error('File doesn\'t exist');
|
2018-04-03 00:04:32 +08:00
|
|
|
return redirect()->back();
|
2018-04-02 07:02:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Storage::disk(config('filesystems.public_files'))->delete($file->path);
|
|
|
|
$file->delete();
|
|
|
|
|
|
|
|
Flash::success('File deleted successfully.');
|
2018-04-03 00:04:32 +08:00
|
|
|
return redirect()->back();
|
2018-04-02 07:02:12 +08:00
|
|
|
}
|
|
|
|
}
|