Skip to content

Commit

Permalink
Multipage conversion with the 'save in group' parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew72ru committed Nov 5, 2023
1 parent a3d51a9 commit f7ec091
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ The format is based now on [Keep a
Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to
[Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [4.1.2]
- Possibility to store conversion details in a separate group.

## [4.1.1]
- Retrieve the Metadata without additional requests when using `getMetadata`.
- You can still get the fresh metadata for the file by calling `getMetadata` method from the `MetadataApiInterface`.
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,14 @@ Result will contain one of two objects:

The `ConvertedItemInterface` will contain a UUID of converted document and token with conversion job ID. You can request the conversion job status with this ID (or the `ConvertedItemInterface` object itself):

You can also pass the `true` to the `setSaveInGroup` method to the Request object.

```php
$request = (new \Uploadcare\Conversion\DocumentConversionRequest('pdf'))->setSaveInGroup(true);
```

In this case, the result of the document conversion will be stored in a group. See further details [here](https://uploadcare.com/docs/transformations/document-conversion/#multipage-conversion).

```php
$status = $convertor->documentJobStatus($result); // or $result->getToken()
```
Expand Down
9 changes: 7 additions & 2 deletions src/Apis/ConversionApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private function requestDocumentConversion(array $conversionParams): BatchRespon
}

/**
* @param array $ids File ID's
* @param string[] $ids File ID's
*/
private function makeDocumentConversionUrl(array $ids, DocumentConversionRequestInterface $request): array
{
Expand All @@ -268,9 +268,14 @@ private function makeDocumentConversionUrl(array $ids, DocumentConversionRequest
}
}

return [
$requestParameters = [
'paths' => $patch,
'store' => $request->store(),
];
if ($request->isSaveInGroup() === true) {
$requestParameters['save_in_group'] = true;
}

return $requestParameters;
}
}
13 changes: 13 additions & 0 deletions src/Conversion/DocumentConversionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class DocumentConversionRequest implements DocumentConversionRequestInterface
private bool $throwError = false;
private bool $store = true;
private ?int $pageNumber = null;
private bool $saveToGroup = false;

public function __construct(string $targetFormat = 'pdf', bool $throwError = false, bool $store = true, ?int $pageNumber = null)
{
Expand Down Expand Up @@ -76,4 +77,16 @@ public function setPageNumber(?int $pageNumber): self

return $this;
}

public function setSaveInGroup(bool $saveInGroup): self
{
$this->saveToGroup = $saveInGroup;

return $this;
}

public function isSaveInGroup(): bool
{
return $this->pageNumber !== null ? false : $this->saveToGroup;
}
}
5 changes: 5 additions & 0 deletions src/Conversion/VideoEncodingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,9 @@ private function checkTime(string $time): void
throw new InvalidArgumentException(\sprintf('Time string \'%s\' not valid', $time));
}
}

public function isSaveInGroup(): bool
{
return false;
}
}
2 changes: 2 additions & 0 deletions src/Interfaces/Conversion/ConversionRequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ public function getTargetFormat(): string;
public function throwError(): bool;

public function store(): bool;

public function isSaveInGroup(): bool;
}
4 changes: 3 additions & 1 deletion tests/Metadata/MetadataRealApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ public function testFileMetadataLoad(): void
$value = \date_create()->format(\DateTimeInterface::ATOM);

$api->metadata()->setKey($file, $key, $value);
$dateOne = \date_create($value)->format('Y-m-d');
$dateTwo = \date_create($file->getMetadata()->offsetGet($key))->format('Y-m-d');

self::assertSame($value, $file->getMetadata()->offsetGet($key));
self::assertSame($dateOne, $dateTwo);
}
}
48 changes: 47 additions & 1 deletion tests/Metadata/MetadataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

namespace Tests\Metadata;

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\ConnectException;
use GuzzleHttp\Psr7\Request;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\String\ByteString;
use Uploadcare\Apis\MetadataApi;
use Uploadcare\Configuration;
use Uploadcare\Exception\MetadataException;
use Uploadcare\Exception\{HttpException, MetadataException};
use Uploadcare\File\Metadata;
use Uploadcare\Security\Signature;
use Uploadcare\Serializer\SerializerFactory;

class MetadataTest extends TestCase
{
Expand Down Expand Up @@ -55,4 +61,44 @@ public function testSetKeyMethodWithWrongValue(): void

$api->setKey(\uuid_create(), $key, $value);
}

public function testRemoveReyWithTheWrongKey(): void
{
$key = ByteString::fromRandom(128)->toString();

$api = new MetadataApi(Configuration::create('demo', 'demo'));
$this->expectException(MetadataException::class);
$this->expectExceptionMessageMatches('/Key should be string up to 64 characters length/');

$api->removeKey(\uuid_create(), $key);
}

public function testRemoveKeyWithAnErrorInTheResponse(): void
{
$client = $this->createMock(ClientInterface::class);
$client->expects(self::atLeastOnce())->method('request')->willThrowException(new ConnectException('Error string', new Request('DELETE', 'https://example.com')));
$configuration = new Configuration('demo', new Signature('demo'), $client, SerializerFactory::create());

$key = 'validString';
$api = new MetadataApi($configuration);
$this->expectException(HttpException::class);

$api->removeKey(\uuid_create(), $key);
}

public function testRemoveKeyWithTheWrongResponseStatus(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->expects(self::atLeastOnce())->method('getStatusCode')->willReturn(401);
$client = $this->createMock(ClientInterface::class);
$client->expects(self::atLeastOnce())->method('request')->willReturn($response);
$configuration = new Configuration('demo', new Signature('demo'), $client, SerializerFactory::create());

$key = 'validString';
$api = new MetadataApi($configuration);
$this->expectException(HttpException::class);
$this->expectExceptionMessageMatches('/Wrong response. Call to support/');

$api->removeKey(\uuid_create(), $key);
}
}

0 comments on commit f7ec091

Please sign in to comment.