X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Ffcp%2FFileEntry.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Ffcp%2FFileEntry.java;h=f939d78c7725787e33c9c007c1d1ed541498d6d5;hb=508624458578f01a393f8b44f32b98b40054fdc8;hp=0000000000000000000000000000000000000000;hpb=dd605aee444057a874fabf7fb0045b8448b0d1cd;p=jFCPlib.git diff --git a/src/main/java/net/pterodactylus/fcp/FileEntry.java b/src/main/java/net/pterodactylus/fcp/FileEntry.java new file mode 100644 index 0000000..f939d78 --- /dev/null +++ b/src/main/java/net/pterodactylus/fcp/FileEntry.java @@ -0,0 +1,294 @@ +/* + * jFCPlib - FileEntry.java - Copyright © 2008 David Roden + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +package net.pterodactylus.fcp; + +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; + +/** + * Container class for file entry data. + * + * @see ClientPutComplexDir#addFileEntry(FileEntry) + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ +public abstract class FileEntry { + + /** The name of the file. */ + protected final String name; + + /** The upload source of the file. */ + protected final UploadFrom uploadFrom; + + /** + * Creates a new file entry with the given name and upload source. + * + * @param name + * The name of the file + * @param uploadFrom + * The upload source of the file + */ + protected FileEntry(String name, UploadFrom uploadFrom) { + this.name = name; + this.uploadFrom = uploadFrom; + } + + /** + * Creates a new file entry for a file that should be transmitted to the + * node in the payload of the message. + * + * @param name + * The name of the file + * @param contentType + * The content type of the file, or null to let the + * node auto-detect it + * @param length + * The length of the file + * @param dataInputStream + * The input stream of the file + * @return A file entry + */ + public static FileEntry createDirectFileEntry(String name, String contentType, long length, InputStream dataInputStream) { + return new DirectFileEntry(name, contentType, length, dataInputStream); + } + + /** + * Creates a new file entry for a file that should be uploaded from disk. + * + * @param name + * The name of the file + * @param filename + * The name of the file on disk + * @param contentType + * The content type of the file, or null to let the + * node auto-detect it + * @param length + * The length of the file, or -1 to not specify a + * size + * @return A file entry + */ + public static FileEntry createDiskFileEntry(String name, String filename, String contentType, long length) { + return new DiskFileEntry(name, filename, contentType, length); + } + + /** + * Creates a new file entry for a file that redirects to another URI. + * + * @param name + * The name of the file + * @param targetURI + * The target URI of the redirect + * @return A file entry + */ + public static FileEntry createRedirectFileEntry(String name, String targetURI) { + return new RedirectFileEntry(name, targetURI); + } + + /** + * Returns the fields for this file entry. + * + * @return The fields for this file entry + */ + abstract Map getFields(); + + /** + * A file entry for a file that should be transmitted in the payload of the + * {@link ClientPutComplexDir} message. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ + static class DirectFileEntry extends FileEntry { + + /** The content type of the data. */ + private final String contentType; + + /** The length of the data. */ + private final long length; + + /** The input stream of the data. */ + private final InputStream inputStream; + + /** + * Creates a new direct file entry with content type auto-detection. + * + * @param name + * The name of the file + * @param length + * The length of the file + * @param inputStream + * The input stream of the file + */ + public DirectFileEntry(String name, long length, InputStream inputStream) { + this(name, null, length, inputStream); + } + + /** + * Creates a new direct file entry. + * + * @param name + * The name of the file + * @param contentType + * The content type of the file, or null to let + * the node auto-detect it + * @param length + * The length of the file + * @param inputStream + * The input stream of the file + */ + public DirectFileEntry(String name, String contentType, long length, InputStream inputStream) { + super(name, UploadFrom.direct); + this.contentType = contentType; + this.length = length; + this.inputStream = inputStream; + } + + /** + * {@inheritDoc} + */ + @Override + Map getFields() { + Map fields = new HashMap(); + fields.put("Name", name); + fields.put("UploadFrom", String.valueOf(uploadFrom)); + fields.put("DataLength", String.valueOf(length)); + if (contentType != null) { + fields.put("Metadata.ContentType", contentType); + } + return fields; + } + + /** + * Returns the input stream of the file. + * + * @return The input stream of the file + */ + InputStream getInputStream() { + return inputStream; + } + + } + + /** + * A file entry for a file that should be uploaded from the disk. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ + static class DiskFileEntry extends FileEntry { + + /** The name of the on-disk file. */ + private final String filename; + + /** The content type of the file. */ + private final String contentType; + + /** The length of the file. */ + private final long length; + + /** + * Creates a new disk file entry. + * + * @param name + * The name of the file + * @param filename + * The name of the on-disk file + * @param length + * The length of the file + */ + public DiskFileEntry(String name, String filename, long length) { + this(name, filename, null, length); + } + + /** + * Creates a new disk file entry. + * + * @param name + * The name of the file + * @param filename + * The name of the on-disk file + * @param contentType + * The content type of the file, or null to let + * the node auto-detect it + * @param length + * The length of the file + */ + public DiskFileEntry(String name, String filename, String contentType, long length) { + super(name, UploadFrom.disk); + this.filename = filename; + this.contentType = contentType; + this.length = length; + } + + /** + * {@inheritDoc} + */ + @Override + Map getFields() { + Map fields = new HashMap(); + fields.put("Name", name); + fields.put("UploadFrom", String.valueOf(uploadFrom)); + fields.put("Filename", filename); + if (length > -1) { + fields.put("DataSize", String.valueOf(length)); + } + if (contentType != null) { + fields.put("Metadata.ContentType", contentType); + } + return fields; + } + + } + + /** + * A file entry for a file that redirects to another URI. + * + * @author David ‘Bombe’ Roden <bombe@freenetproject.org> + */ + static class RedirectFileEntry extends FileEntry { + + /** The target URI of the redirect. */ + private String targetURI; + + /** + * Creates a new redirect file entry. + * + * @param name + * The name of the file + * @param targetURI + * The target URI of the redirect + */ + public RedirectFileEntry(String name, String targetURI) { + super(name, UploadFrom.redirect); + this.targetURI = targetURI; + } + + /** + * {@inheritDoc} + */ + @Override + Map getFields() { + Map fields = new HashMap(); + fields.put("Name", name); + fields.put("UploadFrom", String.valueOf(uploadFrom)); + fields.put("TargetURI", targetURI); + return fields; + } + + } + +}