Class FileUploadPlugin

  • All Implemented Interfaces:
    XWikiPluginInterface

    public class FileUploadPlugin
    extends XWikiDefaultPlugin
    Plugin that offers access to uploaded files. The uploaded files are automatically parsed and preserved as a list of FileItems.
    Version:
    $Id: a3411759e49830eee793a7e1b900452ec2e3a931 $
    • Field Detail

      • FILE_LIST_KEY

        public static final String FILE_LIST_KEY
        The context name of the uploaded file list. It can be used to retrieve the list of uploaded files from the context. Note that the order of the list is not guaranteed and might depend of the servlet engine used.
        See Also:
        Constant Field Values
      • UPLOAD_MAXSIZE_PARAMETER

        public static final String UPLOAD_MAXSIZE_PARAMETER
        The name of the parameter that can be set in the global XWiki preferences to override the default maximum file size.
        See Also:
        Constant Field Values
      • UPLOAD_DEFAULT_MAXSIZE

        public static final long UPLOAD_DEFAULT_MAXSIZE
        The default maximum size for uploaded documents. This limit can be changed using the upload_maxsize XWiki preference.
        See Also:
        Constant Field Values
      • UPLOAD_SIZETHRESHOLD_PARAMETER

        public static final String UPLOAD_SIZETHRESHOLD_PARAMETER
        The name of the parameter that can be set in the global XWiki preferences to override the default size threshold for on-disk storage.
        See Also:
        Constant Field Values
    • Method Detail

      • init

        public void init​(XWikiContext context)
        Description copied from interface: XWikiPluginInterface
        Global initialization, called when the platform is started and the plugin list is initialized.
        Specified by:
        init in interface XWikiPluginInterface
        Overrides:
        init in class XWikiDefaultPlugin
        Parameters:
        context - the current context, should not be stored since it will become mostly unreliable once the initial request is terminated
      • endRendering

        public void endRendering​(XWikiContext context)
        Hook method, called after the 1.0 rendering engine finished processing a wiki document. This allows a plugin to clean up its per-rendering variables, or to modify the (velocity) context. This method can be called multiple times per request, once for each time the 1.0 rendering engine is invoked. At this point the content is processed by all of the rendering components and is ready to be included in the response.

        Make sure we don't leave files in temp directories and in memory.

        Specified by:
        endRendering in interface XWikiPluginInterface
        Overrides:
        endRendering in class XWikiDefaultPlugin
        Parameters:
        context - the current request context
        See Also:
        allows to also alter the rendering result
      • loadFileList

        public void loadFileList​(long uploadMaxSize,
                                 int uploadSizeThreshold,
                                 String tempdir,
                                 XWikiContext context)
                          throws XWikiException
        Loads the list of uploaded files in the context if there are any uploaded files.
        Parameters:
        uploadMaxSize - Maximum size of the uploaded files.
        uploadSizeThreshold - the threshold over which the file data should be stored on disk, and not in memory.
        tempdir - Temporary directory to store the uploaded files that are not kept in memory.
        context - Context of the request.
        Throws:
        XWikiException - if the request could not be parsed, or the maximum file size was reached.
        See Also:
        FileUploadPluginApi.loadFileList(long, int, String)
      • getFileItems

        public List<org.apache.commons.fileupload.FileItem> getFileItems​(XWikiContext context)
        Allows to retrieve the current list of uploaded files, as a list of FileItems. loadFileList(XWikiContext) needs to be called beforehand. Note that the order of this list is not guaranteed and might be different depending on the servlet engine used.
        Parameters:
        context - Context of the request.
        Returns:
        A list of FileItem elements.
        See Also:
        FileUploadPluginApi.getFileItems()
      • getFileItemInputStream

        public InputStream getFileItemInputStream​(String formfieldName,
                                                  XWikiContext context)
                                           throws IOException
        Allows to retrieve the contents of an uploaded file as a stream. loadFileList(XWikiContext) needs to be called beforehand.
        Parameters:
        formfieldName - The name of the form field.
        context - Context of the request.
        Returns:
        a InputStream on the file content
        Throws:
        IOException - if I/O problem occurs
        Since:
        2.3M2
      • getFileItemSize

        public int getFileItemSize​(String formfieldName,
                                   XWikiContext context)
        Retrieve the size of a file content in byte. loadFileList(XWikiContext) needs to be called beforehand.
        Parameters:
        formfieldName - The name of the form field.
        context - Context of the request.
        Returns:
        the size of the file in byte
        Since:
        2.3M2
      • getFileItemNames

        public List<String> getFileItemNames​(XWikiContext context)
        Retrieves the list of FileItem names. loadFileList(XWikiContext) needs to be called beforehand.
        Parameters:
        context - Context of the request
        Returns:
        List of strings of the item names
      • getFileName

        public String getFileName​(String formfieldName,
                                  XWikiContext context)
        Get the name of the file uploaded for a form field.
        Parameters:
        formfieldName - The name of the form field.
        context - Context of the request.
        Returns:
        The file name, or null if no file was uploaded for that form field.
      • getFile

        public org.apache.commons.fileupload.FileItem getFile​(String formfieldName,
                                                              XWikiContext context)
        Return the FileItem corresponding to the file uploaded for a form field.
        Parameters:
        formfieldName - The name of the form field.
        context - Context of the request.
        Returns:
        The corresponding FileItem, or null if no file was uploaded for that form field.