@Slf4j @CompileDynamic class AppResourceLoader extends Object
A place for file resource related functionality which may required an application context or logged-in user. Things related to the attachments directory or the tempUploadsDirectory or any other directory we reference from Config.groovy would go here, especially if they require GString-like parsing. See Attachments and application.groovy for more description of how this works
Modifiers | Name | Description |
---|---|---|
static String |
ATTACHMENT_LOCATION_KEY |
Type | Name and description |
---|---|
GrailsApplication |
grailsApplication |
ResourceLoader |
resourceLoader |
String |
resourcesConfigRootKey The path to resources config root. eg "nine.resources" |
Type Params | Return Type | Name and description |
---|---|---|
|
String |
buildResourceKey(String subKey) |
|
Map |
createAttachmentFile(Long attachmentId, String name, String extension, Object data, String location = null) Creates (or move) file relative to the MonthDirectory. |
|
File |
createTempFile(String originalFileName, Object data) Creates a temp file inside the tempDir and, optionally, populates it with data. |
|
void |
deleteTempUploadedFiles(String attachmentListJson) Deletes any files in the temp directory which are in the list. |
|
void |
deleteTempUploadedFiles(List attachmentList) |
|
void |
forceMkdir(String path) |
|
String |
getAttachmentsRelativePath(File file) returns the path relative to the the attachments directory |
|
ClassLoader |
getClassLoader() |
|
def |
getCurrentTenant() |
|
File |
getFile(String location) Shorthand way to get the attachment file with nothing but the location column contents. |
|
File |
getLocation(String key, Map args = [:], boolean create = true) Get a configured directory location from a nine.resources key. |
|
File |
getMonthDirectory(String configKey, boolean create = true) Gets a month directory for a configuration key. |
|
File |
getProperFile(String fileName, String key, boolean create = true) getProperFile builds a File from a name and ensures it exists, or throws an error. |
|
String |
getRelativePath(String key = 'rootLocation', File child) gets the path relative to some configured directory. |
|
String |
getRelativePath(File parent, File child) gets the relative path of the child with respect to the parent The child must be inside of the parent or the resulting string is null. |
|
String |
getRelativeTempPath(File file) returns the path relative to the the temporary resource directory |
|
Resource |
getResource(String location) if location starts with a / then it realtive to the war (web-app). |
|
def |
getResourceConfig(String subKey) |
|
Resource |
getResourceDirFromKey(String key) get resource dir relative to the config key if key is null then it return the root location |
|
Resource |
getResourceRelative(String locationBase, String location) gets a resource with a locationBase and a relative location - if relative location starts with "/" just pass it to getResource() and ignore locationBase (will look in war (web-app in dev)) - if locationBase is null then we will use a default getLocation('attachments.location') - if relative location starts with URL like classpath:,file:, http: it be passed directly to resourceLoader - if locationBase starts with config: then it will use whatever key comes after to find the location (ex- config:reports.location) |
|
File |
getRootLocation() Gets the rootLocation which is the base for all the app-related directories. |
|
List |
getScripts(Map args = [:]) Get a list of script locations as absolute files. |
|
File |
getSubDirectory(String key, String relativePath, boolean create = true, Map args = [:]) |
|
File |
getTempDir() |
|
String |
getTenantUniqueKey() |
|
void |
init() |
|
Map |
mergeClientValues(Map args = [:]) If the service which wants this directory offers some additional values for substitution they can be passed in with an args map. |
|
void |
setConfiguration(Config co) |
|
File |
verifyOrCreateLocation(File dir, String key, boolean create, boolean wasAbsolute = false) verifyOrCreateLocation checks for validity of the directory before it's returned. |
The path to resources config root. eg "nine.resources"
Creates (or move) file relative to the MonthDirectory. If the data param is a File then its the temp file, rename and move. If data is a byte[] or a String then write to the new file
attachmentId
- the ID of the Attachment record this file is going into.name
- optional name of the fileextension
- The file extension to use.kind
- The "kind" of file from AttachmentKind.name if desireddata
- The contents of the file as a File(temp file), String, byte array or null.
If its null then returns null Creates a temp file inside the tempDir and, optionally, populates it with data.
The file is guaranteed to not have a colliding name.
If the originalFileName has a dot in the name, then the final file short name will be
originalFileName
- the name of the file that was uploadeddata
- is the file contents, and can be String, byte[], or null.
Deletes any files in the temp directory which are in the list.
returns the path relative to the the attachments directory
Shorthand way to get the attachment file with nothing but the location column contents.
Get a configured directory location from a nine.resources key.
key
- The name of the key within the 'nine.resources' construct.env
- The map of substitutions allowedcreate
- True if the directory should be created if missing.Gets a month directory for a configuration key.
configKey
- the dot notation path from the rousources base ex: "attachments.location"create
- If true, create the directory if it's missing.getProperFile builds a File from a name and ensures it exists, or throws an error. If the name is absolute then it builds based on just that name. If the name is relative then it is built relative to rootLocation.
gets the path relative to some configured directory. This is a shallow wrapper around getRelativePath(File,File).
child
- The file to get the relative path of.key
- The key name in RallyDefaultConfig.groovy, for example 'attachments.location'gets the relative path of the child with respect to the parent The child must be inside of the parent or the resulting string is null.
returns the path relative to the the temporary resource directory
if location starts with a / then it realtive to the war (web-app). For example, '/WEB-INF/invoice/greenbar.ftl if it does not start with a / then its considered relative to the rootLocation For example, '2010-11/23452.pdf' would look for file://myroot/2010-11/23452.pdf if that what was set in nine.attachments.directory
get resource dir relative to the config key if key is null then it return the root location
gets a resource with a locationBase and a relative location - if relative location starts with "/" just pass it to getResource() and ignore locationBase (will look in war (web-app in dev)) - if locationBase is null then we will use a default getLocation('attachments.location') - if relative location starts with URL like classpath:,file:, http: it be passed directly to resourceLoader - if locationBase starts with config: then it will use whatever key comes after to find the location (ex- config:reports.location)
locationBase
- if null uses the default key of 'attachments.location'location
- the relative location to useGets the rootLocation which is the base for all the app-related directories. This must be a Closure in the config, it must return a string which will act like a GString and which refers to a directory which exists. You can use code inside the Closure to substitute values there, and also you can use: - ${tenantId} (Client.id) - ${tenantSubDomain} (Client.num)
If the service which wants this directory offers some additional values for substitution they can be passed in with an args map. Any tentantId or tenantSubDomain which is passed in should override the default values. At the time this was written there were no services offering additional values, but tenantId and tenantSubDomain are used. The returned map is a shallow-cloned map. This means that if the map passed in did not have tenantId or tenantSubDomain then it will not have those values after the call, but if some existing value is changed in the destination map then it will be changed in the original as well.
args
- A Map of values to be passed to the configuration Closure, if it's a Closure.verifyOrCreateLocation checks for validity of the directory before it's returned.
dir
- (File) The File object to be tested.key
- (String) The key name in RallyDefaultConfig.groovy for example 'autocash.importDir'create
- (boolean) true to create the directory if absent, false to throw an error.wasAbsolute
- (boolean) True if the file was defined in the configuration as an absolute file.