1 /** 2 * This file Copyright (c) 2003-2015 Magnolia International 3 * Ltd. (http://www.magnolia-cms.com). All rights reserved. 4 * 5 * 6 * This file is dual-licensed under both the Magnolia 7 * Network Agreement and the GNU General Public License. 8 * You may elect to use one or the other of these licenses. 9 * 10 * This file is distributed in the hope that it will be 11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the 12 * implied warranty of MERCHANTABILITY or FITNESS FOR A 13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. 14 * Redistribution, except as permitted by whichever of the GPL 15 * or MNA you select, is prohibited. 16 * 17 * 1. For the GPL license (GPL), you can redistribute and/or 18 * modify this file under the terms of the GNU General 19 * Public License, Version 3, as published by the Free Software 20 * Foundation. You should have received a copy of the GNU 21 * General Public License, Version 3 along with this program; 22 * if not, write to the Free Software Foundation, Inc., 51 23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24 * 25 * 2. For the Magnolia Network Agreement (MNA), this file 26 * and the accompanying materials are made available under the 27 * terms of the MNA which accompanies this distribution, and 28 * is available at http://www.magnolia-cms.com/mna.html 29 * 30 * Any modifications to this file must keep this entire header 31 * intact. 32 * 33 */ 34 package info.magnolia.cms.beans.runtime; 35 36 import org.apache.commons.io.IOUtils; 37 import org.apache.commons.lang.StringUtils; 38 import org.slf4j.Logger; 39 import org.slf4j.LoggerFactory; 40 41 import java.io.FileInputStream; 42 import java.io.FileNotFoundException; 43 import java.io.InputStream; 44 import java.io.Serializable; 45 46 47 /** 48 * A wrapper for a temporary uploaded file. 49 */ 50 public class Document implements Serializable { 51 private static final Logger log = LoggerFactory.getLogger(Document.class); 52 53 /** 54 * request parameter name. 55 */ 56 private String atomName; 57 58 /** 59 * File name, without extension. 60 */ 61 private String fileName; 62 63 /** 64 * File extension. 65 */ 66 private String extension; 67 68 /** 69 * Mime type. 70 */ 71 private String type; 72 73 /** 74 * Underlying file. 75 */ 76 private java.io.File file; 77 78 /** 79 * A reference to the file input stream. 80 */ 81 transient private FileInputStream inputStream; 82 83 /** 84 * package private constructor. 85 */ 86 Document() { 87 } 88 89 /** 90 * Used to create a document based on a existing file. 91 */ 92 public Document(java.io.File file, String type) { 93 String fileName = file.getName(); 94 this.setFile(file); 95 this.setType(type); 96 this.setExtension(StringUtils.substringAfterLast(fileName, ".")); 97 this.setFileName(StringUtils.substringBeforeLast(fileName, ".")); 98 } 99 100 /** 101 * Sets the parameter name. 102 * @param name parameter name 103 */ 104 public void setAtomName(String name) { 105 this.atomName = name; 106 } 107 108 /** 109 * Returns the parameter name. 110 * @return parameter name 111 */ 112 public String getAtomName() { 113 return this.atomName; 114 } 115 116 /** 117 * Sets the file name without extension. 118 * @param name file name without extension 119 */ 120 public void setFileName(String name) { 121 this.fileName = name; 122 } 123 124 /** 125 * Returns the file name without extension. 126 * @return file name 127 */ 128 public String getFileName() { 129 return this.fileName; 130 } 131 132 /** 133 * Returns the full file name with extension (if existing). 134 * @return file name with extension 135 */ 136 public String getFileNameWithExtension() { 137 if (StringUtils.isEmpty(this.extension)) { 138 return this.fileName; 139 } 140 141 return this.fileName + "." + this.extension; 142 } 143 144 /** 145 * Sets the mime type for this file. 146 * @param type mime type 147 */ 148 public void setType(String type) { 149 this.type = type; 150 } 151 152 /** 153 * Returns the mime type for this file. 154 * @return mime type 155 */ 156 public String getType() { 157 return this.type; 158 } 159 160 /** 161 * Sets a reference to the uploaded file. 162 * @param in file 163 */ 164 public void setFile(java.io.File in) { 165 this.file = in; 166 } 167 168 public void setExtension(String ext) { 169 this.extension = ext; 170 } 171 172 /** 173 * Returns the file extension. 174 * @return file extension 175 */ 176 public String getExtension() { 177 return this.extension; 178 } 179 180 /** 181 * Returns the file length in bytes. 182 * @return file length 183 */ 184 public long getLength() { 185 return this.file.length(); 186 } 187 188 /** 189 * Returns a stream from the uploaded file. Note that subsequent invocation will always return a reference to the 190 * same input stream. 191 * @return stream from the uploaded file 192 */ 193 public InputStream getStream() { 194 try { 195 return (this.inputStream = (new FileInputStream(this.file))); 196 } 197 catch (FileNotFoundException e) { 198 log.error("Failed to locate file {}" + this.file.getAbsolutePath()); 199 return null; 200 } 201 } 202 203 /** 204 * Returns the uploaded file. Users should normally use getStream, but getFile() can be used when you need to 205 * repeatedly access the file. <strong>The obtained file should never be deleted by the caller</strong> 206 * @return a reference to the uploaded file. 207 */ 208 public java.io.File getFile() { 209 return this.file; 210 } 211 212 /** 213 * Delete the file, taking care of closing an open input stream. 214 */ 215 public void delete() { 216 IOUtils.closeQuietly(inputStream); 217 this.file.delete(); 218 } 219 }