Subversion Repositories SmartDukaan

Rev

Rev 23780 | Rev 24593 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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