📄 jahiafilemanagerbaseservice.java
字号:
//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//////// JahiaFilemanagerBaseService//// NK 02.02.2001////package org.jahia.services.filemanager;import java.io.*;import java.util.*;import java.sql.*;import java.net.*;import javax.servlet.*;import javax.servlet.http.*;import org.jahia.tools.files.*;import org.jahia.utils.*; // JahiaConsoleimport org.jahia.exceptions.*; // JahiaInitializationExceptionimport org.jahia.settings.*; // JahiaPrivateSettingsimport org.jahia.params.*;import org.jahia.services.sites.*;import org.jahia.services.*;import org.jahia.services.usermanager.*;import org.jahia.registries.*;import org.jahia.data.fields.*;import org.jahia.data.files.*;import org.jahia.utils.*;import org.jahia.utils.keygenerator.JahiaKeyGen;import org.jahia.data.JahiaDOMObject;import org.jahia.data.JahiaDBDOMObject;/** * Class JahiaFilemanagerBaseService * Handle files stored on disk. * * @author Khue ng * @version 1.0 */public class JahiaFilemanagerBaseService extends JahiaFilemanagerService { private static final String CLASS_NAME = JahiaFilemanagerBaseService.class.getName(); private static JahiaFilemanagerBaseService m_instance = null; /** debug flag **/ public static boolean m_DebugQuery = false; /** the real path to the storage root directory **/ private static String m_FileRepositoryDiskpath = null; /** the file max size for file upload **/ private static int m_FileUploadMaxSize = 50485760; // ~50MB /** the Prefix Name to use when creating new Directory **/ private static String m_DIR_PREFIX_NAME = "JAHIAFM"; /** the Prefix Name to use when creating new temp Directory **/ private static String m_TMP_DIR_PREFIX_NAME = "TMP_JAHIAFM"; /** the default Jahia Filemanager owner id **/ private int m_DEFAULT_JAHIA_ID = 1; // FIXME load from properties file ? /** the file repository attribute name in the properties file **/ public final static String PROP_FILE_REPOSITORY_ROOT_PATH = "files.repositoryRootPath"; /** the file max size for upload attribute name in the properties file **/ public final static String PROP_FILE_FILEUPLOAD_MAXSIZE = "files.fileUploadMaxSize"; /** the instance of FilemanagerDB **/ private FilemanagerDB m_FilemanagerDB; /** the instance of FolderDB **/ private FolderDB m_FolderDB; /** the mime types **/ private Properties m_MimeTypes; /** * Constructor * */ protected JahiaFilemanagerBaseService() { } /** * Method getInstance * * @return a reference to the unique instance ofthis class */ public static synchronized JahiaFilemanagerBaseService getInstance() { if ( m_instance == null ) { m_instance = new JahiaFilemanagerBaseService(); } return m_instance; } /*** * init * NK 31.10.2000 * NK 18.11.2000 setting passed through parameters * @param JahiaPrivateSettings jSettings */ public void init( JahiaPrivateSettings jSettings ) throws JahiaInitializationException { m_FileRepositoryDiskpath = jSettings.jahiaFileRepositoryDiskPath; m_FileUploadMaxSize = jSettings.jahiaFileUploadMaxSize; m_MimeTypes = jSettings.getMimeTypes(); if ( m_MimeTypes == null ) m_MimeTypes = new Properties(); // init file repository directory File repositoryFile = new File(m_FileRepositoryDiskpath); if ( !repositoryFile.exists() ) { repositoryFile.mkdirs(); if ( !repositoryFile.isDirectory() ){ JahiaConsole.println("JahiaFilemanagerBaseService","Error Creating repository Folder"); throw new JahiaInitializationException("Error Creating File Repository Directory"); } } m_FilemanagerDB = FilemanagerDB.getInstance(); m_FolderDB = FolderDB.getInstance(); } // end init //-------------------------------------------------------------------------- // // Jahia File Field Section // //-------------------------------------------------------------------------- /** * @param int the siteID * @param pageID * @param publicFile * @return Vector of available files */ public Vector getFilesByPage ( int siteID, int pageID , boolean publicFile){ Filemanager fmng = getFilemanagerByOwner(siteID); String sqlStr = null; if ( fmng != null){ sqlStr = " where pageid_jahia_file=" + pageID; if ( publicFile ) { sqlStr += " or ( filemgrid_jahia_file=" + fmng.getFilemanagerID() + " and is_public_jahia_file=1 )"; } sqlStr += " order by realname_jahia_file "; } return getFilesDB(sqlStr); // actually select all files } /** * @param int fieldID * @return the file object associated with this field */ public JahiaFileField getFileField( int fieldID ) throws JahiaException { return getJahiaFileField(fieldID); } /** * @param JahiaFileField * @return true if success */ public boolean saveJahiaFileField( JahiaFileField fField ) throws JahiaException { JahiaFileField tmpField = getJahiaFileField(fField.getFieldID()); if ( tmpField != null ){ updateJahiaFileField(fField); } else { insertJahiaFileField(fField); } return true; } /** * Change the field value with new file ID * @param int fieldID * @param int fileID */ public void changeFile( int fieldID, int fileID ) throws JahiaException{ JahiaField fieldItem = ServicesRegistry.getInstance().getJahiaFieldService().loadField(fieldID); Integer val = new Integer(fileID); fieldItem.setValue(val.toString()); // 0 for parentAclID in saveField, because field already exists // -> field already has an aclID // -> no need to create a new one ServicesRegistry.getInstance().getJahiaFieldService().saveField(fieldItem, 0, null); } //-------------------------------------------------------------------------- // // Disk Storage Section // //-------------------------------------------------------------------------- /** * Get the File Repository Root Path * */ public String getFileRepositoryRootPath(){ return this.m_FileRepositoryDiskpath; } /** * Set the FileRepositoryRootPath * * @param path the FileRepositoryRootPath */ protected void setFileRepositoryRootPath(String path){ if ( path.length()>0 ){ this.m_FileRepositoryDiskpath= path; } } /** * Get the Dir Prefix Name * */ public String getDirPrefixName(){ return this.m_DIR_PREFIX_NAME; } /** * Set the Dir Prefix Name * * @param path the prefixName */ public void setDirPrefixName(String prefixName){ this.m_DIR_PREFIX_NAME = prefixName; } /** * Get the File Upload Max Size * */ public int getFileUploadMaxSize(){ return this.m_FileUploadMaxSize; } /** * Set the FileUploadMaxSize * * @param maxSize the file Upload Max Size */ public void setFileUploadMaxSize(int maxSize){ this.m_FileUploadMaxSize = maxSize; } /** * Create a new directory in the File Repository Root Directory<br> * The Directory name is randomly generated * * @author Khue ng * @return the abstract File for the created directory else null on error */ public File createDirectory() { File tmpFile = null; File rootFile = new File(getFileRepositoryRootPath()); if ( rootFile != null && rootFile.isDirectory() && rootFile.canWrite() ){ String dirName = getUniqueDirName(); tmpFile = new File(getFileRepositoryRootPath() + File.separator + getDirPrefixName() + dirName); tmpFile.mkdir(); } else { // error with root file repository toConsole("createDirectory, File repository Root Path is not accessible or doesn't exists"); } return tmpFile; } /** * Create a new directory in the File Repository Root Directory<br> * The Directory name is the site key * * @param JahiaSite the site for which to create the file repository directory * @author Khue ng * @return the abstract File for the created directory else null on error */ public File createDirectory(JahiaSite site) { File tmpFile = null; File rootFile = new File(getFileRepositoryRootPath()); if ( rootFile != null && rootFile.isDirectory() && rootFile.canWrite() ){ String dirName = site.getSiteKey(); tmpFile = new File(getFileRepositoryRootPath() + File.separator + dirName); tmpFile.mkdir(); } else { // error with root file repository toConsole("createDirectory, File repository Root Path is not accessible or doesn't exists"); } return tmpFile; } /** * Create a new directory in the File Repository Root Directory<br> * * @param dirName The name of the directory to create * @param overwrite if true overwrite existing directory with same name * @return the abstract File for the created directory else null on error */ public File createDirectory(String dirName, boolean overwrite) { File tmpFile = null; File rootFile = new File(getFileRepositoryRootPath()); if ( rootFile != null && rootFile.isDirectory() && rootFile.canWrite() ){ tmpFile = new File(getFileRepositoryRootPath() + File.separator + dirName); if ( overwrite ) { if ( (tmpFile != null) && tmpFile.isDirectory() && tmpFile.canWrite() ){ tmpFile.delete(); // delete first tmpFile.mkdir(); // recreate again } else { tmpFile.mkdir(); // create new } } else if ( (tmpFile == null) && !tmpFile.isDirectory() ){ tmpFile.mkdir(); // create new } } else { // error with root file repository toConsole("createDirectory, File repository Root Path is not accessible or doesn't exists"); } return tmpFile; } /** * Handle file download * * @exception ServletException * @exception IOException * */ public boolean handleFileDownload( HttpServletRequest req,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -