1 25 package org.riotfamily.forms.element.upload; 26 27 import java.io.File ; 28 import java.io.FileInputStream ; 29 import java.io.IOException ; 30 31 import javax.servlet.http.HttpServletRequest ; 32 import javax.servlet.http.HttpServletResponse ; 33 34 import org.riotfamily.common.util.FlashInfo; 35 import org.riotfamily.forms.ContentElement; 36 import org.riotfamily.forms.EditorBinder; 37 import org.riotfamily.forms.Element; 38 import org.riotfamily.forms.ErrorUtils; 39 import org.riotfamily.forms.element.TemplateElement; 40 import org.springframework.util.FileCopyUtils; 41 42 47 public class FlashUpload extends FileUpload { 48 49 public static final String FLASH_CONTENT_TYPE = "application/x-shockwave-flash"; 50 51 private String widthProperty; 52 53 private String heightProperty; 54 55 private String versionProperty; 56 57 private boolean isValidSwf = false; 58 59 private int width; 60 61 private int height; 62 63 private int version; 64 65 66 public FlashUpload() { 67 super(); 68 } 69 70 protected void destroy() { 71 super.destroy(); 72 } 73 74 75 public boolean isPreviewAvailable() { 76 return true; 77 } 78 79 protected Element createPreviewElement() { 80 return new PreviewElement(); 81 } 82 83 84 public String getContentType() { 85 return FLASH_CONTENT_TYPE; 86 } 87 88 89 public String getWidthProperty() { 90 return this.widthProperty; 91 } 92 93 public void setWidthProperty(String widthProperty) { 94 this.widthProperty = widthProperty; 95 } 96 97 public String getHeightProperty() { 98 return this.heightProperty; 99 } 100 101 public void setHeightProperty(String heightProperty) { 102 this.heightProperty = heightProperty; 103 } 104 105 public String getVersionProperty() { 106 return versionProperty; 107 } 108 109 public void setVersionProperty(String versionProperty) { 110 this.versionProperty = versionProperty; 111 } 112 113 114 public boolean isValidSwf() { 115 return this.isValidSwf; 116 } 117 118 public int getWidth() { 119 return this.width; 120 } 121 122 public int getHeight() { 123 return this.height; 124 } 125 126 public int getVersion() { 127 return this.version; 128 } 129 130 131 protected void validateFile(File file) { 132 try { 133 parseFlashMovie(file); 134 if (!isValidSwf) { 135 ErrorUtils.reject(this, "flash.invalidFormat"); 136 } 137 } 138 catch (IOException e) { 139 ErrorUtils.reject(this, "unexpected"); 140 } 141 } 142 143 public void setValue(Object value) { 144 super.setValue(value); 145 if (value == null) { 146 return; 147 } 148 149 try { 150 parseFlashMovie(getFile()); 151 EditorBinder editorBinder = getEditorBinding().getEditorBinder(); 152 if (widthProperty != null) { 153 width = getIntegerProperty(editorBinder, widthProperty); 154 } 155 if (heightProperty != null) { 156 height = getIntegerProperty(editorBinder, heightProperty); 157 } 158 if (versionProperty != null) { 159 version = getIntegerProperty(editorBinder, versionProperty);; 160 } 161 } 162 catch (IOException e) { 163 isValidSwf = false; 164 } 165 } 166 167 private static int getIntegerProperty(EditorBinder editorBinder, String propertyName) { 168 int result = 0; 169 Object value = editorBinder.getPropertyValue(propertyName); 170 if (value != null && value instanceof Integer ) { 171 result = ((Integer ) value).intValue(); 172 } 173 return result; 174 } 175 176 public Object getValue() { 177 EditorBinder editorBinder = getEditorBinding().getEditorBinder(); 178 setIntegerProperty(editorBinder, widthProperty, width); 179 setIntegerProperty(editorBinder, heightProperty, height); 180 setIntegerProperty(editorBinder, versionProperty, version); 181 return super.getValue(); 182 } 183 184 private static void setIntegerProperty(EditorBinder editorBinder, String propertyName, int value) { 185 if (value != 0) { 186 if (propertyName != null) { 187 editorBinder.setPropertyValue(propertyName, new Integer (value)); 188 } 189 } 190 } 191 192 protected void parseFlashMovie(File file) throws IOException { 193 FlashInfo flashInfo = new FlashInfo(file); 194 isValidSwf = flashInfo.isValid(); 195 width = flashInfo.getWidth(); 196 height = flashInfo.getHeight(); 197 version = flashInfo.getVersion(); 198 } 199 200 public class PreviewElement extends TemplateElement 201 implements ContentElement { 202 203 public PreviewElement() { 204 setAttribute("flash", FlashUpload.this); 205 } 206 207 public void handleContentRequest(HttpServletRequest request, 208 HttpServletResponse response) throws IOException { 209 210 File file = getFile(); 211 if (file != null && file.exists()) { 212 response.setDateHeader("Expires", 0); 213 response.setHeader("Content-Type", getContentType()); 214 215 response.setContentLength(getSize().intValue()); 216 FileCopyUtils.copy(new FileInputStream (file), 217 response.getOutputStream()); 218 } 219 else { 220 response.sendError(HttpServletResponse.SC_NO_CONTENT); 221 } 222 } 223 224 public String getDownloadUrl() { 225 return getFormContext().getContentUrl(this); 226 } 227 228 } 229 230 } 231 | Popular Tags |