From 618f658f155fb3dd5778f451a6cbcb9075f499d7 Mon Sep 17 00:00:00 2001 From: biezhi Date: Thu, 14 Dec 2017 21:05:31 +0800 Subject: [PATCH] add .gitignore and refactor --- README.md | 6 ++++- pom.xml | 7 +----- .../java/com/tale/plugins/QiniuConst.java | 4 ++++ .../com/tale/plugins/QiniuController.java | 15 ++++++++++--- .../java/com/tale/plugins/QiniuWebHook.java | 22 ++----------------- .../resources/templates/plugins/qiniu.html | 10 ++++----- 6 files changed, 29 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index b86d43d..fc1550c 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,8 @@ tale 博客系统七牛图片上传插件 ```bash mvn clean assembly:assembly -``` \ No newline at end of file +``` + +打包后生成 `target/plugin_qiniu-jar-with-dependencies.jar` + +将 `plugin_qiniu-jar-with-dependencies.jar` 重命名为 `plugin_qiniu.jar` 存储在 `tale/resources/plugins` 目录重启即可。 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 297f465..d7e77b6 100644 --- a/pom.xml +++ b/pom.xml @@ -91,12 +91,7 @@ maven-assembly-plugin - - - - - - + plugin_qiniu jar-with-dependencies diff --git a/src/main/java/com/tale/plugins/QiniuConst.java b/src/main/java/com/tale/plugins/QiniuConst.java index b749895..571cab1 100644 --- a/src/main/java/com/tale/plugins/QiniuConst.java +++ b/src/main/java/com/tale/plugins/QiniuConst.java @@ -1,5 +1,7 @@ package com.tale.plugins; +import com.blade.kit.StringKit; + /** * @author biezhi * @date 2017/12/14 @@ -13,5 +15,7 @@ public interface QiniuConst { String ATTACH_URL = "attach_url"; String SAVE_LOG_ACTION = "保存七牛设置"; String ERROR_MSG = "请确认配置完整"; + String UPLOAD_URI = "/admin/attach/upload"; + String DELETE_URI = "/admin/attach/delete"; } \ No newline at end of file diff --git a/src/main/java/com/tale/plugins/QiniuController.java b/src/main/java/com/tale/plugins/QiniuController.java index e9f5aa2..99ca2ba 100644 --- a/src/main/java/com/tale/plugins/QiniuController.java +++ b/src/main/java/com/tale/plugins/QiniuController.java @@ -10,6 +10,10 @@ import com.blade.mvc.http.HttpMethod; import com.blade.mvc.http.Request; import com.blade.mvc.ui.RestResponse; +import com.qiniu.common.Zone; +import com.qiniu.storage.BucketManager; +import com.qiniu.storage.Configuration; +import com.qiniu.storage.UploadManager; import com.qiniu.util.Auth; import com.tale.controller.BaseController; import com.tale.exception.TipException; @@ -83,11 +87,16 @@ public RestResponse save(Request request) { TaleConst.OPTIONS.addAll(optionsService.getOptions()); - QiniuWebHook.auth = Auth.create(name, pass); - QiniuWebHook.upToken = QiniuWebHook.auth.uploadToken(bucket); + if (StringKit.isNotBlank(name) && StringKit.isNotBlank(pass) && StringKit.isNotBlank(bucket)) { + QiniuWebHook.auth = Auth.create(name, pass); + QiniuWebHook.upToken = QiniuWebHook.auth.uploadToken(bucket); + //构造一个带指定Zone对象的配置类 + Configuration cfg = new Configuration(Zone.autoZone()); + QiniuWebHook.uploadManager = new UploadManager(cfg); + QiniuWebHook.bucketManager = new BucketManager(QiniuWebHook.auth, cfg); + } new Logs(QiniuConst.SAVE_LOG_ACTION, JsonKit.toString(request.parameters()), request.address(), this.getUid()).save(); - return RestResponse.ok(); } catch (Exception e) { String msg = "保存设置失败"; diff --git a/src/main/java/com/tale/plugins/QiniuWebHook.java b/src/main/java/com/tale/plugins/QiniuWebHook.java index d2eb665..5cfd36c 100644 --- a/src/main/java/com/tale/plugins/QiniuWebHook.java +++ b/src/main/java/com/tale/plugins/QiniuWebHook.java @@ -9,9 +9,7 @@ import com.blade.mvc.multipart.FileItem; import com.blade.mvc.ui.RestResponse; import com.google.gson.Gson; -import com.qiniu.common.Zone; import com.qiniu.storage.BucketManager; -import com.qiniu.storage.Configuration; import com.qiniu.storage.UploadManager; import com.qiniu.storage.model.DefaultPutRet; import com.qiniu.util.Auth; @@ -63,19 +61,6 @@ public boolean before(Signature signature) { boolean isActive = TaleConst.OPTIONS.getBoolean(QiniuConst.PLUGIN_KEY_ACTIVE, false); if (!isActive) { return true; - } else { - if (auth == null) { - bucket = TaleConst.OPTIONS.getOrNull(QiniuConst.PLUGIN_KEY_BUCKET_NAME); - String name = TaleConst.OPTIONS.getOrNull(QiniuConst.PLUGIN_KEY_OPERATORNAME); - String pass = TaleConst.OPTIONS.getOrNull(QiniuConst.PLUGIN_KEY_OPERATORPWD); - - auth = Auth.create(name, pass); - upToken = auth.uploadToken(bucket); - //构造一个带指定Zone对象的配置类 - Configuration cfg = new Configuration(Zone.autoZone()); - uploadManager = new UploadManager(cfg); - bucketManager = new BucketManager(auth, cfg); - } } log.info("执行七牛插件"); @@ -85,9 +70,7 @@ public boolean before(Signature signature) { String uri = request.uri(); // 拦截上传接口 - if ("/admin/attach/upload".equals(uri)) { - Users users = TaleUtils.getLoginUser(); - Integer uid = users.getUid(); + if (QiniuConst.UPLOAD_URI.equals(uri)) { Map fileItemMap = request.fileItems(); Collection fileItems = fileItemMap.values(); try { @@ -106,7 +89,7 @@ public boolean before(Signature signature) { } // 删除接口 - if ("/admin/attach/delete".equals(uri)) { + if (QiniuConst.DELETE_URI.equals(uri)) { try { Users users = TaleUtils.getLoginUser(); Integer id = request.queryInt("id", 0); @@ -139,7 +122,6 @@ public boolean before(Signature signature) { private String upload(FileItem fileItem) { Users users = TaleUtils.getLoginUser(); Integer uid = users.getUid(); - String fname = fileItem.getFileName(); if (fileItem.getLength() / 1024 <= TaleConst.MAX_FILE_SIZE) { String fkey = TaleUtils.getFileKey(fname); diff --git a/src/main/resources/templates/plugins/qiniu.html b/src/main/resources/templates/plugins/qiniu.html index 791c7a0..5c44fcc 100644 --- a/src/main/resources/templates/plugins/qiniu.html +++ b/src/main/resources/templates/plugins/qiniu.html @@ -90,12 +90,12 @@

七牛设置

function active_plugin(obj) { var this_ = $(obj); - var on = this_.find('.toggle-on.active').length; - var off = this_.find('.toggle-off.active').length; - if (on == 1) { + var on = this_.attr('on'); + if (on == 'true') { + this_.attr('on', 'false'); $('#plugin_qiniu_active').val('false'); - } - if (off == 1) { + } else { + this_.attr('on', 'true'); $('#plugin_qiniu_active').val('true'); } }