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

Feature/update #212

Merged
merged 10 commits into from
Nov 8, 2023
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ 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.2.0]
- Added [multi-page document conversion](https://uploadcare.com/docs/transformations/document-conversion/#multipage-conversion).
- Added [Unsafe content detection](https://uploadcare.com/docs/unsafe-content/).
- `\Uploadcare\Group::getDatetimeStored` method is deprecated.

## [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
38 changes: 34 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,29 @@ while (($status = $api->addons()->checkAwsRecognition($token)) !== 'done') {
$recognitionData = $api->file()->fileInfo($file->getUuid())->getAppdata()->getAwsRekognitionDetectLabels(); // Instance of \Uploadcare\Interfaces\File\AppData\AwsRecognitionLabelsInterface
```

### [Unsafe content detection](https://uploadcare.com/docs/unsafe-content/)

To call unsafe content detection from the library:

```php
$configuration = \Uploadcare\Configuration::create($_ENV['UPLOADCARE_PUBLIC_KEY'], $_ENV['UPLOADCARE_SECRET_KEY']);

$api = new \Uploadcare\Api($configuration);
/** @var \Uploadcare\Interfaces\File\FileInfoInterface $file */
$file = $api->file()->listFiles()->getResults()->first();

# Request recognition, get token to check status
$token = $api->addons()->requestAwsRecognitionModeration($file);
while (($status = $api->addons()->checkAwsRecognitionModeration($token)) !== 'done') {
\usleep(1000);
if ($status === 'error') {
throw new \Exception('Error in process');
}
}

$recognitionModerationData = $api->file()->fileInfo($file->getUuid())->getAppdata()->getAwsRekognitionDetectModerationLabels() // Instance of \Uploadcare\Interfaces\File\AppData\AwsModerationLabelInterface
```

### [Remove background](https://uploadcare.com/docs/remove-bg/)

Remove background from image:
Expand Down Expand Up @@ -347,8 +370,7 @@ After that, you can access group operation methods:

### `Uploadcare\Group` class

This class implements `Uploadcare\Interfaces\GroupInterface` aand has an additional `store()` method that applies the store operation to the group. Calls `GroupApi::store group`;

This class implements `Uploadcare\Interfaces\GroupInterface`.
The `getFiles()` method of the `Uploadcare\Group` object returns [FileCollection](#uploadcarefilecollection-class).

## Project operations
Expand All @@ -373,7 +395,7 @@ Now, the `$projectInfo` variable contains the `Uploadcare\Interfaces\Response\Pr
- `getPubKey()` — Project public key as string.
- `isAutostoreEnabled()` — Returns `true` if the project files are stored automatically.

## Webhooks
## [Webhooks](https://uploadcare.com/docs/webhooks/)

Call the webhook API:

Expand All @@ -385,7 +407,7 @@ $webhookApi = (new \Uploadcare\Api($config))->webhook();
The methods are:

- `listWebhooks()` — Returns a list of project webhooks as an instance of an `Uploadcare\WebhookCollection` class. Each element of this collection is an instance of a `Uploadcare\Webhook` class (see below);
- `createWebhook(string $targetUrl, bool $isActive = true, string $signingSecret = null, string $event = 'file.uploaded')` — Creates a new webhook for the event. Returns the `Uploadcare\Webhook` class.
- `createWebhook(string $targetUrl, bool $isActive = true, string $signingSecret = null, string $event = 'file.uploaded')` — Creates a new webhook for the event. Returns the `Uploadcare\Webhook` class. Event types described [here](https://uploadcare.com/docs/webhooks/#event-types)
- `updateWebhook($id, array $parameters)` — Updates an existing webhook with these parameters. Parameters can be:
- `target_url` — A target callback URL;
- `event` — The only `file.uploaded` event is supported at the moment.
Expand Down Expand Up @@ -435,6 +457,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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$configuration = Uploadcare\Configuration::create((string) $_ENV['UPLOADCARE_PUBLIC_KEY'], (string) $_ENV['UPLOADCARE_SECRET_KEY']);
$api = (new Uploadcare\Api($configuration))->addons();
$status = $api->checkAwsRecognitionModeration('request-id');

echo \sprintf('Recognition status: %s', $status);
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

$configuration = Uploadcare\Configuration::create((string) $_ENV['UPLOADCARE_PUBLIC_KEY'], (string) $_ENV['UPLOADCARE_SECRET_KEY']);
$api = (new Uploadcare\Api($configuration))->addons();
$resultKey = $api->requestAwsRecognitionModeration('1bac376c-aa7e-4356-861b-dd2657b5bfd2');

echo \sprintf('Recognition requested. Key is \'%s\'', $resultKey);
2 changes: 1 addition & 1 deletion api-samples/rest-api/post-webhooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
$configuration = Uploadcare\Configuration::create((string) $_ENV['UPLOADCARE_PUBLIC_KEY'], (string) $_ENV['UPLOADCARE_SECRET_KEY']);
$api = (new Uploadcare\Api($configuration))->webhook();

$result = $api->createWebhook('https://yourwebhook.com', true, 'sign-secret');
$result = $api->createWebhook('https://yourwebhook.com', true, 'sign-secret', 'file.uploaded'); // The event can be any of `file.uploaded`, `file.info_updated`, `file.deleted`, `file.stored`, `file.infected`

echo \sprintf('Webhook %s created', $result->getId());
2 changes: 1 addition & 1 deletion api-samples/upload-api/post-from_url.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
$configuration = Uploadcare\Configuration::create((string) $_ENV['UPLOADCARE_PUBLIC_KEY'], (string) $_ENV['UPLOADCARE_SECRET_KEY']);
$uploader = new Uploadcare\Uploader\Uploader($configuration);

$url = 'https://source.unsplash.com/random';
$url = 'https://source.unsplash.com/featured';
$token = $uploader->fromUrl($url, null, null, 'auto', [
'action' => 'upload from URL', // The "Action" is the metadata key
'checkDuplicates' => true,
Expand Down
17 changes: 17 additions & 0 deletions src/Apis/AddonsApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@

class AddonsApi extends AbstractApi implements AddonsApiInterface
{
public function requestAwsRecognitionModeration($id): string
{
$response = $this->request('POST', '/addons/aws_rekognition_detect_moderation_labels/execute/', [
'body' => ['target' => (string) $id],
])->getBody()->getContents();

return $this->getResponseParameter($response, 'request_id');
}

public function checkAwsRecognitionModeration(string $id): string
{
$uri = \sprintf('/addons/aws_rekognition_detect_moderation_labels/execute/status/?request_id=%s', $id);
$response = $this->request('GET', $uri)->getBody()->getContents();

return $this->getResponseParameter($response, 'status');
}

public function requestAwsRecognition($id): string
{
$response = $this->request('POST', '/addons/aws_rekognition_detect_labels/execute/', [
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;
}
}
2 changes: 1 addition & 1 deletion src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
final class Configuration implements ConfigurationInterface
{
public const LIBRARY_VERSION = 'v4.1.1';
public const LIBRARY_VERSION = 'v4.2.0';
public const API_VERSION = '0.7';
public const API_BASE_URL = 'api.uploadcare.com';
public const USER_AGENT_TEMPLATE = 'PHPUploadcare/{lib-version}/{publicKey} (PHP/{lang-version})';
Expand Down
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;
}
}
15 changes: 15 additions & 0 deletions src/File/AppData.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Uploadcare\File;

use Uploadcare\File\AppData\AwsRecognitionLabels;
use Uploadcare\File\AppData\AwsRecognitionModerationLabels;
use Uploadcare\File\AppData\ClamAvVirusScan;
use Uploadcare\File\AppData\RemoveBg;
use Uploadcare\Interfaces\File\AppDataInterface;
Expand All @@ -11,18 +12,32 @@
final class AppData implements AppDataInterface, SerializableInterface
{
private ?AwsRecognitionLabels $awsRecognitionLabels = null;
private ?AwsRecognitionModerationLabels $awsRekognitionDetectModerationLabels = null;
private ?ClamAvVirusScan $clamAvVirusScan = null;
private ?RemoveBg $removeBg = null;

public static function rules(): array
{
return [
'awsRekognitionDetectLabels' => AwsRecognitionLabels::class,
'awsRekognitionDetectModerationLabels' => AwsRecognitionModerationLabels::class,
'ucClamavVirusScan' => ClamAvVirusScan::class,
'removeBg' => RemoveBg::class,
];
}

public function getAwsRekognitionDetectModerationLabels(): ?AwsRecognitionModerationLabels
{
return $this->awsRekognitionDetectModerationLabels;
}

public function setAwsRekognitionDetectModerationLabels(?AwsRecognitionModerationLabels $labels): self
{
$this->awsRekognitionDetectModerationLabels = $labels;

return $this;
}

public function getAwsRekognitionDetectLabels(): ?AwsRecognitionLabels
{
return $this->awsRecognitionLabels;
Expand Down
46 changes: 46 additions & 0 deletions src/File/AppData/AbstractAwsLabels.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php declare(strict_types=1);

namespace Uploadcare\File\AppData;

abstract class AbstractAwsLabels
{
protected ?string $version = null;
protected ?\DateTimeInterface $datetimeCreated = null;
protected ?\DateTimeInterface $datetimeUpdated = null;

public function getVersion(): ?string
{
return $this->version;
}

public function setVersion(?string $version): self
{
$this->version = $version;

return $this;
}

public function getDatetimeCreated(): ?\DateTimeInterface
{
return $this->datetimeCreated;
}

public function setDatetimeCreated(?\DateTimeInterface $datetimeCreated): self
{
$this->datetimeCreated = $datetimeCreated;

return $this;
}

public function getDatetimeUpdated(): ?\DateTimeInterface
{
return $this->datetimeUpdated;
}

public function setDatetimeUpdated(?\DateTimeInterface $datetimeUpdated): self
{
$this->datetimeUpdated = $datetimeUpdated;

return $this;
}
}
58 changes: 58 additions & 0 deletions src/File/AppData/AwsModerationLabel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types=1);

namespace Uploadcare\File\AppData;

use Uploadcare\Interfaces\File\AppData\AwsRecognitionData\AwsModerationLabelInterface;
use Uploadcare\Interfaces\SerializableInterface;

class AwsModerationLabel implements AwsModerationLabelInterface, SerializableInterface
{
private ?float $confidence = null;
private ?string $name = null;
private ?string $parentName = null;

public static function rules(): array
{
return [
'confidence' => 'float',
'name' => 'string',
'parentName' => 'string',
];
}

public function getConfidence(): ?float
{
return $this->confidence;
}

public function setConfidence(?float $confidence): self
{
$this->confidence = $confidence;

return $this;
}

public function getName(): ?string
{
return $this->name;
}

public function setName(?string $name): self
{
$this->name = $name;

return $this;
}

public function getParentName(): ?string
{
return $this->parentName;
}

public function setParentName(?string $parentName): self
{
$this->parentName = $parentName;

return $this;
}
}
Loading
Loading