Subversion Repositories SmartDukaan

Rev

Rev 24593 | Rev 25840 | Go to most recent revision | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.common.util;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.List;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.tika.Tika;

import com.spice.profitmandi.common.enumuration.ContentType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;

public class FileUtil {

        private static final Logger LOGGER = LogManager.getLogger(FileUtil.class);

        public static ContentType detectFileType(File file) throws ProfitMandiBusinessException {
                Tika tika = new Tika();

                // detecting the file type using detect method
                String filetype;
                try {
                        filetype = tika.detect(file);
                } catch (IOException e) {
                        e.printStackTrace();
                        throw new ProfitMandiBusinessException("Content-Type", file.getName(), "DCMNT_1001");
                }
                if (filetype.contains("pdf")) {
                        return ContentType.PDF;
                } else if (filetype.contains("jpg") | filetype.contains("jpeg")) {
                        return ContentType.JPEG;
                } else if (filetype.contains("png")) {
                        return ContentType.PNG;
                } else {
                        throw new ProfitMandiBusinessException("Content-Type", file.getName(), "DCMNT_1002");
                }
        }

        public static void main(String[] args) {
                // detectFileType(new File("/hsps-docs/1499163441532"));
                // System.out.println("he");
        }

        public static ByteArrayOutputStream getCSVByteStream(List<String> headers, List<List<? extends Serializable>> rows) throws Exception {
                try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                PrintStream writer = new PrintStream(baos);
                                CSVPrinter csvPrinter = new CSVPrinter(writer,
                                                CSVFormat.EXCEL.withTrim().withHeader(headers.toArray(new String[] {})));) {
                        for (List<? extends Serializable> row : rows) {
                                csvPrinter.printRecord(row);
                        }
                        return baos;
                }
        }
}