2020-10-11 01:54:09 +00:00
< ? 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
2021-07-25 21:54:15 +00:00
use App\Exceptions\FileUploadException ;
use App\Services\TeamFileService ;
2020-10-11 01:54:09 +00:00
use App\TeamFile ;
2020-11-02 21:44:05 +00:00
use App\Http\Requests\UploadFileRequest ;
2020-10-11 01:54:09 +00:00
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 ;
2020-10-11 01:54:09 +00:00
class TeamFileController extends Controller
{
2021-07-25 21:54:15 +00:00
private $fileService ;
public function __construct ( TeamFileService $fileService ) {
$this -> fileService = $fileService ;
}
2020-10-11 01:54:09 +00:00
/**
* Display a listing of the resource .
*
* @ param Request $request
*/
public function index ( Request $request )
{
2020-12-21 01:02:05 +00:00
$this -> authorize ( 'index' , TeamFile :: class );
2020-12-08 03:09:17 +00:00
2020-10-11 01:54:09 +00:00
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 ));
2020-10-11 01:54:09 +00:00
}
/**
* Store a newly created resource in storage .
*
2020-11-02 21:44:05 +00:00
* @ param UploadFileRequest $request
* @ return RedirectResponse
2020-10-11 01:54:09 +00:00
*/
2020-11-02 21:44:05 +00:00
public function store ( UploadFileRequest $request )
2020-10-11 01:54:09 +00:00
{
2020-12-21 01:02:05 +00:00
$this -> authorize ( 'store' , TeamFile :: class );
2020-12-08 03:09:17 +00:00
2021-09-03 23:44:54 +00:00
if ( config ( 'demo.is_enabled' ))
{
return redirect ()
-> back ()
-> with ( 'error' , 'This feature is disabled' );
}
2021-07-25 21:54:15 +00:00
try {
$caption = $request -> caption ;
$description = $request -> description ;
2020-11-02 21:44:05 +00:00
2021-07-25 21:54:15 +00:00
$this -> fileService -> addFile ( $request -> file ( 'file' ), Auth :: user () -> id , Auth :: user () -> currentTeam -> id , $caption , $description );
return redirect ()
-> back ()
-> with ( 'success' , __ ( 'File uploaded successfully.' ));
} catch ( FileUploadException $uploadException ) {
return redirect ()
-> back ()
-> with ( 'error' , $uploadException -> getMessage ());
}
2020-11-02 21:44:05 +00:00
2020-10-11 01:54:09 +00:00
}
public function download ( Request $request , TeamFile $teamFile )
{
2020-12-21 01:02:05 +00:00
$this -> authorize ( 'download' , TeamFile :: class );
2020-12-08 03:09:17 +00:00
2020-10-11 01:54:09 +00:00
try
{
2020-11-03 03:00:03 +00:00
return Storage :: download ( $teamFile -> fs_location , $teamFile -> name );
2020-10-11 01:54:09 +00:00
}
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 ();
}
}
/**
* Remove the specified resource from storage .
*
2020-11-02 21:44:05 +00:00
* @ param Request $request
* @ param \App\TeamFile $teamFile
* @ return RedirectResponse
2020-10-11 01:54:09 +00:00
*/
2020-11-02 21:44:05 +00:00
public function destroy ( Request $request , TeamFile $teamFile )
2020-10-11 01:54:09 +00:00
{
2020-12-21 01:02:05 +00:00
$this -> authorize ( 'delete' , $teamFile );
2020-11-02 21:44:05 +00:00
2021-09-03 23:44:54 +00:00
if ( config ( 'demo.is_enabled' ))
{
return redirect ()
-> back ()
-> with ( 'error' , 'This feature is disabled' );
}
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 ();
2021-07-19 23:35:03 +00:00
$request -> session () -> flash ( 'success' , __ ( 'File deleted successfully.' ));
2020-11-02 21:44:05 +00:00
}
catch ( \Exception $ex )
{
2021-07-19 23:35:03 +00:00
$request -> session () -> flash ( 'error' , __ ( 'There was an error deleting the file: :msg' , [ 'msg' => $ex -> getMessage ()]));
2020-11-02 21:44:05 +00:00
}
return redirect () -> back ();
2020-10-11 01:54:09 +00:00
}
}