Search in sources :

Example 11 with Resource

use of lucee.commons.io.res.Resource in project Lucee by lucee.

the class Directory method _fillArrayName.

// this method only exists for performance reasion
private static int _fillArrayName(Array arr, Resource directory, ResourceFilter filter, int count) {
    if (filter == null || filter instanceof ResourceNameFilter) {
        ResourceNameFilter rnf = filter == null ? null : (ResourceNameFilter) filter;
        String[] list = directory.list();
        if (list == null || list.length == 0)
            return count;
        for (int i = 0; i < list.length; i++) {
            if (rnf == null || rnf.accept(directory, list[i])) {
                arr.appendEL(list[i]);
            }
        }
    } else {
        Resource[] list = directory.listResources();
        if (list == null || list.length == 0)
            return count;
        for (int i = 0; i < list.length; i++) {
            if (filter.accept(list[i])) {
                arr.appendEL(list[i].getName());
            }
        }
    }
    return count;
}
Also used : Resource(lucee.commons.io.res.Resource) FileResource(lucee.commons.io.res.type.file.FileResource) ResourceNameFilter(lucee.commons.io.res.filter.ResourceNameFilter)

Example 12 with Resource

use of lucee.commons.io.res.Resource in project Lucee by lucee.

the class FileTag method _actionUpload.

private static Struct _actionUpload(PageContext pageContext, lucee.runtime.security.SecurityManager securityManager, FormItem formItem, String strDestination, int nameconflict, String accept, boolean strict, int mode, String attributes, Object acl, String serverPassword) throws PageException {
    if (nameconflict == NAMECONFLICT_UNDEFINED)
        nameconflict = NAMECONFLICT_ERROR;
    boolean fileWasRenamed = false;
    boolean fileWasAppended = false;
    boolean fileExisted = false;
    boolean fileWasOverwritten = false;
    String contentType = ResourceUtil.getMimeType(formItem.getResource(), formItem.getContentType());
    // set cffile struct
    Struct cffile = new StructImpl();
    long length = formItem.getResource().length();
    cffile.set("timecreated", new DateTimeImpl(pageContext.getConfig()));
    cffile.set("timelastmodified", new DateTimeImpl(pageContext.getConfig()));
    cffile.set("datelastaccessed", new DateImpl(pageContext));
    cffile.set("oldfilesize", Long.valueOf(length));
    cffile.set("filesize", Long.valueOf(length));
    cffile.set("contenttype", ListFirst.call(pageContext, contentType, "/"));
    cffile.set("contentsubtype", ListLast.call(pageContext, contentType, "/"));
    // client file
    String strClientFile = formItem.getName();
    while (strClientFile.indexOf('\\') != -1) strClientFile = strClientFile.replace('\\', '/');
    Resource clientFile = pageContext.getConfig().getResource(strClientFile);
    String clientFileName = clientFile.getName();
    // check file type
    checkContentType(contentType, accept, getFileExtension(clientFile), strict);
    cffile.set("clientdirectory", getParent(clientFile));
    cffile.set("clientfile", clientFile.getName());
    cffile.set("clientfileext", getFileExtension(clientFile));
    cffile.set("clientfilename", getFileName(clientFile));
    // check destination
    if (StringUtil.isEmpty(strDestination))
        throw new ApplicationException("attribute destination is not defined in tag file");
    Resource destination = toDestination(pageContext, strDestination, null);
    securityManager.checkFileLocation(pageContext.getConfig(), destination, serverPassword);
    if (destination.isDirectory())
        destination = destination.getRealResource(clientFileName);
    else if (!destination.exists() && (strDestination.endsWith("/") || strDestination.endsWith("\\")))
        destination = destination.getRealResource(clientFileName);
    else if (!clientFileName.equalsIgnoreCase(destination.getName())) {
        if (ResourceUtil.getExtension(destination, null) == null)
            destination = destination.getRealResource(clientFileName);
        else
            fileWasRenamed = true;
    }
    // check parent destination -> directory of the desinatrion
    Resource parentDestination = destination.getParentResource();
    if (!parentDestination.exists()) {
        Resource pp = parentDestination.getParentResource();
        if (pp == null || !pp.exists())
            throw new ApplicationException("attribute destination has an invalid value [" + destination + "], directory [" + parentDestination + "] doesn't exist");
        try {
            parentDestination.createDirectory(true);
        } catch (IOException e) {
            throw Caster.toPageException(e);
        }
    } else if (!parentDestination.canWrite())
        throw new ApplicationException("can't write to destination directory [" + parentDestination + "], no access to write");
    // set server variables
    cffile.set("serverdirectory", getParent(destination));
    cffile.set("serverfile", destination.getName());
    cffile.set("serverfileext", getFileExtension(destination));
    cffile.set("serverfilename", getFileName(destination));
    cffile.set("attemptedserverfile", destination.getName());
    // check nameconflict
    if (destination.exists()) {
        fileExisted = true;
        if (nameconflict == NAMECONFLICT_ERROR) {
            throw new ApplicationException("destination file [" + destination + "] already exist");
        } else if (nameconflict == NAMECONFLICT_SKIP) {
            cffile.set("fileexisted", Caster.toBoolean(fileExisted));
            cffile.set("filewasappended", Boolean.FALSE);
            cffile.set("filewasoverwritten", Boolean.FALSE);
            cffile.set("filewasrenamed", Boolean.FALSE);
            cffile.set("filewassaved", Boolean.FALSE);
            return cffile;
        } else if (nameconflict == NAMECONFLICT_MAKEUNIQUE) {
            destination = makeUnique(destination);
            fileWasRenamed = true;
            // if(fileWasRenamed) {
            cffile.set("serverdirectory", getParent(destination));
            cffile.set("serverfile", destination.getName());
            cffile.set("serverfileext", getFileExtension(destination));
            cffile.set("serverfilename", getFileName(destination));
            cffile.set("attemptedserverfile", destination.getName());
        // }
        } else if (nameconflict == NAMECONFLICT_OVERWRITE) {
            // fileWasAppended=true;
            fileWasOverwritten = true;
            if (!destination.delete())
                if (// hier hatte ich concurrent problem das damit ausgeraeumt ist
                destination.exists())
                    throw new ApplicationException("can't delete destination file [" + destination + "]");
        }
    // for "overwrite" no action is neded
    }
    setACL(pageContext, destination, acl);
    try {
        destination.createNewFile();
        IOUtil.copy(formItem.getResource(), destination);
    } catch (Throwable t) {
        ExceptionUtil.rethrowIfNecessary(t);
        throw Caster.toPageException(t);
    }
    // Set cffile/file struct
    cffile.set("fileexisted", Caster.toBoolean(fileExisted));
    cffile.set("filewasappended", Caster.toBoolean(fileWasAppended));
    cffile.set("filewasoverwritten", Caster.toBoolean(fileWasOverwritten));
    cffile.set("filewasrenamed", Caster.toBoolean(fileWasRenamed));
    cffile.set("filewassaved", Boolean.TRUE);
    setMode(destination, mode);
    setAttributes(destination, attributes);
    return cffile;
}
Also used : StructImpl(lucee.runtime.type.StructImpl) ApplicationException(lucee.runtime.exp.ApplicationException) Resource(lucee.commons.io.res.Resource) DateTimeImpl(lucee.runtime.type.dt.DateTimeImpl) IOException(java.io.IOException) DateImpl(lucee.runtime.type.dt.DateImpl) Struct(lucee.runtime.type.Struct)

Example 13 with Resource

use of lucee.commons.io.res.Resource in project Lucee by lucee.

the class Ftp method actionPutFile.

/**
 * copy a local file to server
 * @return FTPClient
 * @throws IOException
 * @throws PageException
 */
private AFTPClient actionPutFile() throws IOException, PageException {
    required("remotefile", remotefile);
    required("localfile", localfile);
    AFTPClient client = getClient();
    // new File(localfile);
    Resource local = ResourceUtil.toResourceExisting(pageContext, localfile);
    // if(failifexists && local.exists()) throw new ApplicationException("File ["+local+"] already exist, if you want to overwrite, set attribute failIfExists to false");
    InputStream is = null;
    try {
        is = IOUtil.toBufferedInputStream(local.getInputStream());
        client.setFileType(getType(local));
        client.storeFile(remotefile, is);
    } finally {
        IOUtil.closeEL(is);
    }
    writeCfftp(client);
    return client;
}
Also used : AFTPClient(lucee.runtime.net.ftp.AFTPClient) InputStream(java.io.InputStream) Resource(lucee.commons.io.res.Resource)

Example 14 with Resource

use of lucee.commons.io.res.Resource in project Lucee by lucee.

the class Zip method actionUnzip.

private void actionUnzip() throws ApplicationException, IOException {
    required("file", file, true);
    required("destination", destination, false);
    ZipInputStream zis = null;
    String path;
    Resource target, parent;
    int index;
    try {
        zis = new ZipInputStream(IOUtil.toBufferedInputStream(file.getInputStream()));
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            path = entry.getName().replace('\\', '/');
            index = path.lastIndexOf('/');
            // recurse
            if (!recurse && index != -1) {
                zis.closeEntry();
                continue;
            }
            target = destination.getRealResource(entry.getName());
            // filter
            if (filter != null && !filter.accept(target)) {
                zis.closeEntry();
                continue;
            }
            // entrypath
            if (!entryPathMatch(path)) {
                zis.closeEntry();
                continue;
            }
            if (!storePath)
                target = destination.getRealResource(target.getName());
            if (entry.isDirectory()) {
                target.mkdirs();
            } else {
                if (storePath) {
                    parent = target.getParentResource();
                    if (!parent.exists())
                        parent.mkdirs();
                }
                if (overwrite || !target.exists())
                    IOUtil.copy(zis, target, false);
            }
            target.setLastModified(entry.getTime());
            zis.closeEntry();
        }
    } finally {
        IOUtil.closeEL(zis);
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ZipEntry(java.util.zip.ZipEntry) Resource(lucee.commons.io.res.Resource)

Example 15 with Resource

use of lucee.commons.io.res.Resource in project Lucee by lucee.

the class Zip method actionDelete.

private void actionDelete() throws ApplicationException, IOException {
    required("file", file, true);
    Resource existing = pageContext.getConfig().getTempDirectory().getRealResource(getTempName());
    IOUtil.copy(file, existing);
    ZipInputStream zis = null;
    ZipOutputStream zos = null;
    try {
        zis = new ZipInputStream(IOUtil.toBufferedInputStream(existing.getInputStream()));
        zos = new ZipOutputStream(IOUtil.toBufferedOutputStream(file.getOutputStream()));
        ZipEntry entry;
        String path, name;
        int index;
        boolean accept;
        if (filter == null && recurse && (entryPaths == null || entryPaths.length == 0))
            throw new ApplicationException("define at least one restriction, can't delete all the entries from a zip file");
        while ((entry = zis.getNextEntry()) != null) {
            accept = false;
            path = entry.getName().replace('\\', '/');
            index = path.lastIndexOf('/');
            if (!recurse && index > 0)
                accept = true;
            // dir=index==-1?"":path.substring(0,index);
            name = path.substring(index + 1);
            if (filter != null && !filter.accept(file.getRealResource(name)))
                accept = true;
            if (!entryPathMatch(path))
                accept = true;
            if (!accept)
                continue;
            add(zos, entry, zis, false);
            zis.closeEntry();
        }
    } finally {
        IOUtil.closeEL(zis);
        IOUtil.closeEL(zos);
        existing.delete();
    }
}
Also used : ZipInputStream(java.util.zip.ZipInputStream) ApplicationException(lucee.runtime.exp.ApplicationException) ZipOutputStream(java.util.zip.ZipOutputStream) ZipEntry(java.util.zip.ZipEntry) Resource(lucee.commons.io.res.Resource)

Aggregations

Resource (lucee.commons.io.res.Resource)309 IOException (java.io.IOException)100 ApplicationException (lucee.runtime.exp.ApplicationException)54 PageException (lucee.runtime.exp.PageException)40 ArrayList (java.util.ArrayList)31 Struct (lucee.runtime.type.Struct)28 ByteArrayInputStream (java.io.ByteArrayInputStream)21 InputStream (java.io.InputStream)21 ExpressionException (lucee.runtime.exp.ExpressionException)19 StructImpl (lucee.runtime.type.StructImpl)18 MalformedURLException (java.net.MalformedURLException)17 PageContextImpl (lucee.runtime.PageContextImpl)17 PageSource (lucee.runtime.PageSource)16 FileResource (lucee.commons.io.res.type.file.FileResource)15 SecurityException (lucee.runtime.exp.SecurityException)15 BundleException (org.osgi.framework.BundleException)15 ZipEntry (java.util.zip.ZipEntry)13 ExtensionResourceFilter (lucee.commons.io.res.filter.ExtensionResourceFilter)13 Array (lucee.runtime.type.Array)13 File (java.io.File)12