AnsweredAssumed Answered

Create a transformer on share ZIP to PDF

Question asked by 4535992 on Mar 5, 2018
Latest reply on Mar 6, 2018 by 4535992

Hi , i have zipped a single pdf file and convert them to zip file e.g. test.pdf to test.zip a container of the original file.

What i want it's to extract the single pdf in the zip and send to share like a normal pdf on runtime while using share, so i can use the standard preview e standard thumbnail of the pdf file.

===============================================================

/src/main/resources/alfresco/extension/mimetype/custom-mimetype-map.xml

===============================================================

<alfresco-config area="mimetype-map">
  <config evaluator="string-compare" condition="Mimetype Map">
    <mimetypes>
      <mimetype mimetype="application/octet-stream" display="ZIP">
        <extension>zip</extension>
      </mimetype>
    </mimetypes>
  </config>
</alfresco-config>

=================================================================

/src/main/resources/alfresco/module/webdesktop-amp/alfresco-global.properties

=================================================================

.......

#TRANSFORMER

content.transformer.ZIPToPDF.priority=100
content.transformer.ZIPToPDF.extensions.zip.pdf.supported=true

................

=================================================================

/src/main/resources/alfresco/module/webdesktop-amp/mimetype-map-custom.xml

=================================================================

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

       <!-- IS EMPTY -->
 </beans>

=================================================================

/src/main/resources/alfresco/module/webdesktop-amp/module-context.xml

=================================================================

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
    <import resource="classpath:alfresco/module/${project.artifactId}/context/*-context.xml"/>
</beans>

=================================================================

/src/main/resources/alfresco/module/webdesktop-amp/context/transformer-context.xml

=================================================================

<?xml version='1.0' encoding='UTF-8'?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 
       <bean id="transformer.ZIPToPDF"
             class="it.abd.alfresco.transformers.ZIPToPDFTransformer"
             parent="baseContentTransformer" >                      
       </bean>
</beans>

=================================================================

/src/main/java/it/abd/alfresco/transformers/ZIPToPDFTransformer.java

=================================================================

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;

 

import org.alfresco.repo.content.transform.AbstractContentTransformer2;
import org.alfresco.repo.content.transform.UnsupportedTransformationException;
import org.alfresco.service.cmr.repository.ContentReader;
import org.alfresco.service.cmr.repository.ContentWriter;
import org.alfresco.service.cmr.repository.TransformationOptions;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

 

import it.abd.sign.lib.SignAlfrescoLib;

 

public class ZIPToPDFTransformer extends AbstractContentTransformer2
    {
    private static final Log logger = LogFactory.getLog(ZIPToPDFTransformer.class);
    
    
    /**
     * Select only the file to transform
     */
    public boolean isTransformable(String sourceMimetype, String targetMimetype, TransformationOptions options)
    {
        String ext = getExtensionOrAny("application/zip");    
        if ("application/zip".equalsIgnoreCase(sourceMimetype) || "application/octet-stream".equalsIgnoreCase(sourceMimetype) || ext.equalsIgnoreCase("zip")){
             logger.debug("ZIPToPDFTransformer IsTranformable '"+ sourceMimetype + "' to '" + targetMimetype+"'");
             return true;      
        }else{
             return false;
        }    
    }
        
    /**
     * Transofmer a zip to the pdf single file in it
     */
    @Override
    protected void transformInternal(ContentReader contentReader, ContentWriter contentWriter, TransformationOptions options) throws Exception
    {
        logger.debug("USE ZIPToPDFTransformer");
        BufferedReader reader = null;    
        Writer out = null;
        InputStream inStream = null;
        InputStream is = null;
        try
        {                   
            // Check the transformability
            String myMimeType = myStrictMimetypeCheck(contentReader, contentWriter, options);
            if(myMimeType !=null && !mimeType.isEmpty())contentWriter.setMimetype(myMimeType);
            //The zip file on share
            inStream = contentReader.getContentInputStream();
            out = new BufferedWriter(new OutputStreamWriter(contentWriter.getContentOutputStream()));
            //Extract Document pdf under the zip file (ignore these line they are work just fine)
            byte[] originalFile = SignAlfrescoLib.getOriginalDocumentBinaries(inStream);
            logger.debug("OriginalFile: " + String.valueOf(originalFile!=null && originalFile.length >0));
            is = new BufferedInputStream(SignAlfrescoLib.getOriginalDocumentStream(inStream));
            //Try to guess the mimetype of the document (ignore these line they are work just fine)
            String guessMimeType = URLConnection.guessContentTypeFromStream(is);
            logger.debug("GuessMimetype: " + guessMimeType);
            if(guessMimeType!=null){
                 contentWriter.setMimetype(guessMimeType);//this is "application/pdf"
            }
            //Copy the pdf file extract from zip file on the outputstream with a mimetype "application/pdf"
            //IOUtils.write(originalFile, outStream);
            IOUtils.write(originalFile, out,StandardCharsets.UTF_8.displayName());
            //outStream.flush();            
        }catch(IOException e){
            e.printStackTrace();
        }
        finally
        {
            if (reader != null)
            {
                try{reader.close(); }
                catch (Throwable e){e.printStackTrace();}
            }
            if (out != null) {
                try{out.close();}
                catch (Throwable e){e.printStackTrace();}
            }
            if(inStream != null){
                try{inStream.close();}
                catch (Throwable e){e.printStackTrace();}
            }
            if(is != null){
                try{is.close();}
                catch (Throwable e){e.printStackTrace();}
            }
        }
    }

 

    /**
     * Manage the inner warnings of alfresco 5.2
     * @param reader the reader of the current document
     * @param writer the writer of the current document
     * @param options the transform options of the current document
     * @return the mimetype suggested from share
     * @throws UnsupportedTransformationException
     */
    private String myStrictMimetypeCheck(ContentReader reader,ContentWriter writer, TransformationOptions options)throws UnsupportedTransformationException
    {
        String mimeTypeOfAlfresco = "";
        
        String sourceMimetype = reader.getMimetype();
        String targetMimetype = writer.getMimetype();     
        String differentType = getMimetypeService().getMimetypeIfNotMatches(reader.getReader());

 

        if (!transformerConfig.strictMimetypeCheck(sourceMimetype, differentType))
        {
            String fileName = transformerDebug.getFileName(options, true, 0);
            String readerSourceMimetype = reader.getMimetype();
            String message = "Transformation of ("+fileName+
                ") has not taken place because the declared mimetype ("+
                readerSourceMimetype+") does not match the detected mimetype ("+
                differentType+").";
            logger.warn(message);
            logger.debug("Cambiamo il mimetype con quello voluto da alfresco per evitare stackstrace del warning");
            mimeTypeOfAlfresco = differentType;
        }else{
            logger.debug("Mimetype ok : mimeTypeOfAlfresco = "+mimeTypeOfAlfresco+ " , sourceMimetype = "+sourceMimetype);
            mimeTypeOfAlfresco = sourceMimetype;
        }  
        return mimeTypeOfAlfresco;
    }
    
    
}

========================================================================================

The Alfresco version i use , is the 5.2 community.

 

Here the screenshot for make visualize the difference between a pdf file and a zip file on share:

I just want to retrieve the preview of the single document pdf in the zip file instead the fake preview of the zip file.

 

But i must do something wrong with the configuration of the transformer .

 

Hope i have made clear what i'm trying to do.

Outcomes