001    package de.deepamehta.plugins.files;
002    
003    import org.apache.commons.fileupload.FileItem;
004    
005    import java.io.File;
006    
007    
008    
009    /**
010     * An uploaded file.
011     * <p>
012     * Files are uploaded via the REST API by POSTing <code>multipart/form-data</code> to the <code>/files</code>
013     * resource.
014     * <p>
015     * Client-side support: the public API of the <code>deepamehta-files</code> plugin provides a method
016     * <code>dm4c.get_plugin("de.deepamehta.files").open_upload_dialog()</code> that allows the user to
017     * choose and upload a file.</p>
018     * <p>
019     * At server-side a plugin accesses the upload file via the
020     * {@link de.deepamehta.core.service.Plugin#executeCommandHook}. ### FIXDOC</p>
021     *
022     * @author <a href="mailto:jri@deepamehta.de">Jörg Richter</a>
023     */
024    public class UploadedFile {
025    
026        // ---------------------------------------------------------------------------------------------- Instance Variables
027    
028        private FileItem fileItem;
029    
030        // ---------------------------------------------------------------------------------------------------- Constructors
031    
032        public UploadedFile(FileItem fileItem) {
033            this.fileItem = fileItem;
034        }
035    
036        // -------------------------------------------------------------------------------------------------- Public Methods
037    
038        /**
039         * Returns the original (client-side) file name.
040         */
041        public String getName() {
042            return fileItem.getName();
043        }
044    
045        public long getSize() {
046            return fileItem.getSize();
047        }
048    
049        /**
050         * Returns the content type passed by the browser or null if not defined.
051         */
052        public String getMediaType() {
053            return fileItem.getContentType();
054        }
055    
056        // ---
057    
058        @Override
059        public String toString() {
060            return "file \"" + getName() + "\" (" + getMediaType() + "), " + getSize() + " bytes";
061        }
062    
063        // ----------------------------------------------------------------------------------------- Package Private Methods
064    
065        void write(File file) throws Exception {
066            fileItem.write(file);   // throws Exception
067        }
068    }