Browse Source

impl upload plugin

AE86 4 years ago
parent
commit
2d14ff04ec

+ 8 - 0
dbsyncer-biz/src/main/java/org/dbsyncer/biz/PluginService.java

@@ -20,6 +20,7 @@ public interface PluginService {
 
     /**
      * 获取插件上传路径
+     *
      * @return
      */
     String getPluginPath();
@@ -28,4 +29,11 @@ public interface PluginService {
      * 加载插件
      */
     void loadPlugins();
+
+    /**
+     * 检查文件格式
+     *
+     * @param filename
+     */
+    void checkFileSuffix(String filename);
 }

+ 14 - 0
dbsyncer-biz/src/main/java/org/dbsyncer/biz/impl/PluginServiceImpl.java

@@ -1,8 +1,11 @@
 package org.dbsyncer.biz.impl;
 
+import org.apache.commons.lang.StringUtils;
+import org.dbsyncer.biz.BizException;
 import org.dbsyncer.biz.PluginService;
 import org.dbsyncer.manager.Manager;
 import org.dbsyncer.plugin.config.Plugin;
+import org.dbsyncer.plugin.enums.FileSuffixEnum;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 
@@ -33,4 +36,15 @@ public class PluginServiceImpl implements PluginService {
     public void loadPlugins() {
         manager.loadPlugins();
     }
+
+    @Override
+    public void checkFileSuffix(String filename) {
+        if (StringUtils.isNotBlank(filename)) {
+            String suffix = filename.substring(filename.lastIndexOf(".") + 1, filename.length());
+            // 暂支持jar文件
+            if(!FileSuffixEnum.isJar(suffix)){
+                throw new BizException(String.format("不正确的文件扩展名 \"%s\". 只支持 \"jar\" 的文件扩展名.", filename));
+            }
+        }
+    }
 }

+ 43 - 0
dbsyncer-plugin/src/main/java/org/dbsyncer/plugin/enums/FileSuffixEnum.java

@@ -0,0 +1,43 @@
+package org.dbsyncer.plugin.enums;
+
+import org.apache.commons.lang.StringUtils;
+
+/**
+ * 文件格式
+ *
+ * @author AE86
+ * @version 1.0.0
+ * @date 2021/2/23 23:21
+ */
+public enum FileSuffixEnum {
+
+    /**
+     * jar
+     */
+    JAR("jar"),
+    /**
+     * zip
+     */
+    ZIP("zip");
+
+    private String name;
+
+    FileSuffixEnum(String name) {
+        this.name = name;
+    }
+
+    /**
+     * 是否jar格式
+     *
+     * @param suffix
+     * @return
+     */
+    public static boolean isJar(String suffix) {
+        return StringUtils.equals(JAR.name, suffix);
+    }
+
+    public String getName() {
+        return name;
+    }
+
+}

+ 3 - 3
dbsyncer-web/src/main/java/org/dbsyncer/web/controller/upload/UploadController.java

@@ -9,11 +9,9 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.ModelMap;
 import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.multipart.MultipartFile;
 
-import javax.servlet.http.HttpServletRequest;
 import java.io.File;
 
 @Controller
@@ -41,7 +39,9 @@ public class UploadController {
                 for (int i = 0; i < files.length; i++) {
                     file = files[i];
                     if (file != null) {
-                        File dest = new File(filePath + file.getOriginalFilename());
+                        String filename = file.getOriginalFilename();
+                        pluginService.checkFileSuffix(filename);
+                        File dest = new File(filePath + filename);
                         FileUtils.copyInputStreamToFile(file.getInputStream(), dest);
                     }
                 }

+ 3 - 1
dbsyncer-web/src/main/resources/public/connector/add.html

@@ -7,7 +7,9 @@
         <form id="connectorAddForm" class="form-horizontal" role="form" method="post">
             <!-- 标题 -->
             <div class="row text-center">
-                <h3>添加连接器</h3>
+                <div class="page-header">
+                    <h3>添加连接器</h3>
+                </div>
             </div>
 
             <!-- 操作 -->

+ 3 - 1
dbsyncer-web/src/main/resources/public/connector/edit.html

@@ -7,7 +7,9 @@
         <form id="connectorModifyForm" class="form-horizontal" role="form" method="post">
             <!-- 标题 -->
             <div class="row text-center">
-                <h3>修改连接器</h3>
+                <div class="page-header">
+                    <h3>修改连接器</h3>
+                </div>
             </div>
 
             <!-- 操作 -->

+ 1 - 1
dbsyncer-web/src/main/resources/public/index.html

@@ -13,11 +13,11 @@
     <meta name="author" content="AE86">
     <title>dbsyncer</title>
     <link type="image/x-icon" rel="icon" th:href="@{/img/sync.ico}"/>
+    <link type="text/css" rel="stylesheet" th:href="@{/plugins/css/font-awesome.min.css}"/>
     <link type="text/css" rel="stylesheet" th:href="@{/plugins/css/bootstrap/bootstrap.min.css}"/>
     <link type="text/css" rel="stylesheet" th:href="@{/plugins/css/bootstrap-dialog/bootstrap-dialog.min.css}"/>
     <link type="text/css" rel="stylesheet" th:href="@{/plugins/css/bootstrap-fileinput/fileinput.min.css}"/>
     <link type="text/css" rel="stylesheet" th:href="@{/plugins/css/icheck/all.css}"/>
-    <link type="text/css" rel="stylesheet" th:href="@{/plugins/css/font-awesome.min.css}"/>
     <link type="text/css" rel="stylesheet" th:href="@{/plugins/css/loading-plus/loading-plus.css}"/>
     <link type="text/css" rel="stylesheet" th:href="@{/plugins/css/select2/select2.min.css}"/>
     <link type="text/css" rel="stylesheet" th:href="@{/css/common.css}">

+ 3 - 1
dbsyncer-web/src/main/resources/public/pwd/pwd.html

@@ -5,7 +5,9 @@
 <div class="container">
     <form id="configEditForm" class="form-horizontal" role="form">
         <div class="row text-center">
-            <h3>修改密码</h3>
+            <div class="page-header">
+                <h3>修改密码</h3>
+            </div>
         </div>
 
         <!-- 操作 -->

+ 3 - 1
dbsyncer-web/src/main/resources/public/system/system.html

@@ -5,7 +5,9 @@
 <div class="container">
     <form id="configEditForm" class="form-horizontal" role="form">
         <div class="row text-center">
-            <h3>系统参数</h3>
+            <div class="page-header">
+                <h3>系统参数</h3>
+            </div>
         </div>
 
         <!-- 操作 -->

+ 21 - 48
dbsyncer-web/src/main/resources/public/upload/upload.html

@@ -1,67 +1,40 @@
 <!DOCTYPE html>
 <html xmlns="http://www.w3.org/1999/xhtml"
-      lang="zh-CN">
+      xmlns:th="http://www.thymeleaf.org" lang="zh-CN">
 
 <div class="container">
     <form id="uploadForm" class="form-horizontal" role="form">
-        <div class="row text-center">
-            <h3>上传插件</h3>
-        </div>
 
-        <div class="form-group">
-            <label class="col-sm-2 control-label">插件 <strong class="driverVerifcateRequired">*</strong></label>
-            <div class="col-sm-10">
-                <input id="formFile" type="file" accept="application/zip" name="file" dbsyncer-valid="require" multiple="multiple" />
-            </div>
-        </div>
-
-        <div class="form-group">
-            <label class="col-sm-2 control-label"></label>
-            <div class="col-sm-10">
-                <button id="subBtn" type="button" class="btn btn-default">
-                    <span class="fa fa-arrow-circle-o-up"></span>上传
-                </button>
-            </div>
+        <div class="page-header">
+            <h3>上传插件 <small>只支持 "jar" 的文件扩展名.</small></h3>
         </div>
 
         <div class="form-group">
             <div class="file-loading">
-                <input id="file-5" class="file" type="file" multiple data-preview-file-type="any" data-upload-url="#" data-theme="fas">
+                <input id="filePlugin" type="file" name="files" multiple="multiple" />
             </div>
         </div>
+
     </form>
 </div>
 
 <script type="text/javascript">
-    $("#subBtn").click(function(){
-        var $form = $("#uploadForm");
-        if (!$form.formValidate()) {
-            return;
+    $("#filePlugin").fileinput({
+        theme: 'fas',
+        language: 'zh',
+        uploadUrl: '/upload/upload',
+        enctype: 'multipart/form-data',
+        removeFromPreviewOnError:true, //当选择的文件不符合规则时,例如不是指定后缀文件、大小超出配置等,选择的文件不会出现在预览框中,只会显示错误信息
+        <!--allowedFileExtensions: ['jar'],-->
+        minFileCount: 0, //每次多次上载允许的最小文件数。如果设置为0,则表示文件数是可选的
+        maxFileCount: 5, //表示允许同时上传的最大文件个数 如果设置为0,则表示允许的文件数不受限制
+        showPreview: true,
+        showUpload:true,//不展示上传按钮
+        validateInitialCount:true,//是否在验证minFileCount和包含初始预览文件计数(服务器上载文件)maxFileCount
+    }).on("fileuploaded", function(event, data, previewId, index) {
+        if (!data.response.success) {
+            bootGrowl(data.response.resultValue, "danger");
         }
-
-        var data = new FormData();
-        // 获取文件框中的数据
-        var files = document.getElementById("formFile").files[0];
-        console.log(files);
-        // 将文件数据添加至表单数据对象中
-        data.append("files", files);
-        $.ajax({
-            url:'/upload/upload',
-            type:'POST',
-            data: data,
-            processData : false, // 使数据不做处理
-            contentType : false, // 不要设置Content-Type请求头
-            mimeType : 'multipart/form-data',
-            dataType: 'json',
-            success: function(data){
-                console.log(data);
-                if (data.success == true) {
-                    bootGrowl(data.resultValue, "success");
-                } else {
-                    bootGrowl(data.resultValue, "danger");
-                }
-            }
-        });
-    })
+    });
 </script>
 </html>

+ 1 - 1
dbsyncer-web/src/main/resources/static/plugins/js/bootstrap-fileinput/theme.min.js

@@ -9,4 +9,4 @@
  *
  * Licensed under the BSD-3-Clause
  * https://github.com/kartik-v/bootstrap-fileinput/blob/master/LICENSE.md
- */!function(a){"use strict";a.fn.fileinputThemes.fas={fileActionSettings:{removeIcon:'<i class="fas fa-trash-alt"></i>',uploadIcon:'<i class="fas fa-upload"></i>',uploadRetryIcon:'<i class="fas fa-redo-alt"></i>',downloadIcon:'<i class="fas fa-download"></i>',zoomIcon:'<i class="fas fa-search-plus"></i>',dragIcon:'<i class="fas fa-arrows-alt"></i>',indicatorNew:'<i class="fas fa-plus-circle text-warning"></i>',indicatorSuccess:'<i class="fas fa-check-circle text-success"></i>',indicatorError:'<i class="fas fa-exclamation-circle text-danger"></i>',indicatorLoading:'<i class="fas fa-hourglass text-muted"></i>',indicatorPaused:'<i class="fa fa-pause text-info"></i>'},layoutTemplates:{fileIcon:'<i class="fas fa-file kv-caption-icon"></i> '},previewZoomButtonIcons:{prev:'<i class="fas fa-caret-left fa-lg"></i>',next:'<i class="fas fa-caret-right fa-lg"></i>',toggleheader:'<i class="fas fa-fw fa-arrows-alt-v"></i>',fullscreen:'<i class="fas fa-fw fa-arrows-alt"></i>',borderless:'<i class="fas fa-fw fa-external-link-alt"></i>',close:'<i class="fas fa-fw fa-times"></i>'},previewFileIcon:'<i class="fas fa-file"></i>',browseIcon:'<i class="fas fa-folder-open"></i>',removeIcon:'<i class="fas fa-trash-alt"></i>',cancelIcon:'<i class="fas fa-ban"></i>',pauseIcon:'<i class="fas fa-pause"></i>',uploadIcon:'<i class="fas fa-upload"></i>',msgValidationErrorIcon:'<i class="fas fa-exclamation-circle"></i> '}}(window.jQuery);
+ */(function($){"use strict";$.fn.fileinputThemes.fas={fileActionSettings:{removeIcon:'<i class="fa fa-trash-o"></i>',uploadIcon:'<i class="fa fa-upload"></i>',uploadRetryIcon:'<i class="fa fa-repeat"></i>',downloadIcon:'<i class="fa fa-download"></i>',zoomIcon:'<i class="fa fa-search-plus"></i>',dragIcon:'<i class="fa fa-arrows"></i>',indicatorNew:'<i class="fa fa-plus-circle text-warning"></i>',indicatorSuccess:'<i class="fa fa-check-circle text-success"></i>',indicatorError:'<i class="fa fa-exclamation-circle text-danger"></i>',indicatorLoading:'<i class="fa fa-hourglass text-muted"></i>',indicatorPaused:'<i class="fa fa-pause text-info"></i>'},layoutTemplates:{fileIcon:'<i class="fa fa-file-archive-o"></i> '},previewZoomButtonIcons:{prev:'<i class="fa fa-caret-left fa-lg"></i>',next:'<i class="fa fa-caret-right fa-lg"></i>',toggleheader:'<i class="fa fa-fw fa-arrows-v"></i>',fullscreen:'<i class="fa fa-fw fa-arrows-alt"></i>',borderless:'<i class="fa fa-fw fa-external-link"></i>',close:'<i class="fa fa-fw fa-times"></i>'},previewFileIcon:'<i class="fa fa-file-archive-o"></i>',browseIcon:'<i class="fa fa-folder-open"></i>',removeIcon:'<i class="fa fa-trash-o"></i>',cancelIcon:'<i class="fa fa-ban"></i>',pauseIcon:'<i class="fa fa-pause"></i>',uploadIcon:'<i class="fa fa-upload"></i>',msgValidationErrorIcon:'<i class="fa fa-exclamation-circle"></i> '}})(window.jQuery);

+ 2 - 2
dbsyncer-web/src/main/resources/static/plugins/js/bootstrap-fileinput/zh.js

@@ -76,7 +76,7 @@
         msgImageResizeException: '调整图像大小时发生错误。<pre>{errors}</pre>',
         msgAjaxError: '{operation} 发生错误. 请重试!',
         msgAjaxProgressError: '{operation} 失败',
-        msgDuplicateFile: 'File "{name}" of same size "{size} KB" has already been selected earlier. Skipping duplicate selection.',
+        msgDuplicateFile: '文件 "{name}"({size})KB已选择',
         msgResumableUploadRetriesExceeded:  'Upload aborted beyond <b>{max}</b> retries for file <b>{file}</b>! Error Details: <pre>{error}</pre>',
         msgPendingTime: '{time} remaining',
         msgCalculatingTime: 'calculating time remaining',
@@ -110,4 +110,4 @@
             close: '关闭当前预览'
         }
     };
-})(window.jQuery);
+})(window.jQuery);