dcodegroup/laravel-attachments 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

dcodegroup/laravel-attachments

Composer 安装命令:

composer require dcodegroup/laravel-attachments

包简介

Allow adding attachments to any document

README 文档

README

Simple Dropin package to add attachments to your models.

Latest Version on Packagist GitHub Code Style Action Status Total Downloads

Install

Version / Branch Laravel Support Install Command
0.x <= v10 composer require dcodegroup/laravel-attachments:^0.0
1.x >= v11 composer require dcodegroup/laravel-attachments:^1.0

Then run

php artisan vendor:publish --provider="Dcodegroup\LaravelAttachments\LaravelAttachmentsServiceProvider"

Database

If you are using either ULIDS or UUIDs in your tables ensure to update the published migrations for the media table.

eg. replace with the appropriate type

    Schema::create('media', function (Blueprint $table) {
    ...
        $table->nullableMorphs('model');
        $table->nullableMorphs('parent_model');

or

    Schema::create('media', function (Blueprint $table) {
    ...
        $table->nullableUlidMorphs('model');
        $table->nullableUlidMorphs('parent_model');

Then run the migrations

## Routes 

Add the Routes to the file you need such as `laravel_attachments.php`

```php
<?php

use Illuminate\Support\Facades\Route;

Route::attachments();
Route::attachmentAnnotations();
Route::attachmentCategories();

Then add the following to your RouteServiceProvider

    /**
     * Attachments
     */
    Route::middleware([
        'web',
        'auth',
    ])->as(config('attachments.route_name_prefix').'.')->prefix('attachments')->group(base_path('routes/laravel_attachments.php'));

Frontend

Add the following file to to your css file.

 @import "../../vendor/dcodegroup/laravel-attachments/resources/scss/attachments.scss";

Add the below to the app.js file.

import attachmentPlugin from "../../vendor/dcodegroup/laravel-attachments/resources/js/plugin"

app.use(attachmentPlugin)

Ensure to install these npm packages

npm i @heroicons/vue bytes form-backend-validation vue-image-markup vue-upload-component  

config

Configuration file contains

<?php
return [
    'media' => [
        'conversions' => [
            'thumb' => [
                'width' => 43,
                'height' => 43,
            ],
            'list' => [
                'width' => 43,
                'height' => 43,
            ],
            'grid' => [
                'width' => 160,
                'height' => 192,
            ],
        ],
    ],

    'features' => [
        // annotations
        // categories
    ],
    
    'route_name_prefix' => env('LARAVEL_ATTACHMENTS_ROUTE_NAME_PREFIX', 'laravel_attachments'),
    
    'signed' => env('LARAVEL_ATTACHMENTS_URLS_SIGNED', false),
];

Ensure the publish the config file from the Spatie Media Library.

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="medialibrary-config"

Then change the media model used to

    'media_model' => \Dcodegroup\LaravelAttachments\Models\Media::class,

Update the path generator from the default to our customised one.

    'path_generator' => \Dcodegroup\LaravelAttachments\MediaLibrary\MediaPathGenerator::class,

You probably want to change the disk for stored files from media-library also.

    'disk_name' => env('MEDIA_DISK', 's3'),

Security

You will need to implement a MediaPolicy to control access to the media.

Then add to AuthServiceProvider

    protected $policies = [
        ...
        Media::class => MediaPolicy::class,
    ];

Example policy might be

<?php

namespace App\Policies;

use App\Models\User;
use Dcodegroup\LaravelAttachments\Models\Media;
use Illuminate\Auth\Access\HandlesAuthorization;

class MediaPolicy
{
    use HandlesAuthorization;

    public function viewAny(User $user): bool
    {
        return $this->internalOnly($user);
    }

    public function view(User $user, Media $media): bool
    {
        return $this->internalOnly($user);
    }

    public function create(User $user): bool
    {
        return $this->internalOnly($user);
    }

    public function update(User $user, Media $media): bool
    {
        return $this->internalOnly($user);
    }

    public function delete(User $user, Media $media): bool
    {
        return $this->internalOnly($user);
    }

    public function restore(User $user, Media $media): bool
    {
        return $this->internalOnly($user);
    }

    public function forceDelete(User $user, Media $media): bool
    {
        return $this->internalOnly($user);
    }
    
    public function download(User $user): bool
    {
        return $this->internalOnly($user);
    }
}

If you are using S3 or another hosted service you may need to use signed urls to access the urls.

If yes then update the configuration or the ENV variable to true.

    'use_signed_urls' => env('USE_SIGNED_URLS', true),

This package will use dreamonkey/laravel-cloudfront-url-signer https://github.com/dreamonkey/laravel-cloudfront-url-signer. See the README for how to configure.

Here is how to generate the ssh keys. Make sure to have a directory storage/cloudfront-keypairs

Source of below is from https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/private-content-trusted-signers.html?icmpid=docs_cf_help_panel#private-content-creating-cloudfront-key-pairs

then

openssl genrsa -out private_key.pem 2048

then

openssl rsa -pubout -in private_key.pem -out public_key.pem

User model

Add the following contract to the User model.

use Dcodegroup\LaravelAttachments\Contracts\HasMediaUser;

class User extends Authenticatable implements HasMediaUser
{
...

public function getMediaUserName(): string
{
    return $this->name;
}

Usage

Add the template to the edit page you want.

    @if($model->exists)
    <div class="py-8">
        <hr class="border-gray-100">
    </div>
    <div class="mt-8">
        @include('attachments::attachments', ['model' => $model])
    </div>
    @endif

dcodegroup/laravel-attachments 适用场景与选型建议

dcodegroup/laravel-attachments 是一款 基于 Vue 开发的 Composer 扩展包,目前已累计 31.53k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「attachments」 「dcode」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 dcodegroup/laravel-attachments 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 dcodegroup/laravel-attachments 我们能提供哪些服务?
定制开发 / 二次开发

基于 dcodegroup/laravel-attachments 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 31.53k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 5
  • Forks: 0
  • 开发语言: Vue

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-09