staffmanager/app/Http/Controllers/TeamFileController.php

153 lines
4.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
2020-11-02 21:44:05 +00:00
// Most of these namespaces have no effect on the code, however, they're used by IDEs so they can resolve return types and for PHPDocumentor as well
use App\TeamFile;
2020-11-02 21:44:05 +00:00
use App\Http\Requests\UploadFileRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\FileNotFoundException;
2020-11-02 21:44:05 +00:00
// Documentation-purpose namespaces
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Contts\View\Factory;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
class TeamFileController extends Controller
{
/**
* Display a listing of the resource.
*
* @param Request $request
2020-11-02 21:44:05 +00:00
* @return Application|Factory|View|Response
*/
public function index(Request $request)
{
2020-12-08 03:09:17 +00:00
$this->authorize('index');
if (is_null(Auth::user()->currentTeam))
{
$request->session()->flash('error', 'Please choose a team before viewing it\'s files.');
return redirect()->to(route('teams.index'));
}
return view('dashboard.teams.team-files')
2020-11-02 21:44:05 +00:00
->with('files', TeamFile::with('team', 'uploader')->paginate(6));
}
/**
* Store a newly created resource in storage.
*
2020-11-02 21:44:05 +00:00
* @param UploadFileRequest $request
* @return RedirectResponse
*/
2020-11-02 21:44:05 +00:00
public function store(UploadFileRequest $request)
{
2020-12-08 03:09:17 +00:00
$this->authorize('store');
2020-11-02 21:44:05 +00:00
$upload = $request->file('file');
$file = $upload->store('uploads');
$originalFileName = $upload->getClientOriginalName();
$originalFileExtension = $upload->extension();
$originalFileSize = $upload->getSize();
$fileEntry = TeamFile::create([
'uploaded_by' => Auth::user()->id,
'team_id' => Auth::user()->currentTeam->id,
'name' => $originalFileName,
'caption' => $request->caption,
'description' => $request->description,
'fs_location' => $file,
'extension' => $originalFileExtension,
'size' => $originalFileSize
]);
if ($fileEntry && !is_bool($file))
{
$request->session()->flash('success', 'File uploaded successfully!');
return redirect()->back();
}
$request->session()->flash('error', 'There was an unknown error whilst trying to upload your file.');
return redirect()->back();
}
public function download(Request $request, TeamFile $teamFile)
{
2020-12-08 03:09:17 +00:00
$this->authorize('download');
try
{
2020-11-03 03:00:03 +00:00
return Storage::download($teamFile->fs_location, $teamFile->name);
}
catch (FileNotFoundException $ex)
{
$request->session()->flash('error', 'Sorry, but the requested file could not be found in storage. Sometimes, files may be physically deleted by admins, but not from the app\'s database.');
return redirect()->back();
}
}
/**
* Show the form for editing the specified resource.
*
* @param \App\TeamFile $teamFile
2020-11-02 21:44:05 +00:00
* @return Response
*/
public function edit(TeamFile $teamFile)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\TeamFile $teamFile
2020-11-02 21:44:05 +00:00
* @return Response
*/
public function update(Request $request, TeamFile $teamFile)
{
//
}
/**
* Remove the specified resource from storage.
*
2020-11-02 21:44:05 +00:00
* @param Request $request
* @param \App\TeamFile $teamFile
* @return RedirectResponse
*/
2020-11-02 21:44:05 +00:00
public function destroy(Request $request, TeamFile $teamFile)
{
2020-12-08 03:09:17 +00:00
$this->authorize('delete');
2020-11-02 21:44:05 +00:00
try
{
2020-11-03 03:00:03 +00:00
Storage::delete($teamFile->fs_location);
2020-11-02 21:44:05 +00:00
$teamFile->delete();
$request->session()->flash('success', 'File deleted successfully.');
}
catch (\Exception $ex)
{
$request->session()->flash('error', 'There was an error deleting the file: ' . $ex->getMessage());
}
return redirect()->back();
}
}