Search in sources :

Example 1 with DateImpl

use of lucee.runtime.type.dt.DateImpl 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 2 with DateImpl

use of lucee.runtime.type.dt.DateImpl in project Lucee by lucee.

the class CreateODBCDate method call.

public static DateTime call(PageContext pc, DateTime datetime, TimeZone tz) {
    if (tz == null)
        tz = ((PageContextImpl) pc).getTimeZone();
    Calendar c = Calendar.getInstance(tz);
    c.setTime(datetime);
    c.set(Calendar.HOUR, 0);
    c.set(Calendar.MINUTE, 0);
    c.set(Calendar.SECOND, 0);
    c.set(Calendar.MILLISECOND, 0);
    return new DateImpl(c.getTime());
}
Also used : Calendar(java.util.Calendar) PageContextImpl(lucee.runtime.PageContextImpl) DateImpl(lucee.runtime.type.dt.DateImpl)

Aggregations

DateImpl (lucee.runtime.type.dt.DateImpl)2 IOException (java.io.IOException)1 Calendar (java.util.Calendar)1 Resource (lucee.commons.io.res.Resource)1 PageContextImpl (lucee.runtime.PageContextImpl)1 ApplicationException (lucee.runtime.exp.ApplicationException)1 Struct (lucee.runtime.type.Struct)1 StructImpl (lucee.runtime.type.StructImpl)1 DateTimeImpl (lucee.runtime.type.dt.DateTimeImpl)1