phapi/middleware-file-handler 问题修复 & 功能扩展

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

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

phapi/middleware-file-handler

Composer 安装命令:

composer require phapi/middleware-file-handler

包简介

Middleware responsible for handling files received from and sent to the client

README 文档

README

Please note that this package is still a work in progress and that the implementation might change at any time and that the documentation might be out of date.

Build status Code Climate Test Coverage

File Handler is a middleware preparing the Phapi Framework for handling client request for both uploading and downloading files. It acts like a modified version of a serializer but without the serialization functionality. It registers the supported mime types and makes sure uploaded files are of a mime type that is allowed as well as checking the file size of the uploaded file.

The package also contains an Endpoint that handles both PUT (for uploading files) and GET (for downloading files) requests. The Endpoint depends on league/flysystem for handling the interaction with the file storage.

Installation

This middleware is not included by default in the Phapi Framework but if you need to install it it's available to install via Packagist and Composer.

$ php composer.phar require phapi/middleware-file-handler:1.*

Configuration

The configuration can be a little bit tricky. The phapi-configuration repo has all the configuration added in the right order and place. You can use the repo to start a new project or use it as a reference while updating your existing project. Below is a list of what you need to add to your configuration to get the File handler middleware to work.

configuration/settings.php

Add the following lines to your settings (example) file and configure your list of routes and configuration for each route:

<?php
/*
 * File handler configuration, add an array for each route you want to have.
 * The file handler requires the FlySystem package, link: http://flysystem.thephpleague.com/
 *
 * Each route has the following configuration options:
 *   'route', the route itself (don't forget that the route has to be included in the main route table as well)
 *   'maxFileSize', (optional) the maximum allowed file size
 *   'mimeTypes', an array with the list of allowed mime types
 *   'flySystemAdapter', use the adapter that matches your environment
 */
$container['fileHandlerConfiguration'] = function () {
    return [
        /* Example:
        [
            'route' => '/user/avatar/{id}/{fileName}', // Complete route
            'maxFileSize' => 1024000, // In bytes
            'mimeTypes' => [
                'image/gif',
                'image/png',
                'image/jpg',
                'image/jpeg'
            ],
            'flySystemAdapter' => new League\Flysystem\Adapter\Local('/path/to/file/storage/')
        ]*/
    ];
};

// Create the Fly system file system
$container['flySystem'] = function ($container) {
    return new \League\Flysystem\Filesystem(
        $container['fileHandledConfiguration']['flySystemAdapter']
    );
};

configuration/middleware.php

Add the following two lines to your configuration (example) to add the middleware to the middleware pipeline:

The first line should be added right after the regular serializers and must be added before the FormatNegotiation middleware.

<?php
$pipeline->pipe(new \Phapi\Middleware\FileHandler\FileReader($container['fileHandlerConfiguration']));

The second line must be added between the Route and PostBox middleware.

<?php
$pipeline->pipe(new \Phapi\Middleware\FileHandler\FileUploader($container['fileHandlerConfiguration']));

configuration/routes.php

The last step is to add a new route to your route table (example:

<?php

$routes = [
  '/user/avatar/{id}/{fileName}'    => '\\Phapi\\Endpoint\\File',
];

Usage

Some things to consider:

  • It's important that you set the set memory_limit high enough to be able to handle large files. Uploading files will NOT be affected by either post_max_size or upload_max_filesize since we are using PUT method instead of POST.
  • The included File endpoint expects the route to have a variable last in the string and that variable will be used as the file name. In the example above we have two variables, id and file name, both will be used when the file is saved to storage. If the Flysystem configuration says that a file should be saved to /www/files/ the file will be saved to /www/files/{id}/{filename}.
  • The File endpoint will save the file to the designated place and that's it. So a good strategy is to create a separate endpoint that gets called before the File endpoint with information about the file. That endpoint should save the information and return the designated url where the file should be PUT.

Phapi

This middleware is a Phapi package used by the Phapi Framework. The middleware are also PSR-7 compliant and implements the Phapi Middleware Contract.

License

Serializer JSON is licensed under the MIT License - see the license.md file for details

Contribute

Contribution, bug fixes etc are always welcome.

phapi/middleware-file-handler 适用场景与选型建议

phapi/middleware-file-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.29k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 10 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 phapi/middleware-file-handler 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-10-27