KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > actions > area > customizer > Pictures


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 package net.killingar.forum.actions.area.customizer;
27
28 import net.killingar.GenerateThumbnails;
29 import net.killingar.Utils;
30 import net.killingar.StringUtils;
31 import net.killingar.forum.actions.area.EditMessage;
32 import net.killingar.forum.internal.FieldData;
33
34 import java.io.*;
35 import java.net.URL JavaDoc;
36 import java.net.URLConnection JavaDoc;
37
38 public class Pictures extends Links
39 {
40   // Private --------------------------------------------------------
41
String JavaDoc savePath;
42     double thumbnailSize = 100;
43     String JavaDoc filename;
44     String JavaDoc displayPath;
45
46   // Methods --------------------------------------------------------
47
protected void updateParams(FieldData f)
48     {
49         super.updateParams(f);
50         updateParam("filename", f);
51     }
52
53     public boolean editMessagePreProccess(EditMessage in)
54     {
55         if (in.replyMessageID != -1)
56             return true;
57
58         boolean b = super.editMessagePreProccess(in);
59         try
60         {
61             URLConnection JavaDoc con = new URL JavaDoc(url).openConnection();
62             con.connect();
63
64             if (filename == null)
65             {
66                 filename = Utils.formatSIDateTime().replaceAll(":", ".")+" - "+StringUtils.URLEncode(title).replaceAll("\\/", "_").replaceAll("\\\\", "_");
67
68                 String JavaDoc contentType = con.getContentType();
69                 if (contentType.equals("image/gif"))
70                     filename = filename+".gif";
71                 else if (contentType.equals("image/jpeg"))
72                     filename = filename+".jpg";
73                 else if (contentType.equals("image/png"))
74                     filename = filename+".png";
75                 else
76                     throw new IOException("unknown content type");
77
78                 b = super.editMessagePreProccess(in);
79             }
80             File localCopy = new File(savePath, filename);
81
82             if (!localCopy.exists())
83             {
84                 System.err.println(localCopy);
85                 localCopy.createNewFile();
86
87                 InputStream inS = con.getInputStream();
88                 OutputStream outS = new FileOutputStream(localCopy);
89                 Utils.pipeStream(inS, outS);
90                 inS.close();
91                 outS.close();
92             }
93         }
94         catch (Exception JavaDoc e)
95         {
96             e.printStackTrace();
97             in.addErrorMessage("failed to download image: "+e);
98             return false;
99         }
100
101         try
102         {
103             // generate thumbnail
104
new GenerateThumbnails(savePath, thumbnailSize);
105         }
106         catch (Exception JavaDoc e)
107         {
108             e.printStackTrace();
109             in.addErrorMessage("failed to generate thumbnails: "+e);
110             return false;
111         }
112         return b;
113     }
114
115   // Types ----------------------------------------------------------
116

117   // Setters --------------------------------------------------------
118
public void setSavePath(String JavaDoc in) { savePath = in; }
119     public void setFilename(String JavaDoc in) { filename = in; }
120     public void setDisplayPath(String JavaDoc in) { displayPath = in; }
121
122   // Getters --------------------------------------------------------
123
public String JavaDoc getWriteView() { return "picture-write.jsp"; }
124     public String JavaDoc getMessageView() { return "picture-message.jsp"; }
125     public String JavaDoc getFilename() { return filename; }
126
127     public String JavaDoc getThumbnail(String JavaDoc inFilename)
128     {
129         String JavaDoc foo = StringUtils.URLEncode(inFilename.substring(0, inFilename.lastIndexOf(".")));
130         foo += ".jpg";
131         return displayPath+"/.thumbnails/"+foo;
132     }
133 }
Popular Tags