Save file asset
This commit is contained in:
@@ -29,6 +29,7 @@ class FileController extends Controller
|
||||
* Store a newly file
|
||||
* @param Request $request
|
||||
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
|
||||
* @throws \Hashids\HashidsException
|
||||
*/
|
||||
public function store(Request $request)
|
||||
{
|
||||
@@ -49,19 +50,14 @@ class FileController extends Controller
|
||||
|
||||
Log::info('Uploading files', $attrs);
|
||||
|
||||
|
||||
$file = $request->file('file');
|
||||
$file_path = $this->fileSvc->saveFile($file, 'files');
|
||||
|
||||
$asset = new File();
|
||||
$asset->name = $attrs['filename'];
|
||||
$asset->description = $attrs['file_description'];
|
||||
$asset->disk = config('filesystems.public_files');
|
||||
$asset->path = $file_path;
|
||||
$asset->public = false; // need to be logged in to see. default (for now)
|
||||
$asset->ref_model = $attrs['ref_model'];
|
||||
$asset->ref_model_id = $attrs['ref_model_id'];
|
||||
|
||||
$asset->save();
|
||||
$this->fileSvc->saveFile($file, 'files', [
|
||||
'name' => $attrs['filename'],
|
||||
'description' => $attrs['file_descriptoin'],
|
||||
'ref_model' => $attrs['ref_model'],
|
||||
'ref_model_id' => $attrs['ref_model_id'],
|
||||
]);
|
||||
|
||||
Flash::success('Files uploaded successfully.');
|
||||
return redirect()->back();
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Services;
|
||||
|
||||
use App\Interfaces\Service;
|
||||
use App\Models\File;
|
||||
|
||||
/**
|
||||
* Class FileService
|
||||
@@ -11,20 +12,41 @@ use App\Interfaces\Service;
|
||||
class FileService extends Service
|
||||
{
|
||||
/**
|
||||
* Save a file to disk and return the path
|
||||
* Save a file to disk and return a File asset
|
||||
* @param \Illuminate\Http\UploadedFile $file
|
||||
* @param string $folder
|
||||
* @param string|null $disk
|
||||
* @return string
|
||||
* @param array $attrs
|
||||
* @return File
|
||||
* @throws \Hashids\HashidsException
|
||||
*/
|
||||
public function saveFile($file, $folder, $disk = null): string
|
||||
public function saveFile($file, $folder, array $attrs)
|
||||
{
|
||||
if (!$disk) {
|
||||
$disk = config('filesystems.public_files');
|
||||
}
|
||||
$attrs = array_merge([
|
||||
'name' => '',
|
||||
'description' => '',
|
||||
'public' => false,
|
||||
'ref_model' => '',
|
||||
'ref_model_id' => '',
|
||||
'disk' => config('filesystems.public_files'),
|
||||
], $attrs);
|
||||
|
||||
$id = File::createNewHashId();
|
||||
$path_info = pathinfo($file->getClientOriginalName());
|
||||
$filename = str_slug($path_info['filename']).'.'.$path_info['extension'];
|
||||
return $file->storeAs($folder, $filename, $disk);
|
||||
|
||||
# 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'];
|
||||
|
||||
$file_path = $file->storeAs($folder, $filename, $attrs['disk']);
|
||||
|
||||
$asset = new File($attrs);
|
||||
$asset->id = $id;
|
||||
$asset->path = $file_path;
|
||||
$asset->save();
|
||||
|
||||
return $asset;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user