Expand my Community achievements bar.

SOLVED

How to download the file in the CQ5

Avatar

Former Community Member

We need to download the all extension files like pdf | txt | word |html and son on but except image in the CQ5, then to translate these files in localhost. At last upload these files back to CQ5.

The question is how to know the files list path and how to get the files stored in the CQ5 to localhost or a specific server and how to upload the file to CQ5?

 

Awaiting for anyone suggestion, thanks in advance!  

1 Accepted Solution

Avatar

Correct answer by
Level 10

To download files, (for example, DAM Assets), you use the QueryBuilder API. Here is a community article that talks about how to build a custom Sling Servlet that downloads DAM Assets. In this use case - the collection of DAM Assets retrieved using the QueryBuilder API is placed in a ZIP file and downloaded, as shown in this pic:

[img]downloadClient.png[/img]

To read this development article, see: http://helpx.adobe.com/experience-manager/using/downloading-dam-assets.html.

View solution in original post

13 Replies

Avatar

Community Advisor

Hi York,

More information on your use case would be nice,

(assume that you want to store files in DAM)

Here's some general code that will fulfill your task:

/**      * Downloads file from internet and places it in DAM      * @param url - the url of the file we are interested in      * @param fileName - the filename of the file we are interested to store      */     public void downloadAndStore(String url, String fileName) {         try {             final URL file = new URL(url);             final ReadableByteChannel readableByteChannel = Channels.newChannel(file.openStream());             final FileOutputStream fos = new FileOutputStream("temp"); // ToDo make sure to have a unique name in case of concurrency             fos.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);             final ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);             final InputStream is = ..;             IOUtils.copy(is, fos);             //Use AssetManager to place the file into the AEM DAM             final AssetManager assetMgr = resourceResolver.adaptTo(AssetManager.class);             final String fileToStore = "/content/dam/" + fileName;             assetMgr.createAsset(fileToStore, fos, "image/jpeg", true);             // ToDo delete temp file         } catch (IOException e) {             //ToDo log at appropriate level         }     }

Kindest Regards,

Peter

Avatar

Level 10

Another way to download a DAM asset from CQ to the desktop is write a custom Sling Servlet and in the GET method search for the file using CQ APIS ( ie - QueryBuilder) - and then write logic in the Slign Servlet to download the file:

public class DownloadFileServlet extends org.apache.sling.api.servlets.SlingAllMethodsServlet{

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // reads input file from an absolute path
        //USE QUERY BUILDER TO GET FILE";
        FileInputStream inStream = new FileInputStream(downloadFile);
         
        // if you want to use a relative path to context root:
        String relativePath = getServletContext().getRealPath("");
        System.out.println("relativePath = " + relativePath);
         
        // obtains ServletContext
        ServletContext context = getServletContext();
         
        // gets MIME type of the file
        String mimeType = context.getMimeType(filePath);
        if (mimeType == null) {        
            // set to binary type if MIME mapping not found
            mimeType = "application/octet-stream";
        }
        System.out.println("MIME type: " + mimeType);
         
        // modifies response
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());
         
        // forces download
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
        response.setHeader(headerKey, headerValue);
         
        // obtains response's output stream
        OutputStream outStream = response.getOutputStream();
         
        byte[] buffer = new byte[4096];
        int bytesRead = -1;
         
        while ((bytesRead = inStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }
         
        inStream.close();
        outStream.close();     
    }
}

 

Avatar

Level 10

Hi York Jin,

   Typically all assets are stored in dam and referenced in pages. Though there is exception you can store at page level. You can write an jcr query to get assets of type you need and iterate over it to get files.

Thanks,

Sham

Avatar

Correct answer by
Level 10

To download files, (for example, DAM Assets), you use the QueryBuilder API. Here is a community article that talks about how to build a custom Sling Servlet that downloads DAM Assets. In this use case - the collection of DAM Assets retrieved using the QueryBuilder API is placed in a ZIP file and downloaded, as shown in this pic:

[img]downloadClient.png[/img]

To read this development article, see: http://helpx.adobe.com/experience-manager/using/downloading-dam-assets.html.

Avatar

Former Community Member

hi sham,

now i can print these assets information contain NodeType&NodeName&NodePath and so on. But i want to know how to use JCR query API to download files? Could you possibly give a sample code snippet?

thank a lot.

Avatar

Former Community Member

Hi Peter,

Many thanks for your answer. But i wondering how to get the file list path  since i want to download all files except image.

Looking forward to your reply.

 

thanks  a lot.

Avatar

Former Community Member

Avatar

Level 10

YorkJin wrote...

hi sham,

now i can print these assets information contain NodeType&NodeName&NodePath and so on. But i want to know how to use JCR query API to download files? Could you possibly give a sample code snippet?

thank a lot.

 

You can use asset download service  http://dev.day.com/docs/en/aem/6-0/develop/ref/javadoc/com/day/cq/dam/api/jobs/AssetDownloadService....

Avatar

Former Community Member

Hi Peter,

Actually I am working on a connector between AEM and a TMS(GlobalSight).I am intrested in the downloadable files(real file nodes) in "Digital Assets" of AEM.I need pull these file/folder structure outside AEM to form a tree, then user can decide which files to translate in GlobalSight. After translated, user will push the translated file back into a selected folder in "Digital Assets". This is what we are expecting to archive.

I am wondering if AEM exposes any webservice APIs (SOAP or REST way, methods like "login", "getFiles", "uploadFiles" etc.) for such case? If possible,I don't intend to do any development work that run in AEM as a bundle, this is because AEM servers are not in our control. Deploying our own bundle in client AEM servers is an extra work. All development should be in GlobalSight(login, get files from AEM, push file into AEM).

As I am new to AEM, even Sling etc, maybe you can offer a constructive suggestion about my business?

Thanks a lot,

York

Avatar

Level 10

"I am wondering if AEM exposes any webservice APIs (SOAP or REST way)"

You can write a custom Sling Servlet and then invoke that Sling Service using a Restful request. I wrote an article that shows this concept. However, instead of pulling files from AEM, I uploaded files using a Rest Request - the target was client libs.

See this article. It will give you an idea:

http://helpx.adobe.com/experience-manager/using/post_files.html


 

Avatar

Former Community Member

Thanks you so much, again: API "AssetDownloadService"  is this a webservice?  If false,  developers how to instance it?   

Avatar

Level 10

AssetDownloadService is a service. one of the way to get an instance using @reference

@Reference

AssetDownloadService  assetDownloadService;

and then you can use this variables to access its methods.