spazzmarticus / tus-server
Tus.io协议服务器实现,遵循PSR标准
Requires
- php: ^8.2|^8.3
- psr/event-dispatcher: ^1.0
- psr/http-factory: ^1.0.2
- psr/http-server-handler: ^1.0.2
- psr/log: ^1.1|^2.0|^3.0
- psr/simple-cache: ^1.0|^2.0|^3.0
- ramsey/uuid: ^4.7.5
Requires (Dev)
- cache/filesystem-adapter: ^1.2.0
- friendsofphp/php-cs-fixer: ^3.51.0
- laminas/laminas-diactoros: ^3.3.1
- laminas/laminas-httphandlerrunner: ^2.10.0
- mikey179/vfsstream: ^1.6.11
- monolog/monolog: ^3.5.0
- phpstan/phpstan: ^1.10.60
- phpstan/phpstan-phpunit: ^1.3
- phpunit/phpunit: ^11.0.5
- psr/http-server-middleware: ^1.0.2
- rector/rector: ^1.0.2
- symfony/event-dispatcher: ^7.0.3
README
这是一个使用PSR HTTP标准实现的"tus.io 可恢复文件上传"协议的服务器实现。
安装
使用 Composer 进行安装
composer require spazzmarticus/tus-server
PSR
实现
- PSR-15: HTTP服务器请求处理器 -
TusServer
实现Psr\Http\Server\RequestHandlerInterface
使用
-
PSR-3: 日志接口 - (可选) 向
TusServer
传递一个Psr\Log\LoggerInterface
-
PSR-7: HTTP消息接口 - 必须将
Psr\Http\Message\ServerRequestInterface
的实例传递给TusServer
。 -
PSR-17: HTTP工厂 - 使用
Psr\Http\Message\ResponseFactoryInterface
创建响应。 -
PSR-16: 简单缓存 - 在上传过程中用于存储元数据(文件路径、客户端传递的
Upload-Metadata
等)。[参见下文“缓存作为存储?!”] -
PSR-12: 扩展编码风格指南 - 代码按照 PSR-12 编写和格式化。
演示
您可以通过安装开发依赖项(composer install
)并运行提供的 server.php
来演示 TusServer
。
php -S localhost:8000 example/server.php
打开您的浏览器,访问 localhost:8000/ 并使用 (Uppy) 进行上传。
上传存储在 example/uploads/...
,文件系统缓存在 example/cache/
。
访问 localhost:8000/reset 以永久删除上传、中间块和元数据存储。可能存在错误日志在 example/log/php-error.php
和包含一些额外信息的服务器日志在 example/log/tus-server.log
中。
测试
自动测试使用
示例
- Slim v4 - Slim 路由允许直接调用此 tus-server 实现。
👋 这是我的 tus-server 使用方法。
缓存作为存储?!?
TusServer
需要一些快速存储上传元数据的东西。由于有效负载很小且性能很重要,可以使用缓存。
除了使用易失性缓存之外,您应该使用包含快速易失性和较慢的非易失性缓存的链。 (上传过程中丢失元数据不允许恢复上传。 )
👋 我使用 symfony/cache
$volatileCache = new Symfony\Component\Cache\Adapter\ApcuAdapter('...'); $nonVolatileCache = new Symfony\Component\Cache\Adapter\FilesystemAdapter('', 0, __DIR__ . '/...'); $cacheChain = new Symfony\Component\Cache\Adapter\ChainAdapter([$volatileCache, $nonVolatileCache]); $storage = new Symfony\Component\Cache\Psr16Cache($cacheChain);
替代方案
- ankitpokhrel/tus-php - 没有提供足够的灵活性来满足我的需求,这也是我决定开始自己的实现的原因。(如果你正在寻找php tus-client,它提供了。)