
com.day.cq.dam.handler.standard.zip.ZipHandler Maven / Gradle / Ivy
/*
* Copyright 1997-2008 Day Management AG
* Barfuesserplatz 6, 4001 Basel, Switzerland
* All Rights Reserved.
*
* This software is the confidential and proprietary information of
* Day Management AG, ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Day.
*/
package com.day.cq.dam.handler.standard.zip;
import com.day.cq.dam.api.Asset;
import com.day.cq.dam.api.Rendition;
import com.day.cq.dam.api.metadata.ExtractedMetadata;
import com.day.cq.dam.commons.handler.AbstractAssetHandler;
import org.apache.commons.io.IOUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
/**
* The ZipHandler
class ...
*/
@Component(inherit = true, metatype = false)
@Service
public class ZipHandler extends AbstractAssetHandler {
/**
* the default logger
*/
private static final Logger log = LoggerFactory.getLogger(ZipHandler.class);
/**
* Mime type
*/
public static final String JAR_MIME_TYPE = "application/java-archive";
public static final String ZIP_MIME_TYPE = "application/zip";
public static final String[] MIME_TYPES = new String[]{JAR_MIME_TYPE, ZIP_MIME_TYPE};
/**
* Regular expression matching the thumbnail image file.
*/
private static final Pattern pattern = Pattern.compile("^thumbnail\\.(gif|jpg|jpeg|png)$", Pattern.CASE_INSENSITIVE);
public ExtractedMetadata extractMetadata(final Asset asset) {
ExtractedMetadata metadata = new ExtractedMetadata();
log.debug("extractMetadata: start extracting asset [{}]", asset.getPath());
// extract metadata
final InputStream is = asset.getOriginal().getStream();
final ZipInputStream zis = new ZipInputStream(is, Charset.forName("Cp437"));
final StringBuffer buffer = new StringBuffer();
int count = 0;
try {
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
if (!entry.isDirectory()) {
buffer.append(entry.getName()).append('\n');
count++;
}
// check if file name matches thumbnail regular expression
Matcher matcher = pattern.matcher(entry.getName());
if (matcher.find()) {
try {
// read image from stream
metadata.setMetaDataProperty("Thumbnail", ImageIO.read(zis));
} catch (Exception e) {
log.debug("Error reading zip thumbnail file {}", entry.getName());
}
}
zis.closeEntry();
}
metadata.setMetaDataProperty("Content", buffer.toString());
metadata.setMetaDataProperty("File Count", count);
} catch (IOException e) {
log.warn("extractMetadata: error while reading ZIP archive [{}]: ", asset.getPath(), e);
} catch (IllegalArgumentException iae) {
// bug#28534
log.warn("extractMetadata: error while reading ZIP entry [{}]: ", asset.getPath(), iae);
} finally {
IOUtils.closeQuietly(zis);
}
// Get XMP
execGenericProcessor(asset.getOriginal().getStream(), metadata);
setMimetype(metadata, asset);
return metadata;
}
public BufferedImage getImage(final Rendition rendition) throws IOException {
ExtractedMetadata metadata = extractMetadata(rendition.getAsset());
BufferedImage thumbnail = (BufferedImage) metadata.getMetaDataProperty("Thumbnail");
return thumbnail != null ? thumbnail : getZipIcon();
}
private BufferedImage getZipIcon() throws IOException {
InputStream stream = getClass().getClassLoader().getResourceAsStream(
"com/day/cq/dam/media/handler/zip/document_zip.png");
return ImageIO.read(stream);
}
public String[] getMimeTypes() {
return MIME_TYPES;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy