Search in sources :

Example 1 with DownloadResource

use of com.day.cq.commons.DownloadResource in project aem-core-wcm-components by Adobe-Marketing-Cloud.

the class DownloadImpl method initAssetDownload.

private void initAssetDownload(String fileReference) {
    Resource downloadResource = resourceResolver.getResource(fileReference);
    if (downloadResource != null) {
        Asset downloadAsset = downloadResource.adaptTo(Asset.class);
        if (downloadAsset != null) {
            Calendar resourceLastModified = properties.get(JcrConstants.JCR_LASTMODIFIED, Calendar.class);
            if (resourceLastModified != null) {
                lastModified = resourceLastModified.getTimeInMillis();
            }
            long assetLastModified = downloadAsset.getLastModified();
            if (assetLastModified > lastModified) {
                lastModified = assetLastModified;
            }
            filename = downloadAsset.getName();
            format = downloadAsset.getMetadataValue(DamConstants.DC_FORMAT);
            if (StringUtils.isNoneEmpty(format)) {
                extension = mimeTypeService.getExtension(format);
            }
            if (StringUtils.isEmpty(extension)) {
                extension = FilenameUtils.getExtension(filename);
            }
            url = linkHandler.getLink(getDownloadUrl(downloadResource), null).map(Link::getURL).orElse(null);
            if (titleFromAsset) {
                title = downloadAsset.getMetadataValue(DamConstants.DC_TITLE);
            }
            if (descriptionFromAsset) {
                String assetDescription = downloadAsset.getMetadataValue(DamConstants.DC_DESCRIPTION);
                if (StringUtils.isNotBlank(assetDescription)) {
                    description = assetDescription;
                }
            }
            long rawFileSize;
            Object rawFileSizeObject = downloadAsset.getMetadata(DamConstants.DAM_SIZE);
            if (rawFileSizeObject != null) {
                rawFileSize = (Long) rawFileSizeObject;
            } else {
                rawFileSize = downloadAsset.getOriginal().getSize();
            }
            size = FileUtils.byteCountToDisplaySize(rawFileSize);
        }
    }
}
Also used : Calendar(java.util.Calendar) DownloadResource(com.day.cq.commons.DownloadResource) Resource(org.apache.sling.api.resource.Resource) Asset(com.day.cq.dam.api.Asset) SlingObject(org.apache.sling.models.annotations.injectorspecific.SlingObject) Link(com.adobe.cq.wcm.core.components.commons.link.Link)

Aggregations

Link (com.adobe.cq.wcm.core.components.commons.link.Link)1 DownloadResource (com.day.cq.commons.DownloadResource)1 Asset (com.day.cq.dam.api.Asset)1 Calendar (java.util.Calendar)1 Resource (org.apache.sling.api.resource.Resource)1 SlingObject (org.apache.sling.models.annotations.injectorspecific.SlingObject)1