Search in sources :

Example 1 with AndResourceFilter

use of lucee.commons.io.res.filter.AndResourceFilter 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;
}
Also used : SecurityManager(lucee.runtime.security.SecurityManager) Query(lucee.runtime.type.Query) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) ArrayImpl(lucee.runtime.type.ArrayImpl) FileResource(lucee.commons.io.res.type.file.FileResource) IOException(java.io.IOException) Array(lucee.runtime.type.Array) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) FileResourceFilter(lucee.commons.io.res.filter.FileResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) DirectoryResourceFilter(lucee.commons.io.res.filter.DirectoryResourceFilter) QueryImpl(lucee.runtime.type.QueryImpl) ApplicationException(lucee.runtime.exp.ApplicationException) ResourceMetaData(lucee.commons.io.res.ResourceMetaData)

Example 2 with AndResourceFilter

use of lucee.commons.io.res.filter.AndResourceFilter in project Lucee by lucee.

the class Directory method actionCopy.

public static void actionCopy(PageContext pc, Resource directory, String strDestination, String serverPassword, boolean createPath, Object acl, String storage, final ResourceFilter filter, boolean recurse, int nameconflict) throws PageException {
    // check directory
    SecurityManager securityManager = pc.getConfig().getSecurityManager();
    securityManager.checkFileLocation(pc.getConfig(), directory, serverPassword);
    if (!directory.exists())
        throw new ApplicationException("directory [" + directory.toString() + "] doesn't exist");
    if (!directory.isDirectory())
        throw new ApplicationException("file [" + directory.toString() + "] exists, but isn't a directory");
    if (!directory.canRead())
        throw new ApplicationException("no access to read directory [" + directory.toString() + "]");
    if (StringUtil.isEmpty(strDestination))
        throw new ApplicationException("attribute destination is not defined");
    // real to source
    Resource newdirectory = toDestination(pc, strDestination, directory);
    if (nameconflict == NAMECONFLICT_ERROR && newdirectory.exists())
        throw new ApplicationException("new directory [" + newdirectory.toString() + "] already exist");
    securityManager.checkFileLocation(pc.getConfig(), newdirectory, serverPassword);
    try {
        boolean clearEmpty = false;
        // has already a filter
        ResourceFilter f = null;
        if (filter != null) {
            if (!recurse) {
                f = new AndResourceFilter(new ResourceFilter[] { filter, new NotResourceFilter(DirectoryResourceFilter.FILTER) });
            } else {
                clearEmpty = true;
                f = new OrResourceFilter(new ResourceFilter[] { filter, DirectoryResourceFilter.FILTER });
            }
        } else {
            if (!recurse)
                f = new NotResourceFilter(DirectoryResourceFilter.FILTER);
        }
        if (!createPath) {
            Resource p = newdirectory.getParentResource();
            if (p != null && !p.exists())
                throw new ApplicationException("parent directory for [" + newdirectory + "] doesn't exist");
        }
        ResourceUtil.copyRecursive(directory, newdirectory, f);
        if (clearEmpty)
            ResourceUtil.removeEmptyFolders(newdirectory, f == null ? null : new NotResourceFilter(filter));
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw new ApplicationException(t.getMessage());
    }
    // set S3 stuff
    setS3Attrs(pc, directory, acl, storage);
}
Also used : AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter) FileResourceFilter(lucee.commons.io.res.filter.FileResourceFilter) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) ResourceFilter(lucee.commons.io.res.filter.ResourceFilter) DirectoryResourceFilter(lucee.commons.io.res.filter.DirectoryResourceFilter) ApplicationException(lucee.runtime.exp.ApplicationException) SecurityManager(lucee.runtime.security.SecurityManager) OrResourceFilter(lucee.commons.io.res.filter.OrResourceFilter) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) Resource(lucee.commons.io.res.Resource) FileResource(lucee.commons.io.res.type.file.FileResource) NotResourceFilter(lucee.commons.io.res.filter.NotResourceFilter)

Example 3 with AndResourceFilter

use of lucee.commons.io.res.filter.AndResourceFilter in project Lucee by lucee.

the class FileStorageScopeCleaner method _clean.

@Override
protected void _clean() {
    ConfigWebImpl cwi = (ConfigWebImpl) engine.getFactory().getConfig();
    Resource dir = type == Scope.SCOPE_CLIENT ? cwi.getClientScopeDir() : cwi.getSessionScopeDir();
    // for old files only the defintion from admin can be used
    long timeout = type == Scope.SCOPE_CLIENT ? cwi.getClientTimeout().getMillis() : cwi.getSessionTimeout().getMillis();
    long time = new DateTimeImpl(cwi).getTime() - timeout;
    try {
        // delete files that has expired
        AndResourceFilter andFilter = new AndResourceFilter(new ResourceFilter[] { EXT_FILTER, new ExpiresFilter(time, true) });
        String appName, cfid2, cfid;
        Resource[] apps = dir.listResources(DIR_FILTER), cfidDir, files;
        if (apps != null)
            for (int a = 0; a < apps.length; a++) {
                appName = StorageScopeImpl.decode(apps[a].getName());
                cfidDir = apps[a].listResources(DIR_FILTER);
                if (cfidDir != null)
                    for (int b = 0; b < cfidDir.length; b++) {
                        cfid2 = cfidDir[b].getName();
                        files = cfidDir[b].listResources(andFilter);
                        if (files != null) {
                            for (int c = 0; c < files.length; c++) {
                                cfid = files[c].getName();
                                cfid = cfid2 + cfid.substring(0, cfid.length() - 5);
                                if (listener != null)
                                    listener.doEnd(engine, this, appName, cfid);
                                // info("remove from memory "+appName+"/"+cfid);
                                engine.remove(type, appName, cfid);
                                info("remove file " + files[c]);
                                files[c].delete();
                            }
                        }
                    }
            }
        ResourceUtil.deleteEmptyFolders(dir);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        error(t);
    }
// long maxSize = type==Scope.SCOPE_CLIENT?cwi.getClientScopeDirSize():cwi.getSessionScopeDirSize();
// checkSize(config,dir,maxSize,extfilter);
}
Also used : ConfigWebImpl(lucee.runtime.config.ConfigWebImpl) AndResourceFilter(lucee.commons.io.res.filter.AndResourceFilter) Resource(lucee.commons.io.res.Resource) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl)

Aggregations

AndResourceFilter (lucee.commons.io.res.filter.AndResourceFilter)3 Resource (lucee.commons.io.res.Resource)2 DirectoryResourceFilter (lucee.commons.io.res.filter.DirectoryResourceFilter)2 FileResourceFilter (lucee.commons.io.res.filter.FileResourceFilter)2 NotResourceFilter (lucee.commons.io.res.filter.NotResourceFilter)2 OrResourceFilter (lucee.commons.io.res.filter.OrResourceFilter)2 ResourceFilter (lucee.commons.io.res.filter.ResourceFilter)2 FileResource (lucee.commons.io.res.type.file.FileResource)2 ApplicationException (lucee.runtime.exp.ApplicationException)2 SecurityManager (lucee.runtime.security.SecurityManager)2 IOException (java.io.IOException)1 ResourceMetaData (lucee.commons.io.res.ResourceMetaData)1 ConfigWebImpl (lucee.runtime.config.ConfigWebImpl)1 Array (lucee.runtime.type.Array)1 ArrayImpl (lucee.runtime.type.ArrayImpl)1 Query (lucee.runtime.type.Query)1 QueryImpl (lucee.runtime.type.QueryImpl)1 DateTimeImpl (lucee.runtime.type.dt.DateTimeImpl)1