use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class IsFile method decode.
/**
* 仅对 URL 的文件名部分进行解码
* @param name
* @return
*/
private String decode(String name) {
String path;
int p = name.lastIndexOf("/");
if (p != -1) {
p += 1;
path = name.substring(0, p);
name = name.substring(p);
} else {
path = "";
}
try {
name = URLDecoder.decode(name, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new HongsExemption(e);
}
// name = name.replace("%20","+");
return path + name;
}
use of io.github.ihongs.HongsExemption 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;
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class AssocCase method allow.
private void allow(Table table, Table assoc, Map ac, String tn, String qn, Map al) {
/**
* 三个变量: 层级名(qn), 名前缀(ax), 表前缀(tx)
* 第一层时, 无需加名前缀, 无关联表前缀也不用加
* 第二层时, 需将表名作为名前缀, 下级需带层级名
*/
String tx, ax;
tx = "`" + tn + "`.";
if (null == qn) {
qn = "";
ax = "";
} else if ("".equals(qn)) {
qn = tn;
ax = qn + ".";
} else {
qn = qn + "." + tn;
ax = qn + ".";
}
try {
Map fs = assoc.getFields();
for (Object n : fs.keySet()) {
String k = (String) n;
String f = (String) n;
k = ax + /**/
k;
f = tx + "`" + f + "`";
al.put(k, f);
}
} catch (HongsException e) {
throw e.toExemption();
}
if (ac == null || ac.isEmpty()) {
return;
}
Iterator it = ac.entrySet().iterator();
while (it.hasNext()) {
Map.Entry et = (Map.Entry) it.next();
Map tc = (Map) et.getValue();
String jn = (String) tc.get("join");
// 不是 JOIN 的不理会
if (!"INNER".equals(jn) && !"LEFT".equals(jn) && !"RIGHT".equals(jn) && !"FULL".equals(jn)) {
continue;
}
tn = (String) et.getKey();
ac = (Map) tc.get("assocs");
// 获取真实的表名, 构建关联表实例
String rn;
rn = (String) tc.get("tableName");
if (rn == null || "".equals(rn)) {
rn = (String) tc.get("name");
}
try {
assoc = table.db.getTable(rn);
} catch (HongsException e) {
throw e.toExemption();
}
if (null == assoc) {
throw new HongsExemption(1026, "Can not get table '" + rn + "' in DB '" + table.db.name + "'");
}
allow(table, assoc, ac, tn, qn, al);
}
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class Capts method captcha.
/**
* 生成验证码
* @param h 图片高(px)
* @param b 背景色
* @param f 前景色
* @return
*/
public static Capts captcha(int h, String b, String f) {
if (h < 24 || h > 96) {
throw new HongsExemption(400, "h must be 24~96 (px)");
}
// 获取配置
CoreConfig cc = CoreConfig.getInstance();
String ff = cc.getProperty("core.capts.font.file", "!Capts.ttf");
String cs = cc.getProperty("core.capts.code.dict", "1234567890");
int cn = cc.getProperty("core.capts.code.count", 4);
int mn = cc.getProperty("core.capts.mask.count", 8);
float sr = cc.getProperty("core.capts.size.ratio", 0.40f);
float fr = cc.getProperty("core.capts.font.ratio", 0.80f);
float mr = cc.getProperty("core.capts.mend.ratio", 0.10f);
float xr = cc.getProperty("core.capts.mask.ratio", 0.05f);
int w = (int) ((float) h * sr * (cn + 1));
char[] cd = cs.toCharArray();
Color bc = "".equals(b) ? new Color(0xffffff, true) : new Color(Integer.parseInt(b, 16));
Color fc = "".equals(f) ? new Color(0x000000, false) : new Color(Integer.parseInt(f, 16));
// 构建实例
Capts vc = new Capts();
vc.setSize(w, h);
vc.setCodeCount(cn);
vc.setMaskCount(mn);
vc.setFontRatio(fr);
vc.setMendRatio(mr);
vc.setMaskRatio(xr);
vc.setBackColor(bc);
vc.setFontColor(fc);
vc.setFontFile(ff);
vc.setCodeDict(cd);
return vc;
}
use of io.github.ihongs.HongsExemption in project HongsCORE by ihongs.
the class Data method getInstance.
/**
* 获取实例
*
* <pre>
* 配置如指定 db-class 则调用类的:
* getInstance(conf, form)
* getInstance()
* new Xxxx ()
* 此类必须是 Data 的子类.
* 存在以上任一方法即返回,
* 首个需自行维护生命周期,
* 后两个交由 Core 作存取.
* </pre>
*
* <pre>
* 错误代码:
* 821 找不到对应的类
* 822 构建方法不可用
* 823 构建实例不成功
* 910 配置文件不存在
* 912 表单信息不存在
* </pre>
*
* @param conf
* @param form
* @return
* @throws HongsException 表单获取失败
* @throws HongsExemption 实例构建失败
* @throws ClassCastException 不是 Data 的子类
*/
public static Data getInstance(String conf, String form) throws HongsException {
// 外部指定
Map dict = FormSet.getInstance(conf).getForm(form);
String name = (String) Dict.get(dict, null, "@", "db-class");
if (name != null && !name.isEmpty() && !name.equals(Data.class.getName())) {
Class type;
try {
type = Class.forName(name);
} catch (ClassNotFoundException e) {
throw new HongsExemption(821, "Can not find class by name '" + name + "'.");
}
try {
Method func = type.getMethod("getInstance", new Class[] { String.class, String.class });
int modi = func.getModifiers();
if (!Modifier.isPublic(modi) || !Modifier.isStatic(modi) || type != func.getDeclaringClass()) {
throw new NoSuchMethodException();
}
return (Data) func.invoke(null, new Object[] { conf, form });
} catch (NoSuchMethodException ex) {
return (Data) Core.getInstance(type);
} catch (InvocationTargetException ex) {
Throwable ta = ex.getCause();
// 调用层级过多, 最好直接抛出
if (ta instanceof StackOverflowError) {
throw (StackOverflowError) ta;
}
throw new HongsExemption(823, "Can not call '" + name + ".getInstance'", ta);
} catch (IllegalArgumentException ex) {
throw new HongsExemption(823, "Can not call '" + name + ".getInstance'", ex);
} catch (IllegalAccessException ex) {
throw new HongsExemption(823, "Can not call '" + name + ".getInstance'", ex);
} catch (SecurityException se) {
throw new HongsExemption(822, "Can not call '" + name + ".getInstance'", se);
}
}
// 默认构造
name = Data.class.getName() + ":" + conf + "!" + form;
Core core = Core.getInstance();
Data inst = (Data) core.get(name);
if (inst == null) {
inst = new Data(conf, form);
core.set(name, inst);
}
return inst;
}
Aggregations