2018-04-02 07:02:12 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
use App\Interfaces\Controller;
|
|
|
|
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
|
|
|
* @package App\Http\Controllers\Admin
|
|
|
|
*/
|
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-04-03 00:04:32 +08:00
|
|
|
* @param Request $request
|
2018-04-02 07:02:12 +08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
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',
|
|
|
|
'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);
|
|
|
|
|
|
|
|
$file = $request->file('file');
|
2018-04-03 00:04:32 +08:00
|
|
|
$file_path = $this->fileSvc->saveFile($file, 'files');
|
2018-04-02 07:02:12 +08:00
|
|
|
|
|
|
|
$asset = new File();
|
2018-04-03 00:04:32 +08:00
|
|
|
$asset->name = $attrs['filename'];
|
2018-04-03 00:46:33 +08:00
|
|
|
$asset->description = $attrs['file_description'];
|
2018-04-03 00:04:32 +08:00
|
|
|
$asset->disk = config('filesystems.public_files');
|
2018-04-02 07:02:12 +08:00
|
|
|
$asset->path = $file_path;
|
2018-04-03 01:55:37 +08:00
|
|
|
$asset->public = false; // need to be logged in to see. default (for now)
|
2018-04-02 07:02:12 +08:00
|
|
|
$asset->ref_model = $attrs['ref_model'];
|
|
|
|
$asset->ref_model_id = $attrs['ref_model_id'];
|
|
|
|
|
|
|
|
$asset->save();
|
|
|
|
|
|
|
|
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-04-03 00:04:32 +08:00
|
|
|
* @param $id
|
2018-04-02 07:02:12 +08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
}
|