229 url for downloads (#369)
* Allow file uploads for subfleet * Allow URL to be used for a download * Remove old FileUploadRequest * Move file removal logic to service layer * Remove unused import * Remove unused packages
This commit is contained in:
@@ -11,8 +11,6 @@ use Illuminate\Http\Request;
|
|||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Laracasts\Flash\Flash;
|
use Laracasts\Flash\Flash;
|
||||||
use PragmaRX\Version\Package\Facade as Version;
|
|
||||||
use vierbergenlars\SemVer\version as semver;
|
|
||||||
|
|
||||||
class DashboardController extends Controller
|
class DashboardController extends Controller
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ namespace App\Http\Controllers\Admin;
|
|||||||
use App\Contracts\Controller;
|
use App\Contracts\Controller;
|
||||||
use App\Models\File;
|
use App\Models\File;
|
||||||
use App\Services\FileService;
|
use App\Services\FileService;
|
||||||
use Flash;
|
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Input;
|
use Illuminate\Support\Facades\Input;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Log;
|
use Illuminate\Support\Facades\Validator;
|
||||||
use Validator;
|
use Illuminate\Validation\Rule;
|
||||||
|
use Laracasts\Flash\Flash;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class FileController
|
* Class FileController
|
||||||
@@ -37,30 +37,68 @@ class FileController extends Controller
|
|||||||
{
|
{
|
||||||
$attrs = $request->post();
|
$attrs = $request->post();
|
||||||
|
|
||||||
// Not using a form validation here because when it redirects,
|
/*
|
||||||
// it leaves the parent forms all blank, even though it goes
|
* Not using a form validation here because when it redirects, it leaves
|
||||||
// back to the right place. So just manually validate
|
* the parent forms all blank, even though it goes back to the right place.
|
||||||
$validator = Validator::make($request->all(), [
|
*
|
||||||
'filename' => 'required',
|
* The fields are also named file_name and file_description so that if there
|
||||||
'file_description' => 'nullable',
|
* are validation errors, the flash messages doesn't conflict with other
|
||||||
'file' => 'required|file',
|
* fields on the page that might have the "name" and "description" fields
|
||||||
]);
|
*
|
||||||
|
* Was also going to use the "required_without" rule, but that doesn't appear
|
||||||
|
* to work properly with a file upload
|
||||||
|
*/
|
||||||
|
$validator = Validator::make(
|
||||||
|
$request->all(),
|
||||||
|
[
|
||||||
|
'file_name' => 'required',
|
||||||
|
'file_description' => 'nullable',
|
||||||
|
'file' => [
|
||||||
|
Rule::requiredIf(function () {
|
||||||
|
return !request()->filled('url');
|
||||||
|
}),
|
||||||
|
'file',
|
||||||
|
],
|
||||||
|
'url' => [
|
||||||
|
Rule::requiredIf(function () {
|
||||||
|
return !request()->hasFile('file');
|
||||||
|
}),
|
||||||
|
'url',
|
||||||
|
],
|
||||||
|
],
|
||||||
|
[
|
||||||
|
'file.required' => 'File or URL are required',
|
||||||
|
'url.required' => 'File or URL are required',
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
if ($validator->fails()) {
|
if ($validator->fails()) {
|
||||||
return redirect()->back()->withInput(Input::all())->withErrors($validator);
|
return redirect()
|
||||||
|
->back()
|
||||||
|
->withErrors($validator)
|
||||||
|
->withInput(Input::all());
|
||||||
}
|
}
|
||||||
|
|
||||||
Log::info('Uploading files', $attrs);
|
Log::info('Uploading files', $attrs);
|
||||||
|
|
||||||
$file = $request->file('file');
|
$attrs['name'] = $attrs['file_name'];
|
||||||
$this->fileSvc->saveFile($file, 'files', [
|
$attrs['description'] = $attrs['file_description'];
|
||||||
'name' => $attrs['filename'],
|
|
||||||
'description' => $attrs['file_description'],
|
if ($request->hasFile('file')) {
|
||||||
'ref_model' => $attrs['ref_model'],
|
$file = $request->file('file');
|
||||||
'ref_model_id' => $attrs['ref_model_id'],
|
$this->fileSvc->saveFile($file, 'files', $attrs);
|
||||||
]);
|
}
|
||||||
|
|
||||||
|
// Didn't provide a file to upload, just a URL to a file
|
||||||
|
// Create the model directly and just associate that
|
||||||
|
elseif ($request->filled('url')) {
|
||||||
|
$file = new File($attrs);
|
||||||
|
$file->path = $attrs['url'];
|
||||||
|
$file->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
Flash::success('Files saved successfully');
|
||||||
|
|
||||||
Flash::success('Files uploaded successfully.');
|
|
||||||
return redirect()->back();
|
return redirect()->back();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -81,10 +119,10 @@ class FileController extends Controller
|
|||||||
return redirect()->back();
|
return redirect()->back();
|
||||||
}
|
}
|
||||||
|
|
||||||
Storage::disk(config('filesystems.public_files'))->delete($file->path);
|
$this->fileSvc->removeFile($file);
|
||||||
$file->delete();
|
|
||||||
|
|
||||||
Flash::success('File deleted successfully.');
|
Flash::success('File deleted successfully.');
|
||||||
|
|
||||||
return redirect()->back();
|
return redirect()->back();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use App\Contracts\Model;
|
|||||||
use App\Models\Traits\HashIdTrait;
|
use App\Models\Traits\HashIdTrait;
|
||||||
use App\Models\Traits\ReferenceTrait;
|
use App\Models\Traits\ReferenceTrait;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @property string $name
|
* @property string $name
|
||||||
@@ -80,6 +81,10 @@ class File extends Model
|
|||||||
*/
|
*/
|
||||||
public function getUrlAttribute(): string
|
public function getUrlAttribute(): string
|
||||||
{
|
{
|
||||||
|
if (Str::startsWith($this->path, 'http')) {
|
||||||
|
return $this->path;
|
||||||
|
}
|
||||||
|
|
||||||
$disk = $this->disk ?? config('filesystems.public_files');
|
$disk = $this->disk ?? config('filesystems.public_files');
|
||||||
|
|
||||||
// If the disk isn't stored in public (S3 or something),
|
// If the disk isn't stored in public (S3 or something),
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ namespace App\Models;
|
|||||||
use App\Contracts\Model;
|
use App\Contracts\Model;
|
||||||
use App\Models\Enums\AircraftStatus;
|
use App\Models\Enums\AircraftStatus;
|
||||||
use App\Models\Traits\ExpensableTrait;
|
use App\Models\Traits\ExpensableTrait;
|
||||||
|
use App\Models\Traits\FilesTrait;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Subfleet
|
* Class Subfleet
|
||||||
@@ -21,6 +22,7 @@ use App\Models\Traits\ExpensableTrait;
|
|||||||
class Subfleet extends Model
|
class Subfleet extends Model
|
||||||
{
|
{
|
||||||
use ExpensableTrait;
|
use ExpensableTrait;
|
||||||
|
use FilesTrait;
|
||||||
|
|
||||||
public $table = 'subfleets';
|
public $table = 'subfleets';
|
||||||
|
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ namespace App\Services;
|
|||||||
|
|
||||||
use App\Contracts\Service;
|
use App\Contracts\Service;
|
||||||
use App\Models\File;
|
use App\Models\File;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
use Illuminate\Support\Str;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class FileService
|
|
||||||
*/
|
|
||||||
class FileService extends Service
|
class FileService extends Service
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
@@ -37,13 +37,11 @@ class FileService extends Service
|
|||||||
|
|
||||||
// Create the file, add the ID to the front of the file to account
|
// Create the file, add the ID to the front of the file to account
|
||||||
// for any duplicate filenames, but still can be found in an `ls`
|
// for any duplicate filenames, but still can be found in an `ls`
|
||||||
|
$filename = $id.'_'.str_slug(trim($path_info['filename'])).'.'.$path_info['extension'];
|
||||||
$filename = $id.'_'
|
|
||||||
.str_slug(trim($path_info['filename']))
|
|
||||||
.'.'.$path_info['extension'];
|
|
||||||
|
|
||||||
$file_path = $file->storeAs($folder, $filename, $attrs['disk']);
|
$file_path = $file->storeAs($folder, $filename, $attrs['disk']);
|
||||||
|
|
||||||
|
Log::info('File saved to '.$file_path);
|
||||||
|
|
||||||
$asset = new File($attrs);
|
$asset = new File($attrs);
|
||||||
$asset->id = $id;
|
$asset->id = $id;
|
||||||
$asset->path = $file_path;
|
$asset->path = $file_path;
|
||||||
@@ -51,4 +49,21 @@ class FileService extends Service
|
|||||||
|
|
||||||
return $asset;
|
return $asset;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a file, if it exists on disk
|
||||||
|
*
|
||||||
|
* @param File $file
|
||||||
|
*
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function removeFile($file)
|
||||||
|
{
|
||||||
|
if (!Str::startsWith($file->path, 'http')) {
|
||||||
|
Storage::disk(config('filesystems.public_files'))
|
||||||
|
->delete($file->path);
|
||||||
|
}
|
||||||
|
|
||||||
|
$file->delete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,9 @@ Pass in:
|
|||||||
<div id="airport-files-wrapper" class="col-12">
|
<div id="airport-files-wrapper" class="col-12">
|
||||||
<div class="header">
|
<div class="header">
|
||||||
<h3>files</h3>
|
<h3>files</h3>
|
||||||
|
@component('admin.components.info')
|
||||||
|
Add a download link or upload a file to make available
|
||||||
|
@endcomponent
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@if(count($model->files) === 0)
|
@if(count($model->files) === 0)
|
||||||
@@ -58,18 +61,24 @@ Pass in:
|
|||||||
])
|
])
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
<span class="required">*</span>
|
||||||
|
{{ Form::text('file_name', null, ['class' => 'form-control', 'placeholder' => 'Name']) }}
|
||||||
|
{{ Form::text('file_description', null, ['class' => 'form-control', 'placeholder' => 'Description']) }}
|
||||||
|
{{ Form::text('url', null, ['class' => 'form-control', 'placeholder' => 'URL']) }}
|
||||||
|
{{ Form::file('file', ['class' => 'form-control']) }}
|
||||||
|
|
||||||
{{-- Fields for the model --}}
|
{{-- Fields for the model --}}
|
||||||
{{ Form::hidden('ref_model', get_class($model)) }}
|
{{ Form::hidden('ref_model', get_class($model)) }}
|
||||||
{{ Form::hidden('ref_model_id', $model->id) }}
|
{{ Form::hidden('ref_model_id', $model->id) }}
|
||||||
|
|
||||||
<span class="required">*</span>
|
{{ Form::submit('Save', [
|
||||||
{{ Form::text('filename', null, ['class' => 'form-control', 'placeholder' => 'Name']) }}
|
'id' => 'save_file_upload',
|
||||||
{{ Form::text('file_description', null, ['class' => 'form-control', 'placeholder' => 'Description']) }}
|
'class' => 'btn btn-success'
|
||||||
{{ Form::file('file', ['class' => 'form-control']) }}
|
])
|
||||||
|
}}
|
||||||
{{ Form::submit('Upload', ['class' => 'btn btn-success']) }}
|
|
||||||
<div class="text-danger" style="padding-top: 10px;">
|
<div class="text-danger" style="padding-top: 10px;">
|
||||||
<span>{{ $errors->first('filename') }}</span>
|
<span>{{ $errors->first('filename') }}</span>
|
||||||
|
<span>{{ $errors->first('url') }}</span>
|
||||||
<span>{{ $errors->first('file') }}</span>
|
<span>{{ $errors->first('file') }}</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -27,5 +27,11 @@
|
|||||||
@include('admin.subfleets.expenses')
|
@include('admin.subfleets.expenses')
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div class="card border-blue-bottom">
|
||||||
|
<div class="content">
|
||||||
|
@include('admin.common.file_upload', ['model' => $subfleet])
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
@endsection
|
@endsection
|
||||||
@include('admin.subfleets.script')
|
@include('admin.subfleets.script')
|
||||||
|
|||||||
Reference in New Issue
Block a user