View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package io.netty.handler.codec.http.multipart;
17  
18  import io.netty.buffer.ByteBuf;
19  import io.netty.channel.ChannelException;
20  import io.netty.handler.codec.http.HttpHeaderNames;
21  import io.netty.handler.codec.http.HttpHeaderValues;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.nio.charset.Charset;
26  
27  /**
28   * Disk FileUpload implementation that stores file into real files
29   */
30  public class DiskFileUpload extends AbstractDiskHttpData implements FileUpload {
31      public static String baseDirectory;
32  
33      public static boolean deleteOnExitTemporaryFile = true;
34  
35      public static final String prefix = "FUp_";
36  
37      public static final String postfix = ".tmp";
38  
39      private final String baseDir;
40  
41      private final boolean deleteOnExit;
42  
43      private String filename;
44  
45      private String contentType;
46  
47      private String contentTransferEncoding;
48  
49      public DiskFileUpload(String name, String filename, String contentType,
50              String contentTransferEncoding, Charset charset, long size, String baseDir, boolean deleteOnExit) {
51          super(name, charset, size);
52          setFilename(filename);
53          setContentType(contentType);
54          setContentTransferEncoding(contentTransferEncoding);
55          this.baseDir = baseDir == null ? baseDirectory : baseDir;
56          this.deleteOnExit = deleteOnExit;
57      }
58  
59      public DiskFileUpload(String name, String filename, String contentType,
60              String contentTransferEncoding, Charset charset, long size) {
61          this(name, filename, contentType, contentTransferEncoding,
62                  charset, size, baseDirectory, deleteOnExitTemporaryFile);
63      }
64  
65      @Override
66      public HttpDataType getHttpDataType() {
67          return HttpDataType.FileUpload;
68      }
69  
70      @Override
71      public String getFilename() {
72          return filename;
73      }
74  
75      @Override
76      public void setFilename(String filename) {
77          this.filename = FileUploadUtil.validateFileNameForMultiPart(filename);
78      }
79  
80      @Override
81      public int hashCode() {
82          return FileUploadUtil.hashCode(this);
83      }
84  
85      @Override
86      public boolean equals(Object o) {
87          return o instanceof FileUpload && FileUploadUtil.equals(this, (FileUpload) o);
88      }
89  
90      @Override
91      public int compareTo(InterfaceHttpData o) {
92          if (!(o instanceof FileUpload)) {
93              throw new ClassCastException("Cannot compare " + getHttpDataType() +
94                      " with " + o.getHttpDataType());
95          }
96          return compareTo((FileUpload) o);
97      }
98  
99      public int compareTo(FileUpload o) {
100         return FileUploadUtil.compareTo(this, o);
101     }
102 
103     @Override
104     public void setContentType(String contentType) {
105         this.contentType = FileUploadUtil.validateContentTypeForMultiPart(contentType);
106     }
107 
108     @Override
109     public String getContentType() {
110         return contentType;
111     }
112 
113     @Override
114     public String getContentTransferEncoding() {
115         return contentTransferEncoding;
116     }
117 
118     @Override
119     public void setContentTransferEncoding(String contentTransferEncoding) {
120         this.contentTransferEncoding = contentTransferEncoding;
121     }
122 
123     @Override
124     public String toString() {
125         File file = null;
126         try {
127             file = getFile();
128         } catch (IOException e) {
129             // Should not occur.
130         }
131 
132         return HttpHeaderNames.CONTENT_DISPOSITION + ": " +
133                HttpHeaderValues.FORM_DATA + "; " + HttpHeaderValues.NAME + "=\"" + getName() +
134                 "\"; " + HttpHeaderValues.FILENAME + "=\"" + filename + "\"\r\n" +
135                 HttpHeaderNames.CONTENT_TYPE + ": " + contentType +
136                 (getCharset() != null? "; " + HttpHeaderValues.CHARSET + '=' + getCharset().name() + "\r\n" : "\r\n") +
137                 HttpHeaderNames.CONTENT_LENGTH + ": " + length() + "\r\n" +
138                 "Completed: " + isCompleted() +
139                 "\r\nIsInMemory: " + isInMemory() + "\r\nRealFile: " +
140                 (file != null ? file.getAbsolutePath() : "null") + " DeleteAfter: " + deleteOnExit;
141     }
142 
143     @Override
144     protected boolean deleteOnExit() {
145         return deleteOnExit;
146     }
147 
148     @Override
149     protected String getBaseDirectory() {
150         return baseDir;
151     }
152 
153     @Override
154     protected String getDiskFilename() {
155         return "upload";
156     }
157 
158     @Override
159     protected String getPostfix() {
160         return postfix;
161     }
162 
163     @Override
164     protected String getPrefix() {
165         return prefix;
166     }
167 
168     @Override
169     public FileUpload copy() {
170         final ByteBuf content = content();
171         return replace(content != null ? content.copy() : null);
172     }
173 
174     @Override
175     public FileUpload duplicate() {
176         final ByteBuf content = content();
177         return replace(content != null ? content.duplicate() : null);
178     }
179 
180     @Override
181     public FileUpload retainedDuplicate() {
182         ByteBuf content = content();
183         if (content != null) {
184             content = content.retainedDuplicate();
185             boolean success = false;
186             try {
187                 FileUpload duplicate = replace(content);
188                 success = true;
189                 return duplicate;
190             } finally {
191                 if (!success) {
192                     content.release();
193                 }
194             }
195         } else {
196             return replace(null);
197         }
198     }
199 
200     @Override
201     public FileUpload replace(ByteBuf content) {
202         DiskFileUpload upload = new DiskFileUpload(
203                 getName(), getFilename(), getContentType(), getContentTransferEncoding(), getCharset(), size,
204                 baseDir, deleteOnExit);
205         if (content != null) {
206             try {
207                 upload.setContent(content);
208             } catch (IOException e) {
209                 throw new ChannelException(e);
210             }
211         }
212         upload.setCompleted(isCompleted());
213         return upload;
214     }
215 
216     @Override
217     public FileUpload retain(int increment) {
218         super.retain(increment);
219         return this;
220     }
221 
222     @Override
223     public FileUpload retain() {
224         super.retain();
225         return this;
226     }
227 
228     @Override
229     public FileUpload touch() {
230         super.touch();
231         return this;
232     }
233 
234     @Override
235     public FileUpload touch(Object hint) {
236         super.touch(hint);
237         return this;
238     }
239 }