Class FileStoreAPI

java.lang.Object
com.jbstrap.core.filestore.FileStoreAPI

public final class FileStoreAPI extends Object

FileStore API

The FileStore is the JBStrap solution for a file management system, that can operate independently of any environment, thus assisting the developer by covering the tasks related to file management. The FileStore is supported by every file handling method, component and the Record class, thus making it function like a quasi database. In this case, when files are stored on the server's file system, the only data that gets stored in the database is an ID. However, when reading a Record, the file will be accessible, in the JBFile format.

The FILE_STORE_BASE parameter must be set in the application, which contains the filestore's basic path. The folder specified here will contain the files and filestores.

Multiple filestores can be created within the application, which will work like the folders in the file system. Every filestore can have their individual folder assigned to it, that will contain the files that are stored in the filestore. The folder specified for the file store must contain a relative path (from the FILE_STORE_BASE parameter).

Files will be placed in the individual filestores. Any number of files can be stored in a FileStore, and files with the same name can also be stored. Within the FileStore, the files will be placed in individual folders, thus ensuring a swifter read back. When a file is saved, it will recieve a unique ID, that will replace it's original name, ensuring that there is no conflict because of matching filenames. This ID is used when reading back, or deleting files.

The storing of the file's meta data in the database is handled by the handler specified in the FILE_HANDLER JBStrap parameter. The JBStrap framework provides a default file handler, which stores the metadata in a string format in a Record. With the default filehandler, multiple files can be stored in a single field. For more information, see the DefaultFileHandler class.

If you want to use a custom file handler, then it must have a class that extends from the FileHandler abstract class, where the data storage processes must be implemented. In the description of the FileHandler class, you can find detailed instructions for creating a custom file handler.

In the File Store API class the basic file handling operations are implemented: creating and storing, reading back files, and deleting files.

When the FileStore API encounters an error, it will generate its own error message, (FileStoreException) which will contain the error message. These messages can be translated (multilingual) to any language by creating a properties file for the desired language(s). Here, the following keys can be specified:

Key Default message Description
fileStore.notFound FileStore not found by name: {0} Occurs if the filestore is not found. The {0} parameter contains the filestore's name.
baseDirNotExists FileStore ({0}) base directory does not exist or does not have write access. Directory: {1} Occurs if the base directory is not found, or has no access. The {0} parameter contains the file store's name, and the {1} parameter contains the missing folder
fileStore.writeError Can not write {1} file to FileStore ({0}) Occurs if the application has no access to write the filestore folder. The {0} parameter contains the filestore name, The {1} parameter contains the file that the application is trying to write
fileStore.fileNotFound {1} file not found in FileStore ({0}) Occurs if the specified file is not found in the filestore. The {0} parameter contains the filestore name, the {1} parameter contains the file that is being searched for.
fileStore.readError {1} file can not be read from FileStore ({0}) Occurs if it has no reading access rights. The {0} parameter contains the filestore name, The {1} parameter contains the file, that cannot be read by the application.

For a more detailed description, see: MessageSourceAPI

Since:
4.0
Author:
JBStrap
See Also:
  • Method Details

    • store

      public static JBFile store(JBFile file)
      Stores the specified file in the FileStore
      Parameters:
      file - The file to be stored
      Returns:
      The stored file. It will contain it's unique ID.
      Throws:
      NullPointerException - If the file was specified as null
      FileStoreException - If the specified file's filestore was not found, or if the filestore's folder does not exist in the file system, or if there was a write error.
    • getFile

      public static JBFile getFile(String fileStoreName, String fileId, String filename, Long size)
      Gets the specified file from the specified filestore.
      Parameters:
      fileStoreName - The name of the filestore
      fileId - The id of the file
      filename - The file's original name. (If not specified it will be called "unknown")
      size - The filesize.(If not specified, it will be null)
      Returns:
      The requested file
      Throws:
      FileStoreException - If the specified file or filestore was not found, or if there was a read error.
    • delete

      public static void delete(String fileStoreName, String fileId)
      Deletes the specified file from the specified filestore.
      Parameters:
      fileStoreName - The name of the filestore
      fileId - The id of the file
      Throws:
      FileStoreException - If the specified file or filestore was not found, or if the access was denied.