Skip to content

Commit

Permalink
chore: apply changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyrisbee committed Dec 20, 2023
1 parent 55f8a96 commit 1a23dbc
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 35 deletions.
16 changes: 3 additions & 13 deletions src/Requests/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,9 @@ public function list(): array
*/
public function create(UploadedFile $file, array $arguments): MediaObject
{
$uri = sprintf('/media?%s', http_build_query($arguments));

$data = $this->request(
method: 'post',
path: $uri,
options: [
'file' => [
'name' => $file->getFilename(),
'resource' => $file->getContent(),
'mime' => $file->getMimeType(),
],
],
);
$arguments['file'] = $file;

$data = $this->request('post', '/media', $arguments);

if (is_array($data)) {
throw $this->unexpectedValueException();
Expand Down
25 changes: 3 additions & 22 deletions src/Requests/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Storipress\WordPress\Requests;

use Illuminate\Http\Client\Response;
use Illuminate\Http\UploadedFile;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;
use stdClass;
Expand All @@ -18,7 +19,6 @@
use Storipress\WordPress\Exceptions\NotFoundException;
use Storipress\WordPress\Exceptions\TermExistsException;
use Storipress\WordPress\Exceptions\UnauthorizedException;
use Storipress\WordPress\Exceptions\UnexpectedResourceException;
use Storipress\WordPress\Exceptions\UnexpectedValueException;
use Storipress\WordPress\Exceptions\UnknownException;
use Storipress\WordPress\Exceptions\WordPressException;
Expand Down Expand Up @@ -57,27 +57,8 @@ protected function request(
$http->withUserAgent($this->app->userAgent());
}

if (isset($options['file'])) {
$filename = data_get($options, 'file.name');

$resource = data_get($options, 'file.resource');

$mime = data_get($options, 'file.mime');

if (!is_string($filename) || !is_string($resource) || !is_string($mime)) {
throw new UnexpectedResourceException(WordPressError::from((object) [
'code' => '400',
'message' => 'Unexpected resource value.',
'data' => (object) [
'file' => $options['file'],
],
]), 400);
}

$contentDisposition = sprintf('attachment; filename="%s"', $filename);

$http->withHeader('Content-Disposition', $contentDisposition)
->withBody($resource, $mime);
if (isset($options['file']) && $options['file'] instanceof UploadedFile) {
$http->attach('file', $options['file']->getContent(), $options['file']->getClientOriginalName());

unset($options['file']);
}
Expand Down

0 comments on commit 1a23dbc

Please sign in to comment.