View Javadoc
1   /**
2    * 
3    */
4   package com.buckosoft.PicMan.business.util;
5   
6   import java.awt.image.BufferedImage;
7   import java.io.ByteArrayOutputStream;
8   import java.util.Date;
9   import java.util.Iterator;
10  import java.util.List;
11  
12  import javax.imageio.ImageIO;
13  import javax.imageio.ImageWriter;
14  import javax.imageio.stream.ImageOutputStream;
15  
16  import org.apache.commons.logging.Log;
17  import org.apache.commons.logging.LogFactory;
18  
19  import com.buckosoft.PicMan.business.PicManFacade;
20  import com.buckosoft.PicMan.domain.JobLogEntry;
21  import com.buckosoft.PicMan.domain.Pic;
22  import com.buckosoft.PicMan.image.PicReader;
23  
24  /**
25   * @author dick
26   *
27   */
28  public class MD5SumUpdater {
29  	protected final static Log log = LogFactory.getLog(MD5SumUpdater.class);
30  
31  	private	int picProcessing = 0;
32  	private	PicManFacade	pmf;
33  
34  	/** Set the reference to the PicMan API.
35  	 * @param pmf The PicManFacade
36  	 */
37  	public	void setPicMan(PicManFacade pmf) {
38  		this.pmf = pmf;
39  	}
40  
41  	/** Which ordinal picture are we working on?
42  	 * This is just a status indicator
43  	 * @return the picProcessing
44  	 */
45  	public int getPicProcessing() {
46  		return picProcessing;
47  	}
48  
49  	/** Entry point from BatchManager to run the job
50  	 */
51  	public void run() {
52  		List<Pic> pics = pmf.getDB().getPics();
53  		Iterator<Pic> iter = pics.iterator();
54  		int	picCount = pics.size();
55  		picProcessing = 0;
56  		JobLogEntry jle = new JobLogEntry();
57  		jle.setType(JobLogEntry.MD5SUM);
58  		pmf.addJobToLog(jle);
59  		Pic pic;
60  		while (iter.hasNext()) {
61  			picProcessing++;
62  			int cp = picProcessing * 100 / picCount;
63  			jle.setName("" + cp + "%");
64  			pic = iter.next();
65  			calculateMD5Sum(pmf, pic);
66  			//log.info("md5sum for " + pic.getName() + " = " + pic.getMd5());
67  			pmf.getDB().updatePic(pic);
68  		}
69  		jle.setEndTime(new Date());
70  	}
71  	
72  	public static void calculateMD5Sum(PicManFacade pmf, Pic pic) {
73  	
74  		//FileInputStream fis;
75  		String md5;
76  		//String fullPath = null;
77  		try {
78  			BufferedImage bi = pmf.getPicReader().readPic(pic);
79  			ByteArrayOutputStream out = new ByteArrayOutputStream();
80  			ImageOutputStream ios = ImageIO.createImageOutputStream(out);
81  			Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName("jpg");
82  			ImageWriter writer = (ImageWriter)writers.next();
83  			writer.setOutput(ios);
84  			writer.write(bi);
85  			byte[] data = out.toByteArray();
86  			md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(data);
87  			
88  			//fullPath = PicReader.getFullPath(pmf, pic);
89  //			fis = new FileInputStream(new File(fullPath));
90  //			md5 = org.apache.commons.codec.digest.DigestUtils.md5Hex(fis);
91  //			fis.close();
92  		} catch (Exception e) {
93  			// TODO Auto-generated catch block
94  			//e.printStackTrace();
95  			log.warn("md5 file error on " + PicReader.getFullPath(pmf, pic));
96  			pic.setMd5(0);
97  			return;
98  		}
99  		long l = Long.parseLong(md5.substring(0, 8), 16);
100 		pic.setMd5(l);
101 		
102 		//byte[] m = org.apache.commons.codec.digest.DigestUtils.md5(fis);
103 		//long l = m[0] + m[1]<<8 + m[2]<<16 + m[3]<<24 + m[4]<<32 + m[5]<<40 + m[6]<<48 + m[7]<<56;
104 /*		MessageDigest md = null;
105 		try {
106 			md = MessageDigest.getInstance("MD5");
107 		} catch (NoSuchAlgorithmException e) {
108 			log.error(e);
109 			return;
110 		}
111 		InputStream is = null;
112 		try {
113 			is = Files.newInputStream(Paths.get("file.txt"));
114 		} catch (IOException e) {
115 			log.error(e);
116 			return;
117 		}
118 		DigestInputStream dis = new DigestInputStream(is, md);
119 		 Read stream to EOF as normal... 
120 
121 		byte[] digest = md.digest();		
122 */	}
123 }