2018-04-03 07:37:41 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Frontend;
|
|
|
|
|
2019-07-16 03:44:31 +08:00
|
|
|
use App\Contracts\Controller;
|
2021-03-21 21:33:04 +08:00
|
|
|
use App\Models\Airline;
|
2018-04-03 07:37:41 +08:00
|
|
|
use App\Models\File;
|
|
|
|
use Auth;
|
|
|
|
use Flash;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class DownloadController
|
|
|
|
*/
|
|
|
|
class DownloadController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Show all of the available files
|
|
|
|
*/
|
|
|
|
public function index()
|
|
|
|
{
|
2021-03-21 21:33:04 +08:00
|
|
|
$airlines = Airline::where('active', 1)->count();
|
2018-04-03 07:37:41 +08:00
|
|
|
$files = File::orderBy('ref_model', 'asc')->get();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Group all the files but compound the model with the ID,
|
|
|
|
* since we can have multiple files for every `ref_model`
|
|
|
|
*/
|
2018-08-27 00:40:04 +08:00
|
|
|
$grouped_files = $files->groupBy(function ($item, $key) {
|
2018-04-03 07:37:41 +08:00
|
|
|
return $item['ref_model'].'_'.$item['ref_model_id'];
|
|
|
|
});
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The $group here looks like: App\Models\Airport_KAUS
|
|
|
|
* Split it into the $class and $id, and then change the
|
|
|
|
* name of the group to the object instance "name"
|
|
|
|
*/
|
|
|
|
$regrouped_files = [];
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($grouped_files as $group => $files) {
|
2018-04-03 07:37:41 +08:00
|
|
|
[$class, $id] = explode('_', $group);
|
|
|
|
$klass = new $class();
|
|
|
|
$obj = $klass->find($id);
|
|
|
|
|
|
|
|
$category = explode('\\', $class);
|
|
|
|
$category = end($category);
|
|
|
|
|
2021-03-21 21:33:04 +08:00
|
|
|
if ($category == 'Aircraft' && $airlines > 1) {
|
|
|
|
$group_name = $category.' > '.$obj->subfleet->airline->name.' '.$obj->icao.' '.$obj->registration;
|
|
|
|
} elseif ($category == 'Aircraft') {
|
2020-12-19 03:34:05 +08:00
|
|
|
$group_name = $category.' > '.$obj->icao.' '.$obj->registration;
|
|
|
|
} elseif ($category == 'Airport') {
|
|
|
|
$group_name = $category.' > '.$obj->icao.' : '.$obj->name.' ('.$obj->country.')';
|
2021-03-21 21:33:04 +08:00
|
|
|
} elseif ($category == 'Subfleet' && $airlines > 1) {
|
|
|
|
$group_name = $category.' > '.$obj->airline->name.' '.$obj->name;
|
2020-12-19 03:34:05 +08:00
|
|
|
} else {
|
|
|
|
$group_name = $category.' > '.$obj->name;
|
|
|
|
}
|
|
|
|
|
2018-04-03 07:37:41 +08:00
|
|
|
$regrouped_files[$group_name] = $files;
|
|
|
|
}
|
|
|
|
|
2021-03-21 21:33:04 +08:00
|
|
|
ksort($regrouped_files, SORT_STRING);
|
|
|
|
|
2018-04-03 07:37:41 +08:00
|
|
|
return view('downloads.index', [
|
|
|
|
'grouped_files' => $regrouped_files,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-21 21:33:04 +08:00
|
|
|
* Show the application dashboard
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-08-27 02:50:08 +08:00
|
|
|
* @param string $id
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Symfony\Component\HttpFoundation\StreamedResponse
|
2018-04-03 07:37:41 +08:00
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var File $file
|
|
|
|
*/
|
|
|
|
$file = File::find($id);
|
|
|
|
if (!$file) {
|
|
|
|
Flash::error('File doesn\'t exist');
|
|
|
|
return redirect()->back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allowed to download? If not, direct to login
|
|
|
|
if (!$file->public && !Auth::check()) {
|
2018-09-21 02:54:43 +08:00
|
|
|
return redirect(config('phpvms.login_redirect'));
|
2018-04-03 07:37:41 +08:00
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
$file->download_count++;
|
2018-04-03 07:37:41 +08:00
|
|
|
$file->save();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
if ($file->disk === 'public') {
|
2018-04-03 07:37:41 +08:00
|
|
|
$storage = Storage::disk('public');
|
|
|
|
return $storage->download($file->path, $file->filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Config for streamed response?
|
|
|
|
return redirect()->to($file->url);
|
|
|
|
}
|
|
|
|
}
|