1 // 2 // $Id: FileData.java 95 2007-05-02 03:27:05Z 3 // /C=DE/ST=Baden-Wuerttemberg/O=ISDN4Linux/OU=Fritz 4 // Elfert/CN=svn-felfert@isdn4linux.de/emailAddress=fritz@fritz-elfert.de $ 5 // 6 // jupload - A file upload applet. 7 // Copyright 2007 The JUpload Team 8 // 9 // Created: 2006-11-20 10 // Creator: etienne_sf 11 // Last modified: $Date: 2015-03-14 15:13:43 +0100 (sam., 14 mars 2015) $ 12 // 13 // This program is free software; you can redistribute it and/or modify it under 14 // the terms of the GNU General Public License as published by the Free Software 15 // Foundation; either version 2 of the License, or (at your option) any later 16 // version. This program is distributed in the hope that it will be useful, but 17 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 18 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 19 // details. You should have received a copy of the GNU General Public License 20 // along with this program; if not, write to the Free Software Foundation, Inc., 21 // 675 Mass Ave, Cambridge, MA 02139, USA. 22 23 package wjhk.jupload2.filedata; 24 25 import java.io.File; 26 import java.io.InputStream; 27 import java.util.Date; 28 29 import wjhk.jupload2.exception.JUploadException; 30 import wjhk.jupload2.exception.JUploadIOException; 31 import wjhk.jupload2.gui.filepanel.treeview.TreeFileDataNode; 32 import wjhk.jupload2.policies.UploadPolicy; 33 import wjhk.jupload2.upload.FileUploadThread; 34 import wjhk.jupload2.upload.helper.ByteArrayEncoder; 35 36 /** 37 * This class contains all data and methods for a file to upload. The current 38 * {@link wjhk.jupload2.policies.UploadPolicy} contains the necessary parameters to personalize the way files must be 39 * handled. <BR> 40 * The JUpload package provides a default implementation of this class in {@link DefaultFileData}. This default 41 * implementation contains all necessary methods to allow upload. You can override it to add new file behaviour. For 42 * instance, you could add a XMLFileData, that would check that XML is valid before upload. See the <a 43 * href="package-summary.html">package summary</a> for more details about that. <BR> 44 * This class is the interface that all FileData must implement. The {@link DefaultFileData} class contains the default 45 * implementation for this interface. The {@link PictureFileData} contains another implementation of this interface, 46 * adapted to manage pictures (rotation, resizing...). <BR> 47 * The instance of FileData is created by the {@link UploadPolicy#createFileData(File, File)} method. This method can be 48 * overrided in a new upoad policy, to create an instance of another FileData. See {@link PictureFileData} for an 49 * example about FileData customization. 50 * 51 * @author etienne_sf 52 */ 53 54 public interface FileData { 55 56 /** 57 * Called during the upload, by the {@link FileUploadThread}. The FileData instance should then call the 58 * {@link ByteArrayEncoder#appendTextProperty(String, String, int)} method to add each file property to the current 59 * upload. 60 * 61 * @param bae The byte encoder, where the properties must be added 62 * @param index Index of the file concerned by this value. -1 if this is a global parameter. 63 * @throws JUploadIOException Encapsulation of the IOException, if any would occurs. 64 * @see ByteArrayEncoder#appendTextProperty(String, String, int) 65 */ 66 public void appendFileProperties(ByteArrayEncoder bae, int index) throws JUploadIOException; 67 68 /** 69 * Prepare the fileData to upload. For instance, picture data can be resized before upload (see 70 * {@link PictureFileData}. This method is called before the upload of this file. If no exception is thrown, then 71 * the file is correctly prepared. 72 * 73 * @param uploadFileRoot The biggest path which is common to all uploaded files. Used to calculate the relativeDir 74 * to this root path, for each file. This relative path is sent as metadata during the upload 75 * @throws JUploadException Encapsulation of the Exception, if any error would occurs. 76 * @see FileUploadThread 77 * @see #getRelativeDir() 78 */ 79 public void beforeUpload(String uploadFileRoot) throws JUploadException; 80 81 /** 82 * Get size of upload, which may be different from the actual file length. This call is valid only after a call to 83 * {@link #beforeUpload()} and before the call to {@link #afterUpload()}. 84 * 85 * @return The length of upload. In this class, this is the size of the file, as it isn't transformed for upload. 86 * This size may change if encoding is necessary (needs a new FileData class), or if picture is to be 87 * resized or rotated. 88 * @see PictureFileData 89 */ 90 public long getUploadLength(); 91 92 /** 93 * This function is called after upload, whether it is successful or not. It allows fileData to free any resource 94 * created for the upload. For instance, {@link PictureFileData#afterUpload()} removes the temporary file, if any 95 * was created. 96 */ 97 public void afterUpload(); 98 99 /** 100 * This function creates an InputStream from this file. The {@link FileUploadThread} class then reads bytes from it 101 * and transfers them to the webserver. The caller is responsible for closing this stream.<BR> 102 * This method may only be called when {@link #isPreparedForUpload()} returns true. 103 * 104 * @throws JUploadException Encapsulation of the Exception, if any would occurs. 105 * @throws IllegalStateException When the upload is not prepared (before a call to {@link #beforeUpload()} or after 106 * a call to {@link #afterUpload()} 107 * @return An InputStream, representing this instance. 108 */ 109 public InputStream getInputStream() throws JUploadException; 110 111 /** 112 * Get the original filename. This is the name of the file, into the local hardrive 113 * 114 * @return The original filename 115 */ 116 public String getFileName(); 117 118 /** 119 * @return The extension for the original file. 120 */ 121 public String getFileExtension(); 122 123 /** 124 * @return The length of the original file. 125 */ 126 public long getFileLength(); 127 128 /** 129 * @return The original file date. 130 */ 131 public Date getLastModified(); 132 133 /** 134 * Indicates whether the check box is checked or not. That is: whether the file should be uploaded or not. 135 * 136 * @return 137 */ 138 public boolean getUploadFlag(); 139 140 /** 141 * Allows to set or unset the upload flag. 142 * 143 * @return 144 */ 145 public void setUploadFlag(boolean uploadFlag); 146 147 /** 148 * Get the directory of the file. 149 * 150 * @return The directory where this file is stored. 151 */ 152 public String getDirectory(); 153 154 /** 155 * Retrieves the MD5 sum of the file.<BR> 156 * Since 5.0.0, this method is is in DefaultFileData, and is calculated depending on the sendMD5Sum applet 157 * parameter, during the file preparation file. 158 * 159 * @return The corresponding MD5 sum. 160 * @throws JUploadException 161 * @see #beforeUpload() 162 */ 163 public String getMD5() throws JUploadException; 164 165 /** 166 * This function return the FileData content type. 167 * 168 * @return The mimeType for the file. 169 */ 170 public String getMimeType(); 171 172 /** 173 * Indicate if this file can be read. Take care of the File.canRead() methods, that seems to be wrong from time to 174 * time. 175 * 176 * @return indicates whether the file can be read or not. 177 */ 178 public boolean canRead(); 179 180 /** 181 * Retrieves the path of this file relative to the root directory 182 * 183 * @return This instance's relative path or an empty string if it was not created using a root parameter. 184 */ 185 public String getRelativeDir(); 186 187 /** 188 * Returns the absolute path, as it will be sent during the upload. In standard mode, it is the concatenation of the 189 * root and the relative path, that is: the absolute path of the File to upload on the local file system. In 190 * hierarchical mode, it is the path relative to the (empty) upload root. 191 * 192 * @return 193 */ 194 public String getAbsolutePath(); 195 196 /** 197 * Indicates whether the file can be uploaded or not. This boolean should be set to true in the call to 198 * {@link #beforeUpload()}, and the to false in the call to {@link #afterUpload()}. 199 * 200 * @return True if the file is ready for upload. 201 * @throws IllegalStateException When the upload is not prepared (before a call to {@link #beforeUpload()} or after 202 * a call to {@link #afterUpload()} 203 */ 204 public boolean isPreparedForUpload(); 205 206 /** 207 * Links this FileData to the associated {@link TreeFileDataNode}, from the hierarchical view. 208 * 209 * @return 210 */ 211 public TreeFileDataNode getTreeFileDataNode(); 212 213 /** 214 * Setters for the to the associated {@link TreeFileDataNode}, from the hierarchical view. Allows link between the 215 * flat and hierarchical view. 216 * 217 * @return 218 */ 219 public void setTreeFileDataNode(TreeFileDataNode node); 220 }