1 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 ; 36 import java.net.URLConnection ; 37 38 public class Pictures extends Links 39 { 40 String savePath; 42 double thumbnailSize = 100; 43 String filename; 44 String displayPath; 45 46 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 con = new URL (url).openConnection(); 62 con.connect(); 63 64 if (filename == null) 65 { 66 filename = Utils.formatSIDateTime().replaceAll(":", ".")+" - "+StringUtils.URLEncode(title).replaceAll("\\/", "_").replaceAll("\\\\", "_"); 67 68 String 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 e) 95 { 96 e.printStackTrace(); 97 in.addErrorMessage("failed to download image: "+e); 98 return false; 99 } 100 101 try 102 { 103 new GenerateThumbnails(savePath, thumbnailSize); 105 } 106 catch (Exception e) 107 { 108 e.printStackTrace(); 109 in.addErrorMessage("failed to generate thumbnails: "+e); 110 return false; 111 } 112 return b; 113 } 114 115 117 public void setSavePath(String in) { savePath = in; } 119 public void setFilename(String in) { filename = in; } 120 public void setDisplayPath(String in) { displayPath = in; } 121 122 public String getWriteView() { return "picture-write.jsp"; } 124 public String getMessageView() { return "picture-message.jsp"; } 125 public String getFilename() { return filename; } 126 127 public String getThumbnail(String inFilename) 128 { 129 String foo = StringUtils.URLEncode(inFilename.substring(0, inFilename.lastIndexOf("."))); 130 foo += ".jpg"; 131 return displayPath+"/.thumbnails/"+foo; 132 } 133 } | Popular Tags |