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:
parent
bbec276da8
commit
651174bda8
@ -11,8 +11,6 @@ use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Laracasts\Flash\Flash;
|
||||
use PragmaRX\Version\Package\Facade as Version;
|
||||
use vierbergenlars\SemVer\version as semver;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
|
@ -5,12 +5,12 @@ namespace App\Http\Controllers\Admin;
|
||||
use App\Contracts\Controller;
|
||||
use App\Models\File;
|
||||
use App\Services\FileService;
|
||||
use Flash;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Input;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Log;
|
||||
use Validator;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Validation\Rule;
|
||||
use Laracasts\Flash\Flash;
|
||||
|
||||
/**
|
||||
* Class FileController
|
||||
@ -37,30 +37,68 @@ class FileController extends Controller
|
||||
{
|
||||
$attrs = $request->post();
|
||||
|
||||
// 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_description' => 'nullable',
|
||||
'file' => 'required|file',
|
||||
]);
|
||||
/*
|
||||
* 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.
|
||||
*
|
||||
* The fields are also named file_name and file_description so that if there
|
||||
* are validation errors, the flash messages doesn't conflict with other
|
||||
* 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()) {
|
||||
return redirect()->back()->withInput(Input::all())->withErrors($validator);
|
||||
return redirect()
|
||||
->back()
|
||||
->withErrors($validator)
|
||||
->withInput(Input::all());
|
||||
}
|
||||
|
||||
Log::info('Uploading files', $attrs);
|
||||
|
||||
$file = $request->file('file');
|
||||
$this->fileSvc->saveFile($file, 'files', [
|
||||
'name' => $attrs['filename'],
|
||||
'description' => $attrs['file_description'],
|
||||
'ref_model' => $attrs['ref_model'],
|
||||
'ref_model_id' => $attrs['ref_model_id'],
|
||||
]);
|
||||
$attrs['name'] = $attrs['file_name'];
|
||||
$attrs['description'] = $attrs['file_description'];
|
||||
|
||||
if ($request->hasFile('file')) {
|
||||
$file = $request->file('file');
|
||||
$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();
|
||||
}
|
||||
|
||||
@ -81,10 +119,10 @@ class FileController extends Controller
|
||||
return redirect()->back();
|
||||
}
|
||||
|
||||
Storage::disk(config('filesystems.public_files'))->delete($file->path);
|
||||
$file->delete();
|
||||
$this->fileSvc->removeFile($file);
|
||||
|
||||
Flash::success('File deleted successfully.');
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ use App\Contracts\Model;
|
||||
use App\Models\Traits\HashIdTrait;
|
||||
use App\Models\Traits\ReferenceTrait;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @property string $name
|
||||
@ -80,6 +81,10 @@ class File extends Model
|
||||
*/
|
||||
public function getUrlAttribute(): string
|
||||
{
|
||||
if (Str::startsWith($this->path, 'http')) {
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
$disk = $this->disk ?? config('filesystems.public_files');
|
||||
|
||||
// If the disk isn't stored in public (S3 or something),
|
||||
|
@ -5,6 +5,7 @@ namespace App\Models;
|
||||
use App\Contracts\Model;
|
||||
use App\Models\Enums\AircraftStatus;
|
||||
use App\Models\Traits\ExpensableTrait;
|
||||
use App\Models\Traits\FilesTrait;
|
||||
|
||||
/**
|
||||
* Class Subfleet
|
||||
@ -21,6 +22,7 @@ use App\Models\Traits\ExpensableTrait;
|
||||
class Subfleet extends Model
|
||||
{
|
||||
use ExpensableTrait;
|
||||
use FilesTrait;
|
||||
|
||||
public $table = 'subfleets';
|
||||
|
||||
|
@ -4,10 +4,10 @@ namespace App\Services;
|
||||
|
||||
use App\Contracts\Service;
|
||||
use App\Models\File;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* Class FileService
|
||||
*/
|
||||
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
|
||||
// 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']);
|
||||
|
||||
Log::info('File saved to '.$file_path);
|
||||
|
||||
$asset = new File($attrs);
|
||||
$asset->id = $id;
|
||||
$asset->path = $file_path;
|
||||
@ -51,4 +49,21 @@ class FileService extends Service
|
||||
|
||||
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 class="header">
|
||||
<h3>files</h3>
|
||||
@component('admin.components.info')
|
||||
Add a download link or upload a file to make available
|
||||
@endcomponent
|
||||
</div>
|
||||
|
||||
@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 --}}
|
||||
{{ Form::hidden('ref_model', get_class($model)) }}
|
||||
{{ Form::hidden('ref_model_id', $model->id) }}
|
||||
|
||||
<span class="required">*</span>
|
||||
{{ Form::text('filename', null, ['class' => 'form-control', 'placeholder' => 'Name']) }}
|
||||
{{ Form::text('file_description', null, ['class' => 'form-control', 'placeholder' => 'Description']) }}
|
||||
{{ Form::file('file', ['class' => 'form-control']) }}
|
||||
|
||||
{{ Form::submit('Upload', ['class' => 'btn btn-success']) }}
|
||||
{{ Form::submit('Save', [
|
||||
'id' => 'save_file_upload',
|
||||
'class' => 'btn btn-success'
|
||||
])
|
||||
}}
|
||||
<div class="text-danger" style="padding-top: 10px;">
|
||||
<span>{{ $errors->first('filename') }}</span>
|
||||
<span>{{ $errors->first('url') }}</span>
|
||||
<span>{{ $errors->first('file') }}</span>
|
||||
</div>
|
||||
|
||||
|
@ -27,5 +27,11 @@
|
||||
@include('admin.subfleets.expenses')
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="card border-blue-bottom">
|
||||
<div class="content">
|
||||
@include('admin.common.file_upload', ['model' => $subfleet])
|
||||
</div>
|
||||
</div>
|
||||
@endsection
|
||||
@include('admin.subfleets.script')
|
||||
|
Loading…
Reference in New Issue
Block a user