AttachmentDataSource.java 3.34 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.apache.sling.api.request.RequestParameter
 *  org.apache.tika.config.TikaConfig
 *  org.apache.tika.detect.Detector
 *  org.apache.tika.io.TikaInputStream
 *  org.apache.tika.metadata.Metadata
 *  org.apache.tika.mime.MediaType
 *  org.apache.tika.mime.MimeType
 *  org.apache.tika.mime.MimeTypeException
 *  org.apache.tika.mime.MimeTypes
 */
package com.day.cq.wcm.foundation.forms.attachments;

import com.day.cq.wcm.foundation.forms.attachments.FileDataSource;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.sling.api.request.RequestParameter;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.detect.Detector;
import org.apache.tika.io.TikaInputStream;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.mime.MimeTypes;

public class AttachmentDataSource
implements FileDataSource {
    private final RequestParameter fileRequestParameter;

    public AttachmentDataSource(RequestParameter fileRequestParameter) {
        this.fileRequestParameter = fileRequestParameter;
    }

    @Override
    public String getContentType() {
        try {
            TikaConfig config = TikaConfig.getDefaultConfig();
            Detector detector = config.getDetector();
            TikaInputStream stream = TikaInputStream.get((InputStream)this.getInputStream());
            Metadata metadata = new Metadata();
            metadata.add("resourceName", this.getName());
            MediaType mediaType = detector.detect((InputStream)stream, metadata);
            return mediaType.getBaseType().toString();
        }
        catch (IOException e) {
            return this.getName();
        }
    }

    @Override
    public String getType() {
        try {
            TikaConfig config = TikaConfig.getDefaultConfig();
            Detector detector = config.getDetector();
            TikaInputStream stream = TikaInputStream.get((InputStream)this.getInputStream());
            Metadata metadata = new Metadata();
            metadata.add("resourceName", this.getName());
            MediaType mediaType = detector.detect((InputStream)stream, metadata);
            MimeType mimeType = config.getMimeRepository().forName(mediaType.toString());
            return mimeType.getExtension();
        }
        catch (IOException e) {
            return this.getName();
        }
        catch (MimeTypeException e) {
            return this.getName();
        }
    }

    @Override
    public String getTypeFromFileName() {
        String fileName = this.getName();
        int index = fileName.lastIndexOf(".");
        if (index > -1) {
            return fileName.substring(index);
        }
        return null;
    }

    @Override
    public long getSize() {
        return this.fileRequestParameter.getSize();
    }

    @Override
    public InputStream getInputStream() throws IOException {
        return this.fileRequestParameter.getInputStream();
    }

    @Override
    public String getName() {
        return this.fileRequestParameter.getFileName();
    }

    @Override
    public OutputStream getOutputStream() throws IOException {
        throw new UnsupportedOperationException("not implemented");
    }
}