Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.0.4 #7

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ To Upload a file, use the `WaifuApi::uploadFile` function.

The function accepts an **associative array** of arguments.

| Param (key) | Type | Description | Required | Extra info |
|-----------------|-----------|------------------------------------------------------------------------------------|--------------------------------------------------------|--------------------------------------------------------|
| `file` | `string` | The file path to upload | true only if `url` or `file_contents` is not supplied | If `url` is supplied, this prop can't be set |
| `url` | `string` | The URL to a file that exists on the internet | true only if `file` or `file_contents` is not supplied | If `url` is supplied, this prop has no effect |
| `file_contents` | `string` | The file contents | true only if `url` or `file` is not supplied | If `url` or `file` is supplied, this prop can't be set |
| `expires` | `string` | A string containing a number and a unit (1d = 1day) | false | Valid units are `m`, `h` and `d` |
| `hideFilename` | `boolean` | If true, then the uploaded filename won't appear in the URL | false | Defaults to `false` |
| `password` | `string` | If set, then the uploaded file will be encrypted | false | |
| `filename` | `string` | Only used if `file_contents` or `file` is set, will set the filename of the upload | false | |
| Param (key) | Type | Description | Required | Extra info |
|--------------------|------------|------------------------------------------------------------------------------------|--------------------------------------------------------|--------------------------------------------------------|
| `file` | `string` | The file path to upload | true only if `url` or `file_contents` is not supplied | If `url` is supplied, this prop can't be set |
| `url` | `string` | The URL to a file that exists on the internet | true only if `file` or `file_contents` is not supplied | If `url` is supplied, this prop has no effect |
| `file_contents` | `string` | The file contents | true only if `url` or `file` is not supplied | If `url` or `file` is supplied, this prop can't be set |
| `expires` | `string` | A string containing a number and a unit (1d = 1day) | false | Valid units are `m`, `h` and `d` |
| `hideFilename` | `boolean` | If true, then the uploaded filename won't appear in the URL | false | Defaults to `false` |
| `password` | `string` | If set, then the uploaded file will be encrypted | false | |
| `filename` | `string` | Only used if `file_contents` or `file` is set, will set the filename of the upload | false | |
| `oneTimeDownload ` | `boolean` | If true, the file is deleted after the first access | false | |

#### WaifuResponse Return value<a id="waifuresponse-object"></a>

Expand All @@ -54,8 +55,12 @@ The function returns a `WaifuResponse` object, throws and `Exception` or `WaifuE
ErnestMarcinko\WaifuVault\WaifuResponse (4) {
["token"]=> string(36) "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
["url"]=> string(74) "https://waifuvault.moe/f/{timestamp}/{filename}.{file_ext}"
["protected"]=> bool(true)
["retentionPeriod"]=> string(39) "334 days 20 hours 16 minutes 23 seconds"
["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),
}
}
```

Expand Down
22 changes: 19 additions & 3 deletions src/WaifuApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
* url: string
* }
*
* @phpstan-type uploadFileArg FileUpload|FileContentUpload|UrlUpload
* @phpstan-type uploadFileArg array{
* expires?:string,
* hide_filename?:bool,
* password?:string,
* one_time_download?:bool
* }&(FileUpload|FileContentUpload|UrlUpload)
*
* @phpstan-type modifyFileArg array{
* token: string,
* password?: string,
Expand Down Expand Up @@ -57,19 +63,29 @@ public function __construct(private readonly RequestHandler $requestHandler = ne
public function uploadFile(array $args): WaifuResponse {
$post_fields = [];
$url = self::REST_URL;
$params = http_build_query(array_filter(
$params = array_filter(
$args,
function ($v, $k) {
return in_array($k, array(
'expires',
'hide_filename',
'password')) && !is_null($v); // @phpstan-ignore-line
'password',
'one_time_download')) && !is_null($v); // @phpstan-ignore-line
},
ARRAY_FILTER_USE_BOTH
);
/**
* Convert boolean params to "true" or "false" strings, because
* http_build_query will convert them to 1 or 0 integers, which throws an API Exception
*/
$params = http_build_query(array_map(
fn($v)=>is_bool($v) ? ($v ? 'true' : 'false') : $v, // @phpstan-ignore-line
$params
));
if ($params !== '') {
$url .= '?' . $params;
}

if (isset($args['url'])) {
$post_fields['url'] = $args['url'];
} elseif (isset($args['file'])) {
Expand Down
19 changes: 13 additions & 6 deletions src/WaifuResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,24 @@

namespace ErnestMarcinko\WaifuVault;

/**
* @phpstan-import-type WaifuResponseOptionsArgs from WaifuResponseOptions
*/
class WaifuResponse {
/**
* @param string $token
* @param string $url
* @param bool $protected
* @param int|string $retentionPeriod
* @param WaifuResponseOptionsArgs|WaifuResponseOptions $options
*/
public function __construct(
public string $token = '',
public string $url = '',
public bool $protected = false,
public int|string $retentionPeriod = ''
) {}
readonly public string $token = '',
readonly public string $url = '',
readonly public int|string $retentionPeriod = '',
public array|WaifuResponseOptions $options = array(),
) {
if (is_array($this->options)) {
$this->options = new WaifuResponseOptions(...$this->options);
}
}
}
23 changes: 23 additions & 0 deletions src/WaifuResponseOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ErnestMarcinko\WaifuVault;

/**
* @phpstan-type WaifuResponseOptionsArgs array{
* hideFilename?: bool,
* oneTimeDownload?: bool,
* protected?: bool
* }
*/
readonly class WaifuResponseOptions {
/**
* @param bool $hideFilename
* @param bool $oneTimeDownload
* @param bool $protected
*/
public function __construct(
public bool $hideFilename = false,
public bool $oneTimeDownload = false,
public bool $protected = false
) {}
}
8 changes: 6 additions & 2 deletions tests/WaifuTests/WaifuApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,12 @@ public function setUp(): void {
$args = [
"token" => "13b2485a-1010-4e3e-8f75-20f2a0c50b56",
"url" => "https://waifuvault.moe/f/1711098733870/image.jpg",
"protected" => false,
"retentionPeriod" => "300 days 10 hours 5 minutes 1 second"
"retentionPeriod" => "300 days 10 hours 5 minutes 1 second",
"options" => [
"hideFilename" => true,
"oneTimeDownload" => false,
"protected" => false,
]
];
$this->waifuResponse = new WaifuResponse(...$args);
$this->waifu = new WaifuApi();
Expand Down
8 changes: 6 additions & 2 deletions tests/WaifuTests/WaifuRequestHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,12 @@ public function setUp(): void {
$args = [
"token" => "13b2485a-1010-4e3e-8f75-20f2a0c50b56",
"url" => "https://waifuvault.moe/f/1711098733870/image.jpg",
"protected" => false,
"retentionPeriod" => "300 days 10 hours 5 minutes 1 second"
"retentionPeriod" => "300 days 10 hours 5 minutes 1 second",
"options" => [
"hideFilename" => true,
"oneTimeDownload" => false,
"protected" => false,
]
];
$this->waifuResponse = new WaifuResponse(...$args);
}
Expand Down