custom-d/simple-file-storage

简单的文件存储

v1.4.3 2022-09-16 00:30 UTC

README

包描述:允许在可搜索的模型上使用文件存储

安装

通过 composer 安装

composer require custom-d/simple-file-storage

发布包资源(可选)

php artisan vendor:publish --provider="CustomD\SimpleFileStorage\ServiceProvider"

或设置环境变量

SFS_TEMP_URL_MINUTES=15
connection=${DB_CONNECTION}

运行迁移

php artisan migrate

使用

在您想要添加文件附件的模型上添加以下特质

...
use CustomD\SimpleFileStorage\HasFileStorage;

class MyModel extends Model
{

    use HasFileStorage;
    ...

您还可以可选地设置以下常量

const DEFAULT_STORAGE_DISK = ''; //default is your config(filesystems.default)
//or override method:
 protected function getDefaultDisk(): string
{
  return 'xxx';
}

const DEFAULT_STORAGE_COLLECTION = 'default'; //default = default
//or override the method
protected function getCollectionName(?string $collection): string
{
  return 'xxx';
}

上传文件

您可以使用以下方式上传 base64 编码的文件或表单文件请求

$model->storeFileFromRequest($key [,?array $custom_properties = null[, ?string $collection = null[, ?string $disk = null]]]);

$model->storeFilesFromRequest($keys [,?array $custom_properties = null[, ?string $collection = null[, ?string $disk = null]]]);

$model->storeFileFromBase64($base64Data [,?array $custom_properties = null[, ?string $collection = null[, ?string $disk = null]]]);

尽管如果文件不可上传或密钥未设置,它将抛出异常。

检索文件

$model->attachements()->get(); // to get all
$model->getAttachments()->get([$collection]); //will only get the default collection attachemnts


//available scopes
$model->withProperty('key','value'); // key value set vai the custom_properties when storing
$model->orWithProperty('key','value'); // key value set vai the custom_properties when storing
$model->withoutProperty('key','value'); // key value set vai the custom_properties when storing
$model->orWithoutProperty('key', 'value'); // key value set vai the custom_properties when storing
$model->withoutPropertyKey('key'); // key set via teh custom properties when storing (not set in this case)
$model->orWithoutPropertyKey('key'); // key set via teh custom properties when storing (not set in this case)
$model->inCollection('collection'); //can pass a single, or array of collection names
$model->orInCollection('collection'); //can pass a single, or array of collection names
$model->notInCollection('collection'); //can pass a single or array of collection names
$model->orNotInCollection('collection'); //can pass a single or array of collection names

文件方法

  • getFileName - 获取存储的文件名
  • getStorage - 获取存储实例(因此您可以对 $item->getStorage()->Xxxx($filename) 执行操作)
  • missing - 检查文件是否缺失
  • exists - 检查文件是否存在
  • get - 获取文件内容
  • url - 获取文件 URL
  • temporaryUrl - 获取临时 URL(仅 AWS)
  • delete - 删除文件和记录
  • download - 触发流式下载
  • stream - 触发内联流

安全

如果您发现任何与安全相关的问题,请通过电子邮件而不是使用问题跟踪器。

致谢

此包是通过 melihovv/laravel-package-generator 的帮助启动的。