2 * jFCPlib - FileEntry.java - Copyright © 2008–2016 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.fcp;
20 import java.io.InputStream;
21 import java.util.HashMap;
25 * Container class for file entry data.
27 * @see ClientPutComplexDir#addFileEntry(FileEntry)
28 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
30 public abstract class FileEntry {
32 /** The name of the file. */
33 protected final String name;
35 /** The upload source of the file. */
36 protected final UploadFrom uploadFrom;
39 * Creates a new file entry with the given name and upload source.
42 * The name of the file
44 * The upload source of the file
46 protected FileEntry(String name, UploadFrom uploadFrom) {
48 this.uploadFrom = uploadFrom;
52 * Creates a new file entry for a file that should be transmitted to the
53 * node in the payload of the message.
56 * The name of the file
58 * The content type of the file, or <code>null</code> to let the
61 * The length of the file
62 * @param dataInputStream
63 * The input stream of the file
64 * @return A file entry
66 public static FileEntry createDirectFileEntry(String name, String contentType, long length, InputStream dataInputStream) {
67 return new DirectFileEntry(name, contentType, length, dataInputStream);
71 * Creates a new file entry for a file that should be uploaded from disk.
74 * The name of the file
76 * The name of the file on disk
78 * The content type of the file, or <code>null</code> to let the
81 * The length of the file, or <code>-1</code> to not specify a
83 * @return A file entry
85 public static FileEntry createDiskFileEntry(String name, String filename, String contentType, long length) {
86 return new DiskFileEntry(name, filename, contentType, length);
90 * Creates a new file entry for a file that redirects to another URI.
93 * The name of the file
95 * The target URI of the redirect
96 * @return A file entry
98 public static FileEntry createRedirectFileEntry(String name, String targetURI) {
99 return new RedirectFileEntry(name, targetURI);
103 * Returns the fields for this file entry.
105 * @return The fields for this file entry
107 abstract Map<String, String> getFields();
110 * A file entry for a file that should be transmitted in the payload of the
111 * {@link ClientPutComplexDir} message.
113 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
115 static class DirectFileEntry extends FileEntry {
117 /** The content type of the data. */
118 private final String contentType;
120 /** The length of the data. */
121 private final long length;
123 /** The input stream of the data. */
124 private final InputStream inputStream;
127 * Creates a new direct file entry with content type auto-detection.
130 * The name of the file
132 * The length of the file
134 * The input stream of the file
136 public DirectFileEntry(String name, long length, InputStream inputStream) {
137 this(name, null, length, inputStream);
141 * Creates a new direct file entry.
144 * The name of the file
146 * The content type of the file, or <code>null</code> to let
147 * the node auto-detect it
149 * The length of the file
151 * The input stream of the file
153 public DirectFileEntry(String name, String contentType, long length, InputStream inputStream) {
154 super(name, UploadFrom.direct);
155 this.contentType = contentType;
156 this.length = length;
157 this.inputStream = inputStream;
164 Map<String, String> getFields() {
165 Map<String, String> fields = new HashMap<String, String>();
166 fields.put("Name", name);
167 fields.put("UploadFrom", String.valueOf(uploadFrom));
168 fields.put("DataLength", String.valueOf(length));
169 if (contentType != null) {
170 fields.put("Metadata.ContentType", contentType);
176 * Returns the input stream of the file.
178 * @return The input stream of the file
180 InputStream getInputStream() {
187 * A file entry for a file that should be uploaded from the disk.
189 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
191 static class DiskFileEntry extends FileEntry {
193 /** The name of the on-disk file. */
194 private final String filename;
196 /** The content type of the file. */
197 private final String contentType;
199 /** The length of the file. */
200 private final long length;
203 * Creates a new disk file entry.
206 * The name of the file
208 * The name of the on-disk file
210 * The length of the file
212 public DiskFileEntry(String name, String filename, long length) {
213 this(name, filename, null, length);
217 * Creates a new disk file entry.
220 * The name of the file
222 * The name of the on-disk file
224 * The content type of the file, or <code>null</code> to let
225 * the node auto-detect it
227 * The length of the file
229 public DiskFileEntry(String name, String filename, String contentType, long length) {
230 super(name, UploadFrom.disk);
231 this.filename = filename;
232 this.contentType = contentType;
233 this.length = length;
240 Map<String, String> getFields() {
241 Map<String, String> fields = new HashMap<String, String>();
242 fields.put("Name", name);
243 fields.put("UploadFrom", String.valueOf(uploadFrom));
244 fields.put("Filename", filename);
246 fields.put("DataSize", String.valueOf(length));
248 if (contentType != null) {
249 fields.put("Metadata.ContentType", contentType);
257 * A file entry for a file that redirects to another URI.
259 * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
261 static class RedirectFileEntry extends FileEntry {
263 /** The target URI of the redirect. */
264 private String targetURI;
267 * Creates a new redirect file entry.
270 * The name of the file
272 * The target URI of the redirect
274 public RedirectFileEntry(String name, String targetURI) {
275 super(name, UploadFrom.redirect);
276 this.targetURI = targetURI;
283 Map<String, String> getFields() {
284 Map<String, String> fields = new HashMap<String, String>();
285 fields.put("Name", name);
286 fields.put("UploadFrom", String.valueOf(uploadFrom));
287 fields.put("TargetURI", targetURI);