tinywan/storage

webman storage plugin

v1.1.0 2024-09-07 09:03 UTC

This package is auto-updated.

Last update: 2024-11-07 09:28:34 UTC


README

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

特性

安装

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异常

成功响应字段

上传规则

默认情况下是上传到本地服务器,会在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

上传验证

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

支持上传SDK

阿里云对象存储

composer require aliyuncs/oss-sdk-php

腾讯云对象存储

composer require qcloud/cos-sdk-v5

七牛云云对象存储

composer require qiniu/php-sdk

亚马逊(S3)对象存储

composer require league/flysystem-aws-s3-v3

上传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