ernestmarcinko / waifuvault-php-api
SDK,用于与 WaifuVault 临时文件托管服务交互
Requires
- php: ^8.3
Requires (Dev)
- ernestmarcinko/mockutils: ^1.0
- phpstan/phpstan: ^1.10
- phpunit/phpunit: 11.*
- squizlabs/php_codesniffer: ^3.8
This package is auto-updated.
Last update: 2024-09-13 20:29:54 UTC
README
这包含上传、删除和获取文件(通过waifuvault.moe)的官方 API 绑定。包含用于通过 PHP 与服务交互的最新完整 API
通过 Composer 安装
composer require ernestmarcinko/waifuvault-php-api
用法
此 API 包含 4 种交互
所需的唯一类是来自 ErnestMarcinko\WaifuVault
的 WaifuApi
类
use ErnestMarcinko\WaifuVault\WaifuApi;
上传文件
要上传文件,请使用 WaifuApi::uploadFile
函数。
参数
该函数接受一个 关联数组 的参数。
WaifuResponse 返回值
该函数返回一个 WaifuResponse
对象,抛出 Exception
或 WaifuException
。几乎所有的 SDK 函数都将使用此对象作为它们的返回值。
// WaifuResponse object: ErnestMarcinko\WaifuVault\WaifuResponse (4) { ["token"]=> string(36) "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx" ["url"]=> string(74) "https://waifuvault.moe/f/{timestamp}/{filename}.{file_ext}" ["retentionPeriod"]=> string(39) "334 days 20 hours 16 minutes 23 seconds", ["options"] ErnestMarcinko\WaifuVault\WaifuResponseOptions (4) { ["hideFilename"]=> bool(false), ["oneTimeDownload"]=> bool(false), ["protected"]=> bool(false), } }
示例
使用 URL
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->uploadFile(array( 'url' => 'https://waifuvault.moe/assets/custom/images/08.png', )); var_dump($response);
使用文件路径
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->uploadFile(array( 'file' => __DIR__ . '/image.jpg', )); var_dump($response);
使用文件内容
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->uploadFile(array( 'file_contents' => file_get_contents(__DIR__ . '/image.jpg'), 'filename' => 'image.jpg', )); var_dump($response);
获取文件信息
使用 WaifuApi::getFileInfo
函数获取文件信息。
参数
返回值
WaifuApi::getFileInfo
返回一个 WaifuResponse
示例
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->getFileInfo('someToken'); var_dump($response);
可读时间戳保留期
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->getFileInfo('someToken'); var_dump($response->retentionPeriod); // 328 days 18 hours 51 minutes 31 seconds
删除文件
使用 WaifuApi::deleteEntry
函数获取文件信息。
参数
返回值
注意:
deleteFile
只会返回true
或在令牌无效时抛出异常。
示例
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->deleteEntry('someToken');
获取文件
使用 WaifuApi::getFile
函数通过提供令牌或文件的唯一标识符(纪元/文件名)从服务器获取文件内容。
参数
该函数接受一个 关联数组 的参数。
重要: 唯一标识符文件名仅在未隐藏文件名时是纪元/文件名,如果隐藏了文件名,则只是纪元。例如:
1710111505084/08.png
是名为08.png
的标准上传文件的唯一标识符,如果以隐藏文件名上传,则它将是1710111505084.png
返回值
该函数返回文件内容。
示例
获取加密文件
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->uploadFile(array( 'url' => 'https://waifuvault.moe/assets/custom/images/08.png', 'password' => 'epic' )); $contents = $waifu->getFile(array( 'token' => $response->token, 'password' => 'epic' )); var_dump($contents);
从唯一标识符获取文件
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $contents = $waifu->getFile(array( 'filename' => '/1710111505084/08.png' )); var_dump($contents);
修改条目
使用 WaifuApi::modifyEntry
修改条目的某些方面,例如密码、删除密码、解密文件、加密文件、更改过期时间等。
参数
该函数接受一个 关联数组 的参数。
返回值
WaifuApi::getFileInfo
返回一个 WaifuResponse
示例
对非加密文件设置密码
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->modifyEntry(array( 'token' => 'token', 'password' => 'apple' )); var_dump($response->protected);
更改密码
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->modifyEntry(array( 'token' => 'token', 'password' => 'newPass' 'previousPassword' => 'apple' )); var_dump($response->protected);
更改过期时间
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->modifyEntry(array( 'token' => 'token', 'customExpiry' => "1d" )); var_dump($response->retentionPeriod);
解密文件并删除密码
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); $response = $waifu->modifyEntry(array( 'token' => 'token', 'password' => '' 'previousPassword' => 'apple' )); var_dump($response->protected);
自定义请求处理器
可以实例化 WaifuApi 对象与自定义请求处理器(实现 RequestHandler 接口)。内置请求处理器使用 CURL。
默认请求处理器
在没有任何参数的情况下实例化类时,将使用默认请求处理器。
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi(); // which is equivalent to: $waifu = new WaifuApi( new WaifuRequestHandler() );
创建您的自定义请求处理器
如果您更喜欢使用自己的方法来处理请求,您可以创建自己的类,并将其传递给 WaifuApi
构造函数。该类必须实现 ErnestMarcinko\WaifuVault\RequestHandler
接口。
use ErnestMarcinko\WaifuVault\RequestMethods; use ErnestMarcinko\WaifuVault\RequestHandler; class MyCustomWaifuRequestHandler implements RequestHandler { public function make( RequestMethods $method, string $endpoint, array|null $header = null, array|string|bool|null $post_fields = null ): static { // do your thing here return $this; } public function getWaifu(): WaifuResponse { return new WaifuResponse(); } public function getTrue(): true { return true; } public function getRaw(): string { return 'raw file content'; } }
然后通过依赖注入(DI)使用它。
use ErnestMarcinko\WaifuVault\WaifuApi; $waifu = new WaifuApi( new MyCustomWaifuRequestHandler() );