Upload Asset preserving the Asset metadata | Community
Skip to main content
Level 6
June 28, 2023
Solved

Upload Asset preserving the Asset metadata

  • June 28, 2023
  • 2 replies
  • 2729 views

I would like to Upload Assets in DAM while preserving the Asset Metadata.

I found there is a replace method in fileUpload.js under coral.components.commons.fileupload.clientlibs but this it deletes the assets and uploads the new and therefore the previous metadata are lost.

Any ideas how to achieve this?

 

replace: function(duplicateDialog, damfileupload, duplicates) {
var applyAllCheckbox = duplicateDialog.getElementsByTagName("coral-checkbox")[0];
if ((applyAllCheckbox && applyAllCheckbox.checked) || duplicates.length === 1) {
this._addReplaceAssetParam(damfileupload.fileUpload, duplicates);
damfileupload._continueUpload(damfileupload.fileUpload);
} else {
this._addReplaceAssetParam(damfileupload.fileUpload, [ duplicates[0] ]);
duplicates.splice(0, 1);
damfileupload._showDuplicates(duplicates);
}
},

rep 

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by Tanika02

Hello @anasustic - 

 

Here's a general approach to achieve this:

  1. Create a custom client library that overrides the fileUpload.js file. You can create a new client library under /apps or /etc/clientlibs.

  2. Copy the fileUpload.js file from coral.components.commons.fileupload.clientlibs to your custom client library.

  3. Modify the replace method in your custom fileUpload.js to handle the asset replacement without deleting the metadata.

 

replace: function(duplicateDialog, damfileupload, duplicates) { var applyAllCheckbox = duplicateDialog.getElementsByTagName("coral-checkbox")[0]; if ((applyAllCheckbox && applyAllCheckbox.checked) || duplicates.length === 1) { this._preserveMetadataAndReplaceAsset(damfileupload.fileUpload, duplicates); damfileupload._continueUpload(damfileupload.fileUpload); } else { this._preserveMetadataAndReplaceAsset(damfileupload.fileUpload, [ duplicates[0] ]); duplicates.splice(0, 1); damfileupload._showDuplicates(duplicates); } }, _preserveMetadataAndReplaceAsset: function(fileUpload, duplicates) { // Perform your custom logic here to preserve the metadata of the asset. // You can retrieve the metadata of the existing asset and apply it to the new asset being uploaded. // Example code to preserve the metadata: for (var i = 0; i < duplicates.length; i++) { var existingAssetPath = duplicates[i].path; var existingAssetMetadata = this._retrieveAssetMetadata(existingAssetPath); // Apply the existing asset metadata to the new asset being uploaded this._applyMetadataToNewAsset(fileUpload, existingAssetMetadata); } // Add the necessary parameters or make the necessary API calls to handle the asset replacement. }, _retrieveAssetMetadata: function(assetPath) { // Implement your logic to retrieve the metadata of the existing asset based on the asset path. // You can use AEM's APIs or services to fetch the metadata. // Return the metadata object. }, _applyMetadataToNewAsset: function(fileUpload, metadata) { // Implement your logic to apply the existing asset metadata to the new asset being uploaded. // You can set the necessary metadata fields in the fileUpload object or make API calls to apply the metadata. // Make sure to preserve the metadata fields and values when uploading the new asset. },

 

 

Include your custom client library in your project's client libraries configuration and ensure that it overrides the default fileUpload.js from coral.components.commons.fileupload.clientlibs.

 

OR

 

There is an alternative approach to achieve the requirement of preserving asset metadata when replacing files in AEM DAM. Instead of customizing the asset replacement process, you can leverage AEM's built-in versioning feature.

 

Here's how you can use versioning to preserve asset metadata:

  1. Enable versioning for the DAM asset folder: In AEM, navigate to the folder where the assets are stored and enable versioning for that folder. This can be done by accessing the folder properties and enabling the "Versionable" option.

  2. Upload a new version of the asset: Instead of replacing the existing asset file directly, upload the new version of the file as a new asset. AEM will automatically create a new version of the asset, preserving the metadata of the previous versions.

  3. Retrieve the desired version of the asset: When downloading or accessing the asset, you can specify the desired version to retrieve the appropriate file with its associated metadata. AEM provides APIs and functionalities to retrieve specific versions of assets.

Using versioning has the advantage of preserving the complete history of asset versions, including their metadata. It allows you to access previous versions of assets if needed, while still providing the latest version as the default for downloads and access.

 

This approach avoids the need for customizing the asset replacement process and ensures that metadata is maintained for each version of the asset. It leverages the capabilities provided by AEM's built-in versioning functionality.

2 replies

Tanika02
Tanika02Accepted solution
Level 7
June 28, 2023

Hello @anasustic - 

 

Here's a general approach to achieve this:

  1. Create a custom client library that overrides the fileUpload.js file. You can create a new client library under /apps or /etc/clientlibs.

  2. Copy the fileUpload.js file from coral.components.commons.fileupload.clientlibs to your custom client library.

  3. Modify the replace method in your custom fileUpload.js to handle the asset replacement without deleting the metadata.

 

replace: function(duplicateDialog, damfileupload, duplicates) { var applyAllCheckbox = duplicateDialog.getElementsByTagName("coral-checkbox")[0]; if ((applyAllCheckbox && applyAllCheckbox.checked) || duplicates.length === 1) { this._preserveMetadataAndReplaceAsset(damfileupload.fileUpload, duplicates); damfileupload._continueUpload(damfileupload.fileUpload); } else { this._preserveMetadataAndReplaceAsset(damfileupload.fileUpload, [ duplicates[0] ]); duplicates.splice(0, 1); damfileupload._showDuplicates(duplicates); } }, _preserveMetadataAndReplaceAsset: function(fileUpload, duplicates) { // Perform your custom logic here to preserve the metadata of the asset. // You can retrieve the metadata of the existing asset and apply it to the new asset being uploaded. // Example code to preserve the metadata: for (var i = 0; i < duplicates.length; i++) { var existingAssetPath = duplicates[i].path; var existingAssetMetadata = this._retrieveAssetMetadata(existingAssetPath); // Apply the existing asset metadata to the new asset being uploaded this._applyMetadataToNewAsset(fileUpload, existingAssetMetadata); } // Add the necessary parameters or make the necessary API calls to handle the asset replacement. }, _retrieveAssetMetadata: function(assetPath) { // Implement your logic to retrieve the metadata of the existing asset based on the asset path. // You can use AEM's APIs or services to fetch the metadata. // Return the metadata object. }, _applyMetadataToNewAsset: function(fileUpload, metadata) { // Implement your logic to apply the existing asset metadata to the new asset being uploaded. // You can set the necessary metadata fields in the fileUpload object or make API calls to apply the metadata. // Make sure to preserve the metadata fields and values when uploading the new asset. },

 

 

Include your custom client library in your project's client libraries configuration and ensure that it overrides the default fileUpload.js from coral.components.commons.fileupload.clientlibs.

 

OR

 

There is an alternative approach to achieve the requirement of preserving asset metadata when replacing files in AEM DAM. Instead of customizing the asset replacement process, you can leverage AEM's built-in versioning feature.

 

Here's how you can use versioning to preserve asset metadata:

  1. Enable versioning for the DAM asset folder: In AEM, navigate to the folder where the assets are stored and enable versioning for that folder. This can be done by accessing the folder properties and enabling the "Versionable" option.

  2. Upload a new version of the asset: Instead of replacing the existing asset file directly, upload the new version of the file as a new asset. AEM will automatically create a new version of the asset, preserving the metadata of the previous versions.

  3. Retrieve the desired version of the asset: When downloading or accessing the asset, you can specify the desired version to retrieve the appropriate file with its associated metadata. AEM provides APIs and functionalities to retrieve specific versions of assets.

Using versioning has the advantage of preserving the complete history of asset versions, including their metadata. It allows you to access previous versions of assets if needed, while still providing the latest version as the default for downloads and access.

 

This approach avoids the need for customizing the asset replacement process and ensures that metadata is maintained for each version of the asset. It leverages the capabilities provided by AEM's built-in versioning functionality.

anasusticAuthor
Level 6
June 30, 2023

Thank you very much @tanika02 
I was looking for versioning in Asset Folder Properties and cannot find it. I am at AEM 6.5.15

 

aanchal-sikka
Community Advisor
Community Advisor
June 28, 2023

Hello @anasustic 

 

If we use "Create Version" instead of "Replace", it should retain the metadata. 

Incase of "Create Version" we would only be replacing Binary file

Aanchal Sikka
anasusticAuthor
Level 6
June 28, 2023

Thanks @aanchal-sikka and @tanika02  creating a new version is rather not desirable, therefore I need to replace the Asset while preserving metadata of the existing Asset if it exists.

aanchal-sikka
Community Advisor
Community Advisor
June 28, 2023

@anasustic 

I would suggest to use workflows to achieve this requirement. The process should be handled in a clean and a transactional manner.

 

1. Rename existing asset

2. Upload the new asset

3. Copy shortlisted properties from old to new asset

4. If save is success, Let an author review the new asset (if desirable)

5. If all is good, delete the old asset.

 

With workflow, you will have better clarity and avoid loosing any information

Aanchal Sikka