Package org.ametys.core.file
Class FileHelper
java.lang.Object
org.apache.avalon.framework.logger.AbstractLogEnabled
org.ametys.core.file.FileHelper
- All Implemented Interfaces:
Component,Contextualizable,LogEnabled,Serviceable
public final class FileHelper
extends AbstractLogEnabled
implements Component, Serviceable, Contextualizable
Helper for managing files and folders of a application directory such as
WEB-INF/params
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CurrentUserProviderThe current user provider.protected SourceResolverThe source resolverprotected TikaProviderThe tika providerstatic final StringThe Avalon role name -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription_collection2JsonObject(TraversableSource folder, TraversableSource root) Convert collection to JSON objectprotected String_getRelativePath(TraversableSource root, TraversableSource file) Get the relative path from root directory_resource2JsonObject(TraversableSource file, TraversableSource root) Convert file to JSON objectCreate a folderaddOrUpdateFile(Part part, FileSource parentDir, String mode, boolean unzip) Add or update a filevoidcontextualize(Context context) copySource(String srcUri, String parentTargetUri) Copy a file or foldervoiddelete(Path path, DirectoryStream.Filter<Path> fileFilter, boolean recursiveDelete, boolean deleteEmptyDirs) Delete all files corresponding to the file filter into the file tree.deleteFile(String fileUri) Remove a folder or a filefilterSources(TraversableSource source, String value) Get the URIs of sources which match filter value.Get the files/folders of a given sourceGet files and folders contained in the given path.booleanTests if a file/folder with given name existsmoveSource(String srcUri, String parentTargetUri) Move a file or folderrenameFile(String fileUri, String name) Rename a file or a folderSaves text to given file in UTF-8 formatvoidservice(ServiceManager serviceManager) Methods inherited from class org.apache.avalon.framework.logger.AbstractLogEnabled
enableLogging, getLogger, setupLogger, setupLogger, setupLogger
-
Field Details
-
ROLE
The Avalon role name -
_currentUserProvider
The current user provider. -
_tikaProvider
The tika provider -
_srcResolver
The source resolver
-
-
Constructor Details
-
FileHelper
public FileHelper()
-
-
Method Details
-
service
- Specified by:
servicein interfaceServiceable- Throws:
ServiceException
-
contextualize
- Specified by:
contextualizein interfaceContextualizable- Throws:
ContextException
-
getFiles
Get the files/folders of a given source- Parameters:
rootURI- The uri of root folderpath- The path of folder in root folder- Returns:
- the child files and folders as JSON representation
- Throws:
IOException- if an error occurred
-
getFiles
public List<Map<String,Object>> getFiles(String rootURI, String path, List<String> ignoredSources) throws IOException Get files and folders contained in the given path.- Parameters:
rootURI- The uri of root folderpath- the relative file's path from files root directoryignoredSources- The names of source to ignore- Returns:
- the list of parameters files and folders as JSON representation
- Throws:
IOException- If an error occurred while listing files
-
_collection2JsonObject
protected Map<String,Object> _collection2JsonObject(TraversableSource folder, TraversableSource root) Convert collection to JSON object- Parameters:
folder- the folderroot- the root directory- Returns:
- JSON object
-
_resource2JsonObject
Convert file to JSON object- Parameters:
file- the fileroot- the root directory- Returns:
- JSON object
-
_getRelativePath
Get the relative path from root directory- Parameters:
root- The root directoryfile- The file- Returns:
- The relative path
-
saveFile
Saves text to given file in UTF-8 format- Parameters:
fileURI- the file URI. Must point to an existing file.text- the UTF-8 file content- Returns:
- A result map.
- Throws:
IOException- If an error occurred while saving
-
addFolder
public Map<String,Object> addFolder(String parentURI, String name, boolean renameIfExists) throws IOException Create a folder- Parameters:
parentURI- the parent URI, relative to the rootname- the name of the new folder to createrenameIfExists- true if the folder have to be renamed if the folder with same name already exits.- Returns:
- The result Map with the name and uri of created folder, or a boolean "success" to false if an error occurs.
- Throws:
IOException- If an error occurred adding the folder
-
addOrUpdateFile
public Map<String,Object> addOrUpdateFile(Part part, FileSource parentDir, String mode, boolean unzip) throws IOException Add or update a file- Parameters:
part- The file multipart to uploadparentDir- The parent directorymode- The insertion mode: 'add-rename' or 'update' or null.unzip- true to unzip .zip file- Returns:
- the result map
- Throws:
IOException- If an error occurred manipulating the file
-
deleteFile
Remove a folder or a file- Parameters:
fileUri- the file/folder URI- Returns:
- the result map.
- Throws:
IOException- If an error occurs while removing the folder/file
-
delete
public void delete(Path path, DirectoryStream.Filter<Path> fileFilter, boolean recursiveDelete, boolean deleteEmptyDirs) throws IOException Delete all files corresponding to the file filter into the file tree.- Parameters:
path- the path to delete (can be a file or a directory)fileFilter- the file filter to applyrecursiveDelete- iftrue, the file tree will be explored to delete filesdeleteEmptyDirs- iftrue, empty dirs will be deleted- Throws:
IOException- if an error occured while exploring or deleting files
-
renameFile
Rename a file or a folder- Parameters:
fileUri- the relative URI of the file or folder to renamename- the new name of the file/folder- Returns:
- The result Map with the name, path of the renamed file/folder, or a boolean "already-exist" is a file/folder already exists with this name.
- Throws:
IOException- if an error occurs while renaming the file/folder
-
hasChild
Tests if a file/folder with given name exists- Parameters:
parentUri- the parent folder URIname- the name of the child- Returns:
- true if the file exists
- Throws:
IOException- if an error occurred
-
copySource
Copy a file or folder- Parameters:
srcUri- The URI of file/folder to copyparentTargetUri- The URI of parent target file- Returns:
- a result map with the name and uri of copied file in case of success.
- Throws:
IOException- If an error occured manipulating the source
-
moveSource
Move a file or folder- Parameters:
srcUri- The URI of file/folder to moveparentTargetUri- The URI of parent target file- Returns:
- a result map with the name and uri of moved file in case of success.
- Throws:
IOException- If an error occurred manipulating the source
-
filterSources
Get the URIs of sources which match filter value. Source are filtered both on their filename and in their content for text file. The search will be performed on the current source and all its descendants- Parameters:
source- The source to start searchvalue- the value to match- Returns:
- the URIs of matching source
-