Subversion Repositories SmartDukaan

Rev

Rev 24593 | Rev 25840 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
22124 ashik.ali 1
package com.spice.profitmandi.common.util;
2
 
3
import java.io.File;
4
import java.io.IOException;
23951 amit.gupta 5
import java.io.PrintStream;
24809 amit.gupta 6
import java.io.Serializable;
23951 amit.gupta 7
import java.util.List;
22124 ashik.ali 8
 
23951 amit.gupta 9
import org.apache.commons.csv.CSVFormat;
10
import org.apache.commons.csv.CSVPrinter;
11
import org.apache.commons.io.output.ByteArrayOutputStream;
12
import org.apache.logging.log4j.LogManager;
13
import org.apache.logging.log4j.Logger;
22124 ashik.ali 14
import org.apache.tika.Tika;
15
 
16
import com.spice.profitmandi.common.enumuration.ContentType;
17
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
18
 
19
public class FileUtil {
23951 amit.gupta 20
 
21
	private static final Logger LOGGER = LogManager.getLogger(FileUtil.class);
22
 
23
	public static ContentType detectFileType(File file) throws ProfitMandiBusinessException {
22124 ashik.ali 24
		Tika tika = new Tika();
23951 amit.gupta 25
 
26
		// detecting the file type using detect method
27
		String filetype;
22124 ashik.ali 28
		try {
29
			filetype = tika.detect(file);
30
		} catch (IOException e) {
31
			e.printStackTrace();
23780 ashik.ali 32
			throw new ProfitMandiBusinessException("Content-Type", file.getName(), "DCMNT_1001");
22124 ashik.ali 33
		}
23951 amit.gupta 34
		if (filetype.contains("pdf")) {
35
			return ContentType.PDF;
36
		} else if (filetype.contains("jpg") | filetype.contains("jpeg")) {
37
			return ContentType.JPEG;
38
		} else if (filetype.contains("png")) {
39
			return ContentType.PNG;
40
		} else {
41
			throw new ProfitMandiBusinessException("Content-Type", file.getName(), "DCMNT_1002");
42
		}
22124 ashik.ali 43
	}
23951 amit.gupta 44
 
45
	public static void main(String[] args) {
46
		// detectFileType(new File("/hsps-docs/1499163441532"));
47
		// System.out.println("he");
22124 ashik.ali 48
	}
23951 amit.gupta 49
 
24809 amit.gupta 50
	public static ByteArrayOutputStream getCSVByteStream(List<String> headers, List<List<? extends Serializable>> rows) throws Exception {
23951 amit.gupta 51
		try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
52
				PrintStream writer = new PrintStream(baos);
53
				CSVPrinter csvPrinter = new CSVPrinter(writer,
54
						CSVFormat.EXCEL.withTrim().withHeader(headers.toArray(new String[] {})));) {
24809 amit.gupta 55
			for (List<? extends Serializable> row : rows) {
23951 amit.gupta 56
				csvPrinter.printRecord(row);
57
			}
58
			return baos;
59
		}
60
	}
22124 ashik.ali 61
}