codegaf/storagemanager

统一文件管理包。

dev-master 2022-04-20 09:02 UTC

This package is auto-updated.

Last update: 2024-09-20 14:29:53 UTC


README

Latest Version on Packagist Total Downloads

Spatie Media Library的包装器,结合Spatie Permission Laravel文件隐私管理。

安装

您可以通过composer安装此包

composer require codegaf/storagemanager dev-master

使用指南

Storage manager作为Spatie Media Library的包装器创建,收集其主要功能,并通过更易于记忆的全局语法封装。此外,利用Spatie Permission库,预定义了一系列功能,为角色、权限和用户标识符提供安全层,同时允许开拓新案例。

此包由以下类组成

  • StorageManagerController: 通过publish进行访问,该类预定义了一系列方法,将通过http进行访问。与StorageManagerService服务连接。
  • StorageManagerService: 通过publish进行访问,该类从StorageManagerServiceBase扩展,并创建了默认的checkPermissions函数。在这个函数中,我们可以为集合设置文件的权限。在StorageManagerServiceBase类中,我们有如何使用预定义方法(如userHasRole、userHasRoles、userIsOwner、userHasPermission)创建具有权限的集合的示例。如果我们需要扩展库以添加新功能或替换基本功能,我们可以在该类中做到这一点。
/**
     * Ejemplo. Comprueba los permisos establecidos por colección y media
     *
     * @param Media $media
     * @return bool
     */
    private function checkPermissionsExample(Media $media) {
        switch ($media->collection_name) {
            case 'avatar':
                if (!$this->userHasRole($media)) {
                    return false;
                }

                if (!$this->userIsOwner($media)) {
                    return false;
                }
                
                if (!$this->userHasRoles($media)) {
                    return false;
                }

                if (!$this->userHasPermission($media, 'download')) {
                    return false;
                }

                return true;
            default:
                return true;
        }
    }
  • StorageManagerServiceBase: 该类预定义的功能包括

file -> (Media $media, string $conversionName = ''): 根据权限返回文件。如果指定,可以返回其转换版本。

addFile -> (Model $model, UploadedFile $file, string collection, array customProperties = []): 向模型添加新的媒体。通过自定义属性,我们可以向文件添加权限。

addFiles -> (Model $model, array $files, string $collection, array $customProperties): 向模型添加多个媒体。

addFileFromBase64 -> (Model $model, string $base64File, string $fileName, string $collection, array $customProperties = []): 向模型添加格式为base64的新媒体。

updateCustomProperties -> (Media $media, array $customProperties): 修改媒体的自定义属性。

deleteCustomProperties -> (Media, $media, array $customProperties): 删除传递的自定义属性。

deleteFile -> (Media $media, bool $force = false): 从数据库及其关联文件中删除媒体。如果第二个参数传递true,则将执行彻底删除。

deleteModelFiles -> (Model $model, string $collection = ''): 删除模型的所有媒体或指定集合的媒体。

reorderFiles -> (array $ids, int $startOrder = 1): 默认从编号1开始对通过参数传递的媒体进行排序。

filesByCollection -> (string $collection): 下载包含集合中所有原始图像的zip文件。

filesByModel -> (array $data): 下载包含模型或指定集合中所有文件的zip文件。

filesByIds -> (array $data): 下载包含通过参数传递的ids对应的原始文件的zip文件。

userIsOwner -> (Media $media): 检查媒体的自定义属性中是否包含认证用户的id。

userHasRole -> (Media $media): 检查媒体的自定义属性中是否包含认证用户的角色。

userHasRoles -> (Media $media): 检查媒体是否具有用户在自定义属性中的角色。

userHasPermission -> (Media $media, string $permission): 检查用户和媒体是否具有传递的参数权限。

  • CustomPathGenerator: 类,它覆盖了Spatie在命名媒体路径时的默认行为。在包中用于加密媒体文件夹。

  • Web.php: 捕获包中预定义的路由以及web和auth中间件。

一旦安装了该包,就需要执行发布操作

php vendor:publish --provider=Codegaf\StorageManager\StorageManagerProvider

此命令将发布StorageManagerController\StorageManagerController和StorageManagerService\StorageManagerService类。

最后,我们将访问config/medialibrary.php文件,并在“path_generator”索引处添加CustomPathGenerator。

/*
     * The class that contains the strategy for determining a media file's path.
     */
    'path_generator' => \Codegaf\StorageManager\CustomPathGenerator::class,

如果你正在缓存配置,别忘了做

php artisan config:cache

更新日志

请参阅更新日志获取最近更改的更多信息。

安全性

如果你发现任何与安全相关的问题,请通过isaaccamrod@gmail.com发送电子邮件,而不是使用问题跟踪器。

致谢

许可

MIT许可(MIT)。请参阅许可文件获取更多信息。