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
|
|
|
|
|
|
|
/**
|
|
|
|
* Class FilesController
|
|
|
|
* @package App\Http\Controllers\Admin
|
|
|
|
*/
|
|
|
|
class FilesController extends Controller
|
|
|
|
{
|
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(), [
|
|
|
|
'filename' => 'required',
|
|
|
|
'file' => 'required|file'
|
|
|
|
]);
|
|
|
|
|
|
|
|
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'];
|
|
|
|
$asset->disk = config('filesystems.public_files');
|
2018-04-02 07:02:12 +08:00
|
|
|
$asset->path = $file_path;
|
|
|
|
$asset->public = true;
|
|
|
|
$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
|
|
|
}
|
|
|
|
}
|