Giter Site home page Giter Site logo

tinywan / webman-storage Goto Github PK

View Code? Open in Web Editor NEW
22.0 2.0 5.0 65 KB

the simple more file upload library for webman plugin

Home Page: https://www.workerman.net/plugin/21

License: MIT License

PHP 100.00%
webman workerman upload upload-file oss php-library tinywan

webman-storage's Introduction

webman 简单易用多文件上传插件

Latest Stable Version Total Downloads Daily Downloads Latest Unstable Version License PHP Version Require last-commit storage tag

特性

云端 多文件上传 Base64图片上传 服务器文件上传
🍏 私有云 --
🍓 阿里云
🍋 腾讯云
🍇 七牛云

安装

composer require tinywan/storage

基本用法

$res = Tinywan\Storage\Storage::uploadFile();
var_dump(json_encode($res));

v1.0.0 版本之后不需要调用此方法Tinywan\Storage\Storage::config() 初始化,会自动初始化,默认为配置文件app.php默认配置default配置上传适配器。

上传成功信息

[
    {
        "key": "webman",
        "origin_name": "常用编程软件和工具.xlsx",
        "save_name": "03414c9bdaf7a38148742c87b96b8167.xlsx",
        "save_path": "runtime/storage/03414c9bdaf7a38148742c87b96b8167.xlsx",
        "save_path": "/var/www/webman-admin/public/storage/03414c9bdaf7a38148742c87b96b8167.xlsx",
        "url": "/storage/fd2d472da56c71a6da0a5251f5e1b586.png",
        "uniqid ": "03414c9bdaf7a38148742c87b96b8167",
        "size": 15050,
        "mime_type": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
        "extension": "xlsx"
    }
    ...
]

失败,抛出StorageAdapterException异常

成功响应字段

字段 描述 示例值
key 上传文件key webman
origin_name 原始文件名 常用编程软件和工具.xlsx
save_name 保存文件名 03414c9bdaf7a38148742c87b96b8167.xlsx
save_path 文件保存路径(相对) /var/www/webman-admin/runtime/storage/03414c9bdaf7a38148742c87b96b8167.xlsx
url url访问路径 /storage/03414c9bdaf7a38148742c87b96b8167.xlsx
unique_id uniqid 03414c9bdaf7a38148742c87b96b8167
size 文件大小 15050(字节)
mime_type 文件类型 application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
extension 文件扩展名 xlsx

上传规则

默认情况下是上传到本地服务器,会在runtime/storage目录下面生成以当前日期为子目录,以文件流的sha1编码为文件名的文件,例如上面生成的文件名可能是:

runtime/storage/fd2d472da56c71a6da0a5251f5e1b586.png

如果你希望上传的文件是可以直接访问或者下载的话,可以使用public存储方式。

你可以在config/plugin/tinywan/storage/app.php配置文件中配置上传根目录,例如:

'local' => [
    'adapter' => \Tinywan\Storage\Adapter\LocalAdapter::class,
    'root' => public_path() . '/storage',
],

浏览器访问:http://127.0.0.1:8787/storage/fd2d472da56c71a6da0a5251f5e1b586.png

上传验证

支持使用验证类对上传文件的验证,包括文件大小、文件类型和后缀

字段 描述 示例值
single_limit 单个文件的大小限制,默认200M 1024 * 1024 * 200
total_limit 所有文件的大小限制,默认200M 1024 * 1024 * 200
nums 文件数量限制,默认10 10
include 被允许的文件类型列表 ['xlsx','pdf']
exclude 不被允许的文件类型列表 ['png','jpg']

支持上传SDK

阿里云对象存储

composer require aliyuncs/oss-sdk-php

腾讯云对象存储

composer require qcloud/cos-sdk-v5

七牛云云对象存储

composer require qiniu/php-sdk

上传Base64图片

使用场景: 前端直接截图(头像、Canvas等)一个Base64数据流的图片直接上传到云端

请求参数

{
    "extension": "png",
    "base64": "data:image/jpeg;base64,/9j/4AAQSkxxxxxxxxxxxxZJRgABvtyQBIr/MPTPTP/2Q=="
}

请求案例(阿里云)

public function upload(Request $request)
{
    $base64 = $request->post('base64');
    $response = \Tinywan\Storage\Storage::disk(\Tinywan\Storage\Storage::MODE_OSS, false)->uploadBase64($base64,'png');
    var_dump($response);
}

v1.0.0 版本之后不需要调用此方法Tinywan\Storage\Storage::config() 初始化,会自动初始化,默认为配置文件app.php默认配置default配置上传适配器。

响应参数

{
	"save_path": "storage/20220402213639624851671439e.png",
	"url": "http://webman.oss.tinywan.com/storage/20220402213639624851671439e.png",
	"unique_id": "20220402213639624851671439e",
	"size": 11802,
	"extension": "png"
}

上传服务端文件

使用场景: 服务端导出文件需要上传到云端存储,或者零时下载文件存储。

请求案例(阿里云)

$serverFile = runtime_path() . DIRECTORY_SEPARATOR . 'storage/webman.png';
$res = \Tinywan\Storage\Storage::disk(\Tinywan\Storage\Storage::MODE_OSS, false)->uploadServerFile($serverFile);

v1.0.0 版本之后不需要调用此方法Tinywan\Storage\Storage::config() 初始化,会自动初始化,默认为配置文件app.php默认配置default配置上传适配器。

响应参数

{
	"origin_name": "/var/www/webman-admin/runtime/storage/webman.png",
	"save_path": "storage/6edf04d7c26f020cf5e46e6457620220402213414.png",
	"url": "http://webman.oss.tinywan.com/storage/6ed9ffd54d0df57620220402213414.png",
	"unique_id": "6edf04d7c26f020cf5e46e6403213414",
	"size": 3505604,
	"extension": "png"
}

Other

phpstan

vendor/bin/phpstan analyse src

vendor/bin/php-cs-fixer fix src

webman-storage's People

Contributors

edwinhuish avatar tinywan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

webman-storage's Issues

前端以images[]格式上传的话直接报错

<input type="file" name="images[]"/>多图上传的组件经常用到这样的方式。
这样得foreach两次才能拿到上传对象,大概是这个样子:

foreach (request()->file() as $spl_file) {
            foreach ($spl_file as $_file) {
                $upload = Upload::instance();
                $res = $upload->uploadOne($_file);
                $path[] = $res['url'];
            }
        }

用组件的方法$res = Tinywan\Storage\Storage::uploadFile();这样使用就报验证错误,截图找不到了。。。

七牛的适配器有bug

文档中
// 第二个参数要设置为 false
Tinywan\Storage\Storage::config(Storage::MODE_OSS, false);

七牛如果设置为 false 抽象类的 loadConfig 就没有执行 驱动器内部 this->config 就为null
loadConfig 是保护的 无法直接调用

Storage::config(Storage::MODE_OSS, false); 参数2 设置成true 的话
public function getUploadToken(): string
{
if ($this->uploadToken) {
$auth = new Auth($this->config['accessKey'], $this->config['secretKey']);
$this->uploadToken = $auth->uploadToken($this->config['bucket']);
}

    return $this->uploadToken;
}

发现 if ($this->uploadToken) { 这句反了
没提交过pr 我就加个 issue 吧

无法支持多个同类型的storage

如题,无法支持多个同类型的storage,如多个local storage,其中一个为 public 常规文件上传,另外一个为 private 文件上传,或者2个不同 path 的 local storage。

稍后将提交 PR

建议~

👍🏻,建议增加单文件上传(可指定目录及文件名),批量上传可自定义path等实际常用功能。

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.