Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

AEM 6580 - Assets Download Version from Timeline | AEM Community Blog Seeding

Avatar

Administrator

BlogImage.jpg

AEM 6580 - Assets Download Version from Timeline by Sreekanth Choudry Nalabotu

Abstract

Goal
AEM Assets provides version preview in the timeline, this post extends it to download specific asset version..

Not tested for performance, please test and improve for big asset version downloads....

Solution
1) Create a servlet apps.experienceaem.assets.AssetVersionDownloadServlet to download the specific version...

package apps.experienceaem.assets;

import opennlp.tools.util.StringUtil;
import org.apache.commons.io.IOUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.mime.MimeTypeService;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.Node;
import javax.servlet.Servlet;
import java.io.InputStream;
import java.io.OutputStream;

@Component(
service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "= Experience AEM Download Asset Version Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET,
"sling.servlet.paths=" + "/bin/eaem/downloadVersion"
}
)
public class AssetVersionDownloadServlet extends SlingAllMethodsServlet {
private static final Logger log = LoggerFactory.getLogger(AssetVersionDownloadServlet.class);

@Reference
transient MimeTypeService mimeTypeService;

protected void doGet(final SlingHttpServletRequest request, final SlingHttpServletResponse response) {
ResourceResolver resolver = request.getResourceResolver();
String resPath = request.getParameter("resource");

Resource assetPathResource = resolver.getResource(resPath + "/jcr:frozenNode/jcr:content/renditions/original/jcr:content");

if(assetPathResource == null){
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}

Node fileNode = assetPathResource.adaptTo(Node.class);
Resource jcrContent = resolver.getResource(resPath + "/jcr:frozenNode/jcr:content");
String mimeType = getMIMEType(jcrContent);

InputStream is = null;

try{
is = fileNode.getProperty("jcr:data").getBinary().getStream();

String fileName = jcrContent.getValueMap().get("cq:name", "");

if(StringUtil.isEmpty(fileName)){
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}

response.setContentType(mimeType);
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

final OutputStream out = response.getOutputStream();

IOUtils.copy(is, out);

out.close();

is.close();
}catch(Exception e){
log.error("Error retrieving the version for " + resPath, e);
response.setStatus(SlingHttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}

private String getMIMEType(Resource resource){
String cqName = resource.getValueMap().get("cq:name", "");
return mimeTypeService.getMimeType(cqName.substring(cqName.indexOf('.') + 1));
}
}


2) Create a clientlib /apps/eaem-assets-download-version/clientlib with categories=cq.gui.coral.common.admin.timeline to add the Download Version button and necessary download logic...

Read Full Blog

AEM 6580 - Assets Download Version from Timeline

Q&A

Please use this thread to ask the related questions.



Kautuk Sahni
Topics

Topics help categorize Community content and increase your ability to discover relevant content.

0 Replies