diff --git a/src/Asset/Controller/Upload/ReplaceController.php b/src/Asset/Controller/Upload/ReplaceController.php
index 07122dc5..5445ae91 100644
--- a/src/Asset/Controller/Upload/ReplaceController.php
+++ b/src/Asset/Controller/Upload/ReplaceController.php
@@ -29,6 +29,7 @@
use Pimcore\Bundle\StudioBackendBundle\Exception\Api\UserNotFoundException;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Parameter\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Request\MultipartFormDataRequestBody;
+use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\Content\DataJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\DefaultResponses;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attribute\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
@@ -37,8 +38,8 @@
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\HttpResponseCodes;
use Pimcore\Bundle\StudioBackendBundle\Util\Constant\UserPermissions;
use Symfony\Component\HttpFoundation\File\UploadedFile;
+use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
-use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Component\Serializer\SerializerInterface;
@@ -75,6 +76,7 @@ public function __construct(
)]
#[SuccessResponse(
description: 'asset_replace_success_response',
+ content: new DataJson('new file name of the asset', 'image.jpg')
)]
#[IdParameter(type: ElementTypes::TYPE_ASSET)]
#[MultipartFormDataRequestBody(
@@ -89,6 +91,8 @@ public function __construct(
['file']
)]
#[DefaultResponses([
+ HttpResponseCodes::FORBIDDEN,
+ HttpResponseCodes::INTERNAL_SERVER_ERROR,
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
@@ -96,19 +100,21 @@ public function replaceAsset(
int $id,
// TODO: Symfony 7.1 change to https://symfony.com/blog/new-in-symfony-7-1-mapuploadedfile-attribute
Request $request
- ): Response {
+ ): JsonResponse {
$file = $request->files->get('file');
if (!$file instanceof UploadedFile) {
throw new ElementStreamResourceNotFoundException(0, 'File');
}
- $this->uploadService->replaceAssetBinary(
- $id,
- $file,
- $this->securityService->getCurrentUser()
+ return new JsonResponse(
+ [
+ 'data' => $this->uploadService->replaceAssetBinary(
+ $id,
+ $file,
+ $this->securityService->getCurrentUser()
+ ),
+ ]
);
-
- return new Response();
}
}
diff --git a/src/Asset/Service/UploadService.php b/src/Asset/Service/UploadService.php
index 543f2e2e..f59a9323 100644
--- a/src/Asset/Service/UploadService.php
+++ b/src/Asset/Service/UploadService.php
@@ -173,7 +173,7 @@ public function replaceAssetBinary(
int $assetId,
UploadedFile $file,
UserInterface $user
- ): void {
+ ): string {
$asset = $this->assetService->getAssetElement($user, $assetId);
if (!$asset->isAllowed(ElementPermissions::PUBLISH_PERMISSION)) {
throw new ForbiddenException(
@@ -189,14 +189,17 @@ public function replaceAssetBinary(
$this->validateMimeType($file, $fileName, $asset->getType());
try {
+ $newFileName = $this->getUpdatedFileName($asset->getFilename(), $fileName, $asset->getParent());
$asset->setStream(fopen($sourcePath, 'rb'));
$asset->setCustomSetting('thumbnails', null);
if (method_exists($asset, 'getEmbeddedMetaData')) {
$asset->getEmbeddedMetaData(true);
}
$asset->setUserModification($user->getId());
- $asset->setFilename($this->getUpdatedFileName($asset->getFilename(), $fileName, $asset->getParent()));
+ $asset->setFilename($newFileName);
$asset->save();
+
+ return $newFileName;
} catch (Exception $e) {
throw new DatabaseException($e->getMessage());
} finally {
diff --git a/src/Asset/Service/UploadServiceInterface.php b/src/Asset/Service/UploadServiceInterface.php
index b7bbc442..d5cec8e8 100644
--- a/src/Asset/Service/UploadServiceInterface.php
+++ b/src/Asset/Service/UploadServiceInterface.php
@@ -82,7 +82,7 @@ public function replaceAssetBinary(
int $assetId,
UploadedFile $file,
UserInterface $user
- ): void;
+ ): string;
/**
* @throws AccessDeniedException|EnvironmentException|ForbiddenException|NotFoundException
diff --git a/translations/studio_api_docs.en.yaml b/translations/studio_api_docs.en.yaml
index f99fc361..e33e547d 100644
--- a/translations/studio_api_docs.en.yaml
+++ b/translations/studio_api_docs.en.yaml
@@ -142,7 +142,7 @@ asset_patch_folder_by_id_summary: Patch all assets based on folder ID and filter
asset_replace_description: |
Replace the existing asset binary based on the given {id} with the provided {file} binary.
The new asset binary mime type must be the same as the existing asset binary mime type.
-asset_replace_success_response: Successfully replaced asset binary
+asset_replace_success_response: File name of the successfully replaced asset
asset_replace_summary: Replace existing asset binary
asset_update_by_id_description: |
Update needs to have the complete data present.
You can create/update/delete list entries like metadata, custom settings and properties.
E.g. if you want to remove an entry from metadata simply do not include this entry in the update.
If you want to update only a single field, use the PATCH method.