Skip to content

Commit

Permalink
add .gitignore and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Dec 14, 2017
1 parent 4251e14 commit 618f658
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 35 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,8 @@ tale 博客系统七牛图片上传插件

```bash
mvn clean assembly:assembly
```
```

打包后生成 `target/plugin_qiniu-jar-with-dependencies.jar`

`plugin_qiniu-jar-with-dependencies.jar` 重命名为 `plugin_qiniu.jar` 存储在 `tale/resources/plugins` 目录重启即可。
7 changes: 1 addition & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,7 @@
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<!--这部分可有可无,加上的话则直接生成可运行jar包-->
<!--<archive>-->
<!--<manifest>-->
<!--<mainClass>${exec.mainClass}</mainClass>-->
<!--</manifest>-->
<!--</archive>-->
<finalName>plugin_qiniu</finalName>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/tale/plugins/QiniuConst.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tale.plugins;

import com.blade.kit.StringKit;

/**
* @author biezhi
* @date 2017/12/14
Expand All @@ -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";

}
15 changes: 12 additions & 3 deletions src/main/java/com/tale/plugins/QiniuController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 = "保存设置失败";
Expand Down
22 changes: 2 additions & 20 deletions src/main/java/com/tale/plugins/QiniuWebHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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("执行七牛插件");
Expand All @@ -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<String, FileItem> fileItemMap = request.fileItems();
Collection<FileItem> fileItems = fileItemMap.values();
try {
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions src/main/resources/templates/plugins/qiniu.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,12 @@ <h4 class="page-title">七牛设置</h4>

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');
}
}
Expand Down

0 comments on commit 618f658

Please sign in to comment.