1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.HttpHeaders;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.nio.charset.Charset;
25
26
27
28
29 public class DiskFileUpload extends AbstractDiskHttpData implements FileUpload {
30 public static String baseDirectory;
31
32 public static boolean deleteOnExitTemporaryFile = true;
33
34 public static final String prefix = "FUp_";
35
36 public static final String postfix = ".tmp";
37
38 private String filename;
39
40 private String contentType;
41
42 private String contentTransferEncoding;
43
44 public DiskFileUpload(String name, String filename, String contentType,
45 String contentTransferEncoding, Charset charset, long size) {
46 super(name, charset, size);
47 setFilename(filename);
48 setContentType(contentType);
49 setContentTransferEncoding(contentTransferEncoding);
50 }
51
52 @Override
53 public HttpDataType getHttpDataType() {
54 return HttpDataType.FileUpload;
55 }
56
57 @Override
58 public String getFilename() {
59 return filename;
60 }
61
62 @Override
63 public void setFilename(String filename) {
64 if (filename == null) {
65 throw new NullPointerException("filename");
66 }
67 this.filename = filename;
68 }
69
70 @Override
71 public int hashCode() {
72 return FileUploadUtil.hashCode(this);
73 }
74
75 @Override
76 public boolean equals(Object o) {
77 return o instanceof FileUpload && FileUploadUtil.equals(this, (FileUpload) o);
78 }
79
80 @Override
81 public int compareTo(InterfaceHttpData o) {
82 if (!(o instanceof FileUpload)) {
83 throw new ClassCastException("Cannot compare " + getHttpDataType() +
84 " with " + o.getHttpDataType());
85 }
86 return compareTo((FileUpload) o);
87 }
88
89 public int compareTo(FileUpload o) {
90 return FileUploadUtil.compareTo(this, o);
91 }
92
93 @Override
94 public void setContentType(String contentType) {
95 if (contentType == null) {
96 throw new NullPointerException("contentType");
97 }
98 this.contentType = contentType;
99 }
100
101 @Override
102 public String getContentType() {
103 return contentType;
104 }
105
106 @Override
107 public String getContentTransferEncoding() {
108 return contentTransferEncoding;
109 }
110
111 @Override
112 public void setContentTransferEncoding(String contentTransferEncoding) {
113 this.contentTransferEncoding = contentTransferEncoding;
114 }
115
116 @Override
117 public String toString() {
118 return HttpPostBodyUtil.CONTENT_DISPOSITION + ": " +
119 HttpPostBodyUtil.FORM_DATA + "; " + HttpPostBodyUtil.NAME + "=\"" + getName() +
120 "\"; " + HttpPostBodyUtil.FILENAME + "=\"" + filename + "\"\r\n" +
121 HttpHeaders.Names.CONTENT_TYPE + ": " + contentType +
122 (charset != null? "; " + HttpHeaders.Values.CHARSET + '=' + charset.name() + "\r\n" : "\r\n") +
123 HttpHeaders.Names.CONTENT_LENGTH + ": " + length() + "\r\n" +
124 "Completed: " + isCompleted() +
125 "\r\nIsInMemory: " + isInMemory() + "\r\nRealFile: " +
126 (file != null ? file.getAbsolutePath() : "null") + " DefaultDeleteAfter: " +
127 deleteOnExitTemporaryFile;
128 }
129
130 @Override
131 protected boolean deleteOnExit() {
132 return deleteOnExitTemporaryFile;
133 }
134
135 @Override
136 protected String getBaseDirectory() {
137 return baseDirectory;
138 }
139
140 @Override
141 protected String getDiskFilename() {
142 return "upload";
143 }
144
145 @Override
146 protected String getPostfix() {
147 return postfix;
148 }
149
150 @Override
151 protected String getPrefix() {
152 return prefix;
153 }
154
155 @Override
156 public FileUpload copy() {
157 DiskFileUpload upload = new DiskFileUpload(getName(),
158 getFilename(), getContentType(), getContentTransferEncoding(), getCharset(), size);
159 ByteBuf buf = content();
160 if (buf != null) {
161 try {
162 upload.setContent(buf.copy());
163 } catch (IOException e) {
164 throw new ChannelException(e);
165 }
166 }
167 return upload;
168 }
169
170 @Override
171 public FileUpload duplicate() {
172 DiskFileUpload upload = new DiskFileUpload(getName(),
173 getFilename(), getContentType(), getContentTransferEncoding(), getCharset(), size);
174 ByteBuf buf = content();
175 if (buf != null) {
176 try {
177 upload.setContent(buf.duplicate());
178 } catch (IOException e) {
179 throw new ChannelException(e);
180 }
181 }
182 return upload;
183 }
184
185 @Override
186 public FileUpload retain(int increment) {
187 super.retain(increment);
188 return this;
189 }
190
191 @Override
192 public FileUpload retain() {
193 super.retain();
194 return this;
195 }
196 }