Parcourir la source

update:图床增加修改v1

lvzhiqiang il y a 1 an
Parent
commit
a5c3cf383a

+ 86 - 35
src/main/java/top/lvzhiqiang/controller/CoinController.java

@@ -119,51 +119,102 @@ public class CoinController {
      * @author lvzhiqiang
      * 2023/12/29 10:26
      */
-    @RequestMapping("/uploadImgs")
+    @RequestMapping("/InsertOrUpdateImg")
     @ResponseBody
-    public R uploadImgs(@RequestParam("file") MultipartFile file, String remark, Long categoryId) {
-        if (file == null || file.getSize() == 0) {
+    public R InsertOrUpdateImg(@RequestParam("file") MultipartFile file, String remark, Long categoryId, String id) {
+        if (StringUtils.isEmpty(id) && (file == null || file.getSize() == 0)) {
             throw new ParameterException("文件为空!");
         }
 
+        if (categoryId == null) {
+            throw new ParameterException("categoryId为空!");
+        }
+
         String imageUrl = "";
         String imageSize = "";
-        try {
-            // 1、给上传的图片生成新的文件名
-            // 1.1获取原始文件名
-            String oldName = file.getOriginalFilename();
-            // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀
-            String newName = FtpUtil.genImageName();
-            newName = newName + oldName.substring(oldName.lastIndexOf("."));
-
-            // 2、把图片上传到图片服务器
-            // 2.1获取上传的io流
-            InputStream input = file.getInputStream();
-            // 2.2调用FtpUtil工具类进行上传
-            boolean result = FtpUtil.uploadFile(newName, input);
-            if (result) {
-                //返回给前端图片访问路径
-                imageUrl = LocalDate.now().format(DateUtils.dateFormatter_) + "/" + newName;
-                imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
-
-                FileImage fileImage = new FileImage();
-                fileImage.setOldName(oldName);
-                fileImage.setNewName(newName);
-                fileImage.setSize(imageSize);
-                fileImage.setPath(imageUrl);
+        if (StringUtils.isEmpty(id)) {
+            try {
+                // 1、给上传的图片生成新的文件名
+                // 1.1获取原始文件名
+                String oldName = file.getOriginalFilename();
+                // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀
+                String newName = FtpUtil.genImageName();
+                newName = newName + oldName.substring(oldName.lastIndexOf("."));
+
+                // 2、把图片上传到图片服务器
+                // 2.1获取上传的io流
+                InputStream input = file.getInputStream();
+                // 2.2调用FtpUtil工具类进行上传
+                boolean result = FtpUtil.uploadFile(newName, input);
+                if (result) {
+                    //返回给前端图片访问路径
+                    imageUrl = LocalDate.now().format(DateUtils.dateFormatter_) + "/" + newName;
+                    imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
+
+                    FileImage fileImage = new FileImage();
+                    fileImage.setOldName(oldName);
+                    fileImage.setNewName(newName);
+                    fileImage.setSize(imageSize);
+                    fileImage.setPath(imageUrl);
+                    fileImage.setRemark(remark);
+                    fileImage.setCategoryId(categoryId);
+                    coinMapper.insertFileImage(fileImage);
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+            }
+
+            JSONObject result = new JSONObject();
+            result.put("imageUrl", FtpUtil.getBaseUrl() + imageUrl);
+            result.put("imageSize", imageSize);
+
+            return R.ok().data(result);
+        } else {
+            FileImage fileImage = coinMapper.findFileImageById(Long.valueOf(id));
+            if (fileImage == null) {
+                throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "ID 不存在!");
+            }
+
+            fileImage.setCategoryId(categoryId);
+            if (StringUtils.isNotEmpty(remark)) {
                 fileImage.setRemark(remark);
-                fileImage.setCategoryId(categoryId);
-                coinMapper.insertFileImage(fileImage);
             }
-        } catch (IOException e) {
-            e.printStackTrace();
-        }
 
-        JSONObject result = new JSONObject();
-        result.put("imageUrl", FtpUtil.getBaseUrl() + imageUrl);
-        result.put("imageSize", imageSize);
+            if (file != null && file.getSize() > 0) {
+                try {
+                    String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue();
+                    boolean flag = FtpUtil.delFile(ftpBasePath + fileImage.getPath());
+                    if (flag) {
+                        String oldName = file.getOriginalFilename();
+                        String newName = FtpUtil.genImageName();
+                        newName = newName + oldName.substring(oldName.lastIndexOf("."));
+                        InputStream input = file.getInputStream();
+                        // 2.2调用FtpUtil工具类进行上传
+                        boolean result = FtpUtil.uploadFile(newName, input);
+                        if (result) {
+                            imageUrl = LocalDate.now().format(DateUtils.dateFormatter_) + "/" + newName;
+                            imageSize = BigDecimal.valueOf(file.getSize()).divide(new BigDecimal("1024")).setScale(0, RoundingMode.UP).toPlainString().concat("KB");
+
+                            fileImage.setOldName(oldName);
+                            fileImage.setNewName(newName);
+                            fileImage.setSize(imageSize);
+                            fileImage.setPath(imageUrl);
+                        } else {
+                            throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "上传新文件失败!");
+                        }
+
+                    } else {
+                        throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "删除旧文件失败!");
+                    }
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    return R.error().message(e.getMessage());
+                }
+            }
 
-        return R.ok().data(result);
+            coinMapper.updateFileImage(fileImage);
+            return R.ok().data("success");
+        }
     }
 
     @RequestMapping("/deleteImgs/{imageId}")

+ 3 - 0
src/main/java/top/lvzhiqiang/mapper/CoinMapper.java

@@ -115,6 +115,9 @@ public interface CoinMapper {
             "VALUES (#{oldName}, #{newName}, #{categoryId}, #{size}, #{path}, #{remark}, now(), now())")
     int insertFileImage(FileImage fileImage);
 
+    @Update("update file_image set old_name=#{oldName},new_name=#{newName},category_id=#{categoryId},size=#{size},path=#{path},remark=#{remark},modify_time=now() where id=#{id}")
+    int updateFileImage(FileImage fileImage);
+
     @Select({"<script>" +
             "select a.*,b.category_name from file_image a left join file_image_category b on a.category_id = b.id WHERE a.delete_flag = 1" +
             "<if test=\"keyword != null and keyword != ''\">" +

+ 5 - 2
src/main/resources/static/coin.html

@@ -254,6 +254,7 @@
                 <option value="">--</option>
             </select>
             <select id="apis-quiet-div-image-sortField" style="height: 24px;">
+                <option value="a.modify_time">更新时间</option>
                 <option value="a.create_time">创建时间</option>
                 <option value="a.size">图片大小</option>
             </select>
@@ -362,9 +363,11 @@
     <div style="display: flex;">
         <div style="margin-right:20px;">
             <div class="uploadImgs-loading"><img src='cover/loading.gif'></div>
-            <span class="font">uploadImgs</span>
+            <span class="font">InsertOrUpdateImg</span>
             <span id="uploadImgsAlert" style="margin-left: 10px;font-size: 13px;"></span>
-            <form method="post" action="coin/uploadImgs" enctype="multipart/form-data" onsubmit="return false;" id="uploadImgs">
+            <form method="post" action="coin/InsertOrUpdateImg" enctype="multipart/form-data" onsubmit="return false;" id="uploadImgs">
+                <span>ID</span>
+                <input type="text" name="id" placeholder="可为空"/>
                 <span>分类</span>
                 <select id="apis-quiet-div-uploadImgs-categoryField" style="height: 24px;" name="categoryId">
                     <option value="">--</option>

+ 2 - 2
src/main/resources/static/js/my-coin.js

@@ -400,7 +400,7 @@ function mainSearch(url, nameEn, slideDiv, typetype, needCustomFlag) {
 
                     str += '<tr>';
                     $.each(returnEn, function (index, obj) {
-                        var objStyle = dataDetail.hasOwnProperty(obj + 'Style') && !$.isNull(dataDetail[obj + 'Style']) ? dataDetail[obj + 'Style'] : '';
+                        var objStyle = dataDetail.hasOwnProperty(obj + 'Style') && dataDetail[obj + 'Style'] != null ? dataDetail[obj + 'Style'] : '';
                         var objContent = dataDetail.hasOwnProperty(obj) ? dataDetail[obj] : '--';
                         str += '<td' + objStyle + '>' + objContent + '</td>';
                     });
@@ -585,7 +585,7 @@ function initContentEvent(nameEn) {
 function uploadImgsSubmit(){
     var fromData = new FormData($("#uploadImgs")[0]);
     $.ajax({
-        url: "coin/uploadImgs", //请求的url地址
+        url: "coin/InsertOrUpdateImg", //请求的url地址
         dataType: "json", //返回格式为json
         data: fromData, //参数值
         type: "post", //请求方式