use of lucee.commons.io.res.ResourceMetaData in project Lucee by lucee.
the class Directory method actionList.
/**
* list all files and directories inside a directory
* @throws PageException
*/
public static Object actionList(PageContext pageContext, Resource directory, String serverPassword, int type, ResourceFilter filter, int listInfo, boolean recurse, String sort) throws PageException {
// check directory
SecurityManager securityManager = pageContext.getConfig().getSecurityManager();
securityManager.checkFileLocation(pageContext.getConfig(), directory, serverPassword);
if (type != TYPE_ALL) {
ResourceFilter typeFilter = (type == TYPE_DIR) ? DIRECTORY_FILTER : FILE_FILTER;
if (filter == null)
filter = typeFilter;
else
filter = new AndResourceFilter(new ResourceFilter[] { typeFilter, filter });
}
// create query Object
String[] names = new String[] { "name", "size", "type", "dateLastModified", "attributes", "mode", "directory" };
String[] types = new String[] { "VARCHAR", "DOUBLE", "VARCHAR", "DATE", "VARCHAR", "VARCHAR", "VARCHAR" };
boolean hasMeta = directory instanceof ResourceMetaData;
if (hasMeta) {
names = new String[] { "name", "size", "type", "dateLastModified", "attributes", "mode", "directory", "meta" };
types = new String[] { "VARCHAR", "DOUBLE", "VARCHAR", "DATE", "VARCHAR", "VARCHAR", "VARCHAR", "OBJECT" };
}
Array array = null;
Query query = null;
Object rtn;
if (listInfo == LIST_INFO_QUERY_ALL || listInfo == LIST_INFO_QUERY_NAME) {
boolean listOnlyNames = listInfo == LIST_INFO_QUERY_NAME;
rtn = query = new QueryImpl(listOnlyNames ? new String[] { "name" } : names, listOnlyNames ? new String[] { "VARCHAR" } : types, 0, "query");
} else
rtn = array = new ArrayImpl();
if (!directory.exists()) {
if (directory instanceof FileResource)
return rtn;
throw new ApplicationException("directory [" + directory.toString() + "] doesn't exist");
}
if (!directory.isDirectory()) {
if (directory instanceof FileResource)
return rtn;
throw new ApplicationException("file [" + directory.toString() + "] exists, but isn't a directory");
}
if (!directory.isReadable()) {
if (directory instanceof FileResource)
return rtn;
throw new ApplicationException("no access to read directory [" + directory.toString() + "]");
}
long startNS = System.nanoTime();
try {
// Query All
if (listInfo == LIST_INFO_QUERY_ALL)
_fillQueryAll(query, directory, filter, 0, hasMeta, recurse);
else // Query Name
if (listInfo == LIST_INFO_QUERY_NAME) {
if (recurse || type != TYPE_ALL)
_fillQueryNamesRec("", query, directory, filter, 0, recurse);
else
_fillQueryNames(query, directory, filter, 0);
} else // Array Name/Path
if (listInfo == LIST_INFO_ARRAY_NAME || listInfo == LIST_INFO_ARRAY_PATH) {
boolean onlyName = listInfo == LIST_INFO_ARRAY_NAME;
if (// QueryNamesRec("",query, directory, filter, 0,recurse);
!onlyName || recurse || type != TYPE_ALL)
// QueryNamesRec("",query, directory, filter, 0,recurse);
_fillArrayPathOrName(array, directory, filter, 0, recurse, onlyName);
else
_fillArrayName(array, directory, filter, 0);
}
} catch (IOException e) {
throw Caster.toPageException(e);
}
// sort
if (sort != null && query != null) {
String[] arr = sort.toLowerCase().split(",");
for (int i = arr.length - 1; i >= 0; i--) {
try {
String[] col = arr[i].trim().split("\\s+");
if (col.length == 1)
query.sort(col[0].trim());
else if (col.length == 2) {
String order = col[1].toLowerCase().trim();
if (order.equals("asc"))
query.sort(col[0], lucee.runtime.type.Query.ORDER_ASC);
else if (order.equals("desc"))
query.sort(col[0], lucee.runtime.type.Query.ORDER_DESC);
else
throw new ApplicationException("invalid order type [" + col[1] + "]");
}
} catch (Throwable t) {
ExceptionUtil.rethrowIfNecessary(t);
}
}
}
if (query != null)
query.setExecutionTime(System.nanoTime() - startNS);
return rtn;
}
Aggregations