phpvms/app/Services/FileService.php

53 lines
1.4 KiB
PHP
Raw Normal View History

<?php
namespace App\Services;
use App\Interfaces\Service;
2018-04-03 06:34:58 +08:00
use App\Models\File;
/**
* Class FileService
* @package App\Services
*/
class FileService extends Service
{
/**
2018-04-03 06:34:58 +08:00
* Save a file to disk and return a File asset
* @param \Illuminate\Http\UploadedFile $file
* @param string $folder
2018-04-03 06:34:58 +08:00
* @param array $attrs
* @return File
* @throws \Hashids\HashidsException
*/
2018-04-03 06:34:58 +08:00
public function saveFile($file, $folder, array $attrs)
{
2018-04-03 06:34:58 +08:00
$attrs = array_merge([
'name' => '',
'description' => '',
'public' => false,
'ref_model' => '',
'ref_model_id' => '',
'disk' => config('filesystems.public_files'),
], $attrs);
2018-04-03 06:34:58 +08:00
$id = File::createNewHashId();
$path_info = pathinfo($file->getClientOriginalName());
2018-04-03 06:34:58 +08:00
# 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;
}
}