Search in sources :

Example 1 with UploadHelper

use of app.hongs.action.UploadHelper in project HongsCORE by ihongs.

the class IsFile method verify.

@Override
public Object verify(Object value) throws Wrong {
    if (value == null || "".equals(value)) {
        // 允许为空
        return null;
    }
    if (value instanceof String) {
        // 跳过资源路径
        if (Synt.declare(params.get("pass-source"), false)) {
            String u = value.toString();
            if (u.indexOf('/') != -1) {
                return value;
            }
        }
        // 跳过远程地址
        if (Synt.declare(params.get("pass-remote"), false)) {
            String u = value.toString();
            if (u.matches("^\\w+://.*")) {
                return value;
            }
        }
        // 下载远程文件
        if (Synt.declare(params.get("down-remote"), false)) {
            String u = value.toString();
            if (u.matches("^\\w+://.*")) {
                do {
                    String x;
                    // 如果是本地路径则不再下载
                    x = (String) params.get("href");
                    if (x != null && !"".equals(x)) {
                        if (u.startsWith(x)) {
                            value = u;
                            break;
                        }
                    }
                    // 如果有临时目录则下载到这
                    x = (String) params.get("temp");
                    if (x == null || "".equals(x)) {
                        x = Core.DATA_PATH + File.separator + "tmp";
                    }
                    value = stores(value.toString(), x);
                } while (false);
            }
        }
    }
    // End If
    UploadHelper hlpr = new UploadHelper();
    String href, path;
    String x;
    String[] y;
    x = (String) params.get("temp");
    if (x != null)
        hlpr.setUploadTemp(x);
    x = (String) params.get("path");
    if (x != null)
        hlpr.setUploadPath(x);
    x = (String) params.get("href");
    if (x != null)
        hlpr.setUploadHref(x);
    y = (String[]) Synt.toArray(params.get("type"));
    if (y != null)
        hlpr.setAllowTypes(y);
    y = (String[]) Synt.toArray(params.get("extn"));
    if (y != null)
        hlpr.setAllowExtns(y);
    if (value instanceof Part) {
        hlpr.upload((Part) value);
    } else if (value instanceof File) {
        hlpr.upload((File) value);
    } else {
        hlpr.upload(value.toString());
    }
    href = hlpr.getResultHref();
    path = hlpr.getResultPath();
    /**
     * 检查新上传的文件
     * 可以返回原始路径
     * 或者抛弃原始文件
     */
    if (!href.equals(value)) {
        x = checks(href, path);
        if (!href.equals(x)) {
            if (Synt.declare(params.get("keep-origin"), false)) {
            // Keep to return origin href
            } else if (Synt.declare(params.get("drop-origin"), false)) {
                new File(path).delete();
                href = x;
            } else {
                href = x;
            }
        }
    }
    return href;
}
Also used : Part(javax.servlet.http.Part) UploadHelper(app.hongs.action.UploadHelper) File(java.io.File)

Example 2 with UploadHelper

use of app.hongs.action.UploadHelper in project HongsCORE by ihongs.

the class FileAction method create.

@Action("create")
public void create(ActionHelper helper) throws HongsException {
    Part prt = (Part) helper.getRequestData().get("file");
    String uid = (String) helper.getSessibute(Cnst.UID_SES);
    String ext = prt.getSubmittedFileName();
    String fid = Core.newIdentity();
    int pos = ext.lastIndexOf('.');
    if (pos > -1) {
        ext = ext.substring(pos + 1);
    } else {
        ext = "";
    }
    // 传到临时目录
    UploadHelper uh = new UploadHelper();
    uh.setUploadPath("static/upload/temp");
    uh.setUploadHref("static/upload/temp");
    String name = uid + "-" + fid + "." + ext;
    String href = uh.getResultHref();
    uh.upload(prt, name);
    // 组织绝对路径
    HttpServletRequest sr = helper.getRequest();
    String host = sr.getServerName();
    int port = sr.getServerPort();
    if (port != 80 && port != 443) {
        host += ":" + port;
    }
    String link = sr.getScheme() + "://" + host + sr.getContextPath() + "/" + href;
    helper.reply("", Synt.mapOf("name", name, "href", href, "link", link));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Part(javax.servlet.http.Part) UploadHelper(app.hongs.action.UploadHelper) Action(app.hongs.action.anno.Action)

Aggregations

UploadHelper (app.hongs.action.UploadHelper)2 Part (javax.servlet.http.Part)2 Action (app.hongs.action.anno.Action)1 File (java.io.File)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1