Search in sources :

Example 1 with UploadHelper

use of io.github.ihongs.action.UploadHelper in project HongsCORE by ihongs.

the class FileAction method create.

@Action("create")
public void create(ActionHelper helper) throws HongsException {
    List list = new ArrayList();
    List fils = Synt.asList(helper.getRequestData().get("file"));
    String uid = Synt.declare(helper.getSessibute(Cnst.UID_SES), "0");
    String nid = Core.newIdentity();
    int idx = 0;
    if (fils == null || fils.size() < 1) {
        helper.reply(Synt.mapOf("ok", false, "ern", "Er400", "err", "file required"));
        return;
    }
    if (fils.size() > 9) {
        helper.reply(Synt.mapOf("ok", false, "ern", "Er400", "err", "up to 9 files"));
        return;
    }
    UploadHelper uh = new UploadHelper();
    uh.setUploadPath("static/upload/tmp");
    uh.setUploadHref("static/upload/tmp");
    for (Object item : fils) {
        if (item instanceof Part) {
            Part part = (Part) item;
            String name = (uid + "-" + nid) + "-" + (idx++) + ".";
            File file = uh.upload(part, name);
            String href = uh.getResultHref();
            String link = Core.SERVER_HREF.get() + Core.SERVER_PATH.get() + "/" + href;
            name = file.getName() + "|" + part.getSubmittedFileName();
            list.add(Synt.mapOf("name", name, "href", href, "link", link));
        }
    }
    helper.reply(Synt.mapOf("list", list));
}
Also used : Part(javax.servlet.http.Part) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) UploadHelper(io.github.ihongs.action.UploadHelper) File(java.io.File) Action(io.github.ihongs.action.anno.Action)

Example 2 with UploadHelper

use of io.github.ihongs.action.UploadHelper in project HongsCORE by ihongs.

the class IsFile method verify.

@Override
public Object verify(Value watch) throws Wrong {
    // 跳过空值和空串
    Object value = watch.get();
    if (value == null) {
        return STAND;
    }
    if (value.equals("")) {
        return STAND;
    }
    if (value instanceof String) {
        // 跳过资源路径
        if (Synt.declare(getParam("pass-source"), false)) {
            String u = value.toString();
            if (u.indexOf('/') != -1) {
                return value;
            }
        }
        // 跳过远程地址
        if (Synt.declare(getParam("pass-remote"), false)) {
            String u = value.toString();
            if (HREF_PATT.matcher(u).matches()) {
                return value;
            }
        }
        // 下载远程文件
        if (Synt.declare(getParam("down-remote"), false)) {
            String u = value.toString();
            if (HREF_PATT.matcher(u).matches()) {
                do {
                    String x;
                    // 如果是本地路径则不再下载
                    x = Core.SERVER_HREF.get() + "/";
                    if (x != null && !"".equals(x)) {
                        if (u.startsWith(x)) {
                            value = u;
                            break;
                        }
                    }
                    x = (String) getParam("href");
                    if (x != null && !"".equals(x)) {
                        if (u.startsWith(x)) {
                            value = u;
                            break;
                        }
                    }
                    // 如果有临时目录则下载到这
                    x = (String) getParam("temp");
                    if (x == null || "".equals(x)) {
                        x = Core.BASE_PATH + "/static/upload/tmp";
                    }
                    value = stores(value.toString(), x);
                    if (value == null) {
                        return null;
                    }
                } while (false);
            }
        }
    }
    // End If
    UploadHelper hlpr = new UploadHelper();
    String href;
    String path;
    Object para;
    String name;
    long size;
    para = getParam("type");
    if (para != null && !"".equals(para))
        hlpr.setAllowTypes(Synt.toSet(para));
    para = getParam("extn");
    if (para != null && !"".equals(para))
        hlpr.setAllowExtns(Synt.toSet(para));
    para = getParam("temp");
    if (para != null && !"".equals(para))
        hlpr.setUploadTemp(Synt.declare(para, String.class));
    para = getParam("path");
    if (para != null && !"".equals(para))
        hlpr.setUploadPath(Synt.declare(para, String.class));
    para = getParam("href");
    if (para != null && !"".equals(para))
        hlpr.setUploadHref(Synt.declare(para, String.class));
    para = getParam("name-digest");
    if (para != null && !"".equals(para))
        hlpr.setDigestType(Synt.declare(para, String.class));
    /**
     * 给当前路径末尾追加记录 ID
     * 以便检测和清理无效文件
     */
    if (Synt.declare(getParam("name-add-id"), false)) {
        String id = Synt.declare(watch.getCleans().get(Cnst.ID_KEY), String.class);
        if (id == null || id.isEmpty()) {
            throw new Wrong(Cnst.ID_KEY + " required for file");
        }
        // 避免单层目录数量过多
        id = Syno.splitPath(id);
        path = Synt.declare(getParam("path"), "static/upload");
        href = Synt.declare(getParam("href"), "static/upload");
        hlpr.setUploadPath(path + "/" + id);
        hlpr.setUploadHref(href + "/" + id);
    }
    String hash = "";
    if (value instanceof Part) {
        Part part = (Part) value;
        name = part.getSubmittedFileName();
        size = part.getSize();
        hlpr.upload(part);
    } else if (value instanceof File) {
        File file = (File) value;
        name = file.getName();
        size = file.length();
        hlpr.upload(file);
    } else {
        href = value.toString();
        name = href;
        /**
         * 井号后为附加选项
         * 竖杆后为真实名称
         * 又斜杠为旧的记录
         * 外部记录的是网址
         * 必须进行解码才行
         */
        int p;
        p = href.indexOf("#");
        if (p != -1) {
            hash = href.substring(p);
            href = href.substring(0, p);
        }
        p = href.indexOf("|");
        if (p != -1) {
            name = href.substring(1 + p);
            href = href.substring(0, p);
        } else if (href.contains("/")) {
            href = decode(href);
            // 不在此返回是因 upload 方法还会做检查
            name = null;
        }
        File file;
        file = hlpr.upload(href);
        size = file.length();
    }
    path = hlpr.getResultPath();
    href = hlpr.getResultHref();
    // 没新上传, 不必检查
    if (null == name) {
        return href + hash;
    }
    // 大小检查, 超限报错
    long max = getParam("size", Long.MAX_VALUE);
    if (size > max) {
        throw new Wrong("core.file.size.invalid", String.valueOf(max));
    }
    /**
     * 检查新上传的文件
     * 可以返回原始路径
     * 或者抛弃原始文件
     */
    String[] hp = checks(href, path);
    if (Synt.declare(getParam("keep-origin"), false)) {
    // Keep to return origin href
    } else if (Synt.declare(getParam("drop-origin"), false)) {
        new File(path).delete();
        href = hp[0];
        path = hp[1];
    } else {
        href = hp[0];
        path = hp[1];
    }
    if (getParam("keep-naming", false)) {
        // 检查外部文件名是否合法
        if (name.getBytes().length > 256) {
            throw new Wrong("fore.file.name.toolong", name);
        }
        if (NAME_PATT.matcher(name).find()) {
            throw new Wrong("fore.file.name.illegal", name);
        }
        try {
            int pos;
            String nick;
            String dist;
            // 新的网址
            pos = href.lastIndexOf('/');
            nick = href.substring(1 + pos);
            href = href.substring(0, pos);
            pos = nick.lastIndexOf('.');
            if (pos <= 0) {
                // 没有扩展名
                href = href + "/" + nick + ".d/" + name;
            } else {
                href = href + "/" + nick.substring(0, pos) + "/" + name;
            }
            // 新的路径
            pos = path.lastIndexOf('/');
            nick = path.substring(1 + pos);
            path = path.substring(0, pos);
            pos = nick.lastIndexOf('.');
            if (pos <= 0) {
                // 没有扩展名
                path = path + "/" + nick + ".d/" + name;
            } else {
                path = path + "/" + nick.substring(0, pos) + "/" + name;
            }
            // 指向上级原始文件
            dist = "../" + nick;
            File d = new File(path).getParentFile();
            if (!d.exists()) {
                d.mkdir();
            }
            Files.createSymbolicLink(Paths.get(path), Paths.get(dist));
        } catch (StringIndexOutOfBoundsException ex) {
            throw new HongsExemption("Wrong path/href setting");
        } catch (IOException ex) {
            throw new HongsExemption(ex);
        }
        /**
         * UploadHelper
         * 不对路径进行转码
         * 故需自行编码处理
         */
        href = encode(href);
    }
    if (getParam("hash-status", false)) {
        /**
         * 附加上原始名称、文件大小
         * 也可能由 checks 附加信息
         * 如图片的宽高, 影音的时长
         */
        name = encode(name);
        href += "#n=" + name;
        href += "&s=" + size;
        if (hp.length > 2) {
            href += "&" + hp[2];
        }
    }
    return href;
}
Also used : Part(javax.servlet.http.Part) HongsExemption(io.github.ihongs.HongsExemption) IOException(java.io.IOException) UploadHelper(io.github.ihongs.action.UploadHelper) File(java.io.File)

Aggregations

UploadHelper (io.github.ihongs.action.UploadHelper)2 File (java.io.File)2 Part (javax.servlet.http.Part)2 HongsExemption (io.github.ihongs.HongsExemption)1 Action (io.github.ihongs.action.anno.Action)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1