Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
tistre committed Feb 27, 2024
2 parents 589bf75 + cc5cd98 commit a8ae4d7
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,11 @@
# Change Log

## 6.2.0 - 2024-02-27

Add AssetsUtils::replaceInvalidFilenameCharacters().

Improve error logging in CreateRequest, UpdateRequest.

## 6.1.0 - 2024-02-26

Fix CreateRequest::$parseMetadataModifications and CreateRequest::$metadataToReturn parameters being ignored. Add
Expand Down
16 changes: 16 additions & 0 deletions src/AssetsUtils.php
Expand Up @@ -134,6 +134,22 @@ public static function escapeForElasticsearch(string $queryTerm): string
}


/**
* Invalid file name characters in Assets Server
* @see https://helpcenter.woodwing.com/hc/en-us/articles/360042269611-Invalid-file-name-characters-in-Assets-Server
*/
public static function getInvalidFilenameCharacters(): array
{
return ['\\', '/', ':', '*', '?', '"', '<', '>', '|', '#', ';'];
}


public static function replaceInvalidFilenameCharacters(string $subject, string $replace): string
{
return str_replace(self::getInvalidFilenameCharacters(), $replace, $subject);
}


/**
* Get a Twig template for building an Assets query
*
Expand Down
23 changes: 17 additions & 6 deletions src/Service/CreateRequest.php
Expand Up @@ -15,11 +15,11 @@
class CreateRequest extends CreateRequestBase
{
public function __construct(
AssetsClient $assetsClient,
mixed $filedata = null,
array $metadata = [],
array $metadataToReturn = ['all'],
bool $parseMetadataModification = false,
AssetsClient $assetsClient,
mixed $filedata = null,
array $metadata = [],
array $metadataToReturn = ['all'],
bool $parseMetadataModification = false,
readonly bool $autoRename = false
)
{
Expand Down Expand Up @@ -48,7 +48,18 @@ public function __invoke(): AssetResponse
try {
$response = $this->assetsClient->serviceRequest('create', $requestData);
} catch (Exception $e) {
throw new AssetsException(sprintf('%s: Create failed: %s', __METHOD__, $e->getMessage()), $e->getCode(), $e);
$logData = array_filter($requestData, fn($key) => ($key !== 'Filedata'), ARRAY_FILTER_USE_KEY);

throw new AssetsException(
sprintf(
'%s: Create failed - <%s> - <%s>',
__METHOD__,
$e->getMessage(),
json_encode($logData)
),
$e->getCode(),
$e
);
}

$assetResponse = AssetResponse::createFromJson($response);
Expand Down
4 changes: 3 additions & 1 deletion src/Service/UpdateRequest.php
Expand Up @@ -51,13 +51,15 @@ public function __invoke(): void
try {
$this->assetsClient->serviceRequest('update', $requestData);
} catch (Exception $e) {
$logData = array_filter($requestData, fn($key) => ($key !== 'Filedata'), ARRAY_FILTER_USE_KEY);

throw new AssetsException(
sprintf(
'%s: Update failed for asset <%s> - <%s> - <%s>',
__METHOD__,
$this->id->id,
$e->getMessage(),
json_encode($requestData)
json_encode($logData)
),
$e->getCode(),
$e
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/AssetsUtilsTest.php
Expand Up @@ -39,6 +39,17 @@ public static function elasticsearchEscapeProvider(): array
}


public function testReplaceInvalidFilenameCharacters(): void
{
$invalidCharacters = AssetsUtils::getInvalidFilenameCharacters();
$input = 'test' . implode('', $invalidCharacters);
$replace = '-';
$expected = 'test' . str_repeat($replace, count($invalidCharacters));

$this->assertEquals($expected, AssetsUtils::replaceInvalidFilenameCharacters($input, $replace));
}


public function testGetQueryTemplate(): void
{
$templateStr = '{% if ID %} title:"prefix {{ ID }}" {% endif %}';
Expand Down

0 comments on commit a8ae4d7

Please sign in to comment.