Subversion Repositories SmartDukaan

Rev

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