Skip to content

Commit

Permalink
return new file name as response (#636)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukmzig authored Dec 11, 2024
1 parent 06358d2 commit 75dd36a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
22 changes: 14 additions & 8 deletions src/Asset/Controller/Upload/ReplaceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -89,26 +91,30 @@ public function __construct(
['file']
)]
#[DefaultResponses([
HttpResponseCodes::FORBIDDEN,
HttpResponseCodes::INTERNAL_SERVER_ERROR,
HttpResponseCodes::UNAUTHORIZED,
HttpResponseCodes::NOT_FOUND,
])]
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();
}
}
7 changes: 5 additions & 2 deletions src/Asset/Service/UploadService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/Asset/Service/UploadServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function replaceAssetBinary(
int $assetId,
UploadedFile $file,
UserInterface $user
): void;
): string;

/**
* @throws AccessDeniedException|EnvironmentException|ForbiddenException|NotFoundException
Expand Down
2 changes: 1 addition & 1 deletion translations/studio_api_docs.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <strong>{id}</strong> with the provided {file} binary. <br>
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. <br> You can create/update/delete list entries like metadata, custom settings and properties. <br> E.g. if you want to remove an entry from metadata simply do not include this entry in the update. <br> If you want to update only a single field, use the PATCH method.
Expand Down

0 comments on commit 75dd36a

Please sign in to comment.