Ver código fonte

update:上传类型pic上传和显示优化v1

tujidelv 1 ano atrás
pai
commit
5bfa6527ec

+ 3 - 3
src/main/java/top/lvzhiqiang/config/MyPicJobs.java

@@ -25,7 +25,7 @@ public class MyPicJobs {
     /**
      * jsoup fuliba
      */
-    @Scheduled(cron = "0 0 9 * * ?", zone = SCHEDULED_ZONE)
+    @Scheduled(cron = "0 0 9,15 * * ?", zone = SCHEDULED_ZONE)
     public void jsoupFulibaPicJob() throws Exception {
         log.warn("jsoupFulibaPicJob开始==============================");
 
@@ -35,7 +35,7 @@ public class MyPicJobs {
     /**
      * jsoup fuliba
      */
-    @Scheduled(cron = "0 0 10 * * ?", zone = SCHEDULED_ZONE)
+    @Scheduled(cron = "0 0 10,16 * * ?", zone = SCHEDULED_ZONE)
     public void jsoupFulibaPicDetailJob1() throws Exception {
         log.warn("jsoupFulibaPicDetailJob1开始==============================");
 
@@ -45,7 +45,7 @@ public class MyPicJobs {
     /**
      * jsoup fuliba
      */
-    @Scheduled(cron = "0 30 9 * * ?", zone = SCHEDULED_ZONE)
+    @Scheduled(cron = "0 30 9,15 * * ?", zone = SCHEDULED_ZONE)
     public void jsoupFulibaPicDetailJob2() throws Exception {
         log.warn("jsoupFulibaPicDetailJob2开始==============================");
 

+ 2 - 2
src/main/java/top/lvzhiqiang/controller/PictureInfoController.java

@@ -46,7 +46,7 @@ public class PictureInfoController {
      */
     @RequestMapping("/insertOrUpdateImg")
     @ResponseBody
-    public R insertOrUpdateImg(@RequestParam("file") MultipartFile file, String remark, Long categoryId, String id) {
+    public R insertOrUpdateImg(@RequestParam("file") MultipartFile file, String remark, String createDate, Long categoryId, String id) {
         if (StringUtils.isEmpty(id) && (file == null || file.getSize() == 0)) {
             throw new ParameterException("文件为空!");
         }
@@ -55,7 +55,7 @@ public class PictureInfoController {
             throw new ParameterException("categoryId为空!");
         }
 
-        return pictureInfoService.insertOrUpdateImg(file, remark, categoryId, id);
+        return pictureInfoService.insertOrUpdateImg(file, remark, createDate, categoryId, id);
     }
 
     @RequestMapping("/deleteImgs/{imageId}")

+ 2 - 2
src/main/java/top/lvzhiqiang/mapper/PictureInfoMapper.java

@@ -39,10 +39,10 @@ public interface PictureInfoMapper {
     List<FileImage> getUploadImageInfoList(Map<String, Object> params);
 
     @Insert("INSERT INTO file_image(old_name, new_name, category_id, size, path, remark, create_time, modify_time) " +
-            "VALUES (#{oldName}, #{newName}, #{categoryId}, #{size}, #{path}, #{remark}, now(), now())")
+            "VALUES (#{oldName}, #{newName}, #{categoryId}, #{size}, #{path}, #{remark}, #{createTime}, 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}")
+    @Update("update file_image set old_name=#{oldName},new_name=#{newName},category_id=#{categoryId},size=#{size},path=#{path},remark=#{remark},create_time=#{createTime},modify_time=now() where id=#{id}")
     int updateFileImage(FileImage fileImage);
 
     @Select({"<script>" +

+ 1 - 3
src/main/java/top/lvzhiqiang/service/PictureInfoService.java

@@ -1,9 +1,7 @@
 package top.lvzhiqiang.service;
 
-import com.github.pagehelper.PageInfo;
 import org.springframework.web.multipart.MultipartFile;
 import top.lvzhiqiang.dto.R;
-import top.lvzhiqiang.entity.FileImage;
 
 import java.time.LocalDate;
 import java.util.Map;
@@ -22,7 +20,7 @@ public interface PictureInfoService {
      */
     Object getPictureInfoPage(Map<String, Object> params);
 
-    R insertOrUpdateImg(MultipartFile file, String remark, Long categoryId, String id);
+    R insertOrUpdateImg(MultipartFile file, String remark, String createDate, Long categoryId, String id);
 
     R deleteImgs(Long imageId);
 

+ 81 - 36
src/main/java/top/lvzhiqiang/service/impl/PictureInfoServiceImpl.java

@@ -41,6 +41,7 @@ import java.net.Proxy;
 import java.net.SocketTimeoutException;
 import java.net.URLDecoder;
 import java.time.LocalDate;
+import java.time.LocalDateTime;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
@@ -112,14 +113,23 @@ public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements P
     }
 
     @Override
-    public R insertOrUpdateImg(MultipartFile file, String remark, Long categoryId, String id) {
+    public R insertOrUpdateImg(MultipartFile file, String remark, String createDate, Long categoryId, String id) {
         String imageUrl = "";
         String imageSize = "";
         String ftpBasePath = InitRunner.dicCodeMap.get("ftp_basepath").getCodeValue();
         String ftpThumbnailBasePath = InitRunner.dicCodeMap.get("ftp_thumbnail_basepath").getCodeValue();
         String ftpBaseUrl = InitRunner.dicCodeMap.get("ftp_baseurl").getCodeValue();
-        String parentPath = LocalDate.now().format(DateUtils.dateFormatter5);
+        List<String> delPathList = new ArrayList<>();
         if (StringUtils.isEmpty(id)) {
+            String parentPath;
+            LocalDateTime createTime = LocalDateTime.now();
+            if (StringUtils.isNotEmpty(createDate)) {
+                createTime = LocalDateTime.parse(createDate, DateUtils.dateTimeFormatter);
+                parentPath = createTime.format(DateUtils.dateFormatter6);
+            } else {
+                parentPath = LocalDate.now().format(DateUtils.dateFormatter6);
+            }
+
             try {
                 // 1、给上传的图片生成新的文件名
                 // 1.1获取原始文件名
@@ -133,13 +143,16 @@ public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements P
                 InputStream input = file.getInputStream();
                 // 2.2调用FtpUtil工具类进行上传
                 boolean result = FtpUtil.uploadFile(ftpBasePath, parentPath, newName, input);
+                delPathList.add(ftpBasePath + parentPath + "/" + newName);
                 // 2.3缩略图
                 BufferedImage originalImage = ImageIO.read(file.getInputStream());
-                BufferedImage thumbnailImage = Thumbnails.of(originalImage).size(300, 200).asBufferedImage();
                 ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
-                ImageIO.write(thumbnailImage, "jpg", thumbnailOutputStream);
+                Thumbnails.of(originalImage).size(300, 200).outputFormat(oldName.substring(oldName.lastIndexOf(".") + 1)).toOutputStream(thumbnailOutputStream);
                 ByteArrayInputStream thumbnailInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
                 FtpUtil.uploadFile(ftpThumbnailBasePath, parentPath, newName, thumbnailInputStream);
+                delPathList.add(ftpThumbnailBasePath + parentPath + "/" + newName);
+                thumbnailOutputStream.close();
+                thumbnailInputStream.close();
                 if (result) {
                     //返回给前端图片访问路径
                     imageUrl = parentPath + "/" + newName;
@@ -152,10 +165,17 @@ public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements P
                     fileImage.setPath(imageUrl);
                     fileImage.setRemark(remark);
                     fileImage.setCategoryId(categoryId);
+                    fileImage.setCreateTime(createTime);
                     pictureInfoMapper.insertFileImage(fileImage);
                 }
-            } catch (IOException e) {
+            } catch (Exception e) {
+                // 异常,删除已经上传的文件
+                if (!delPathList.isEmpty()) {
+                    delPathList.forEach(FtpUtil::delFile);
+                }
+
                 log.error("insertOrUpdateImg Exception,", e);
+                throw new BusinessException(30000, e.getMessage());
             }
 
             JSONObject result = new JSONObject();
@@ -174,46 +194,71 @@ public class PictureInfoServiceImpl extends BaseServiceImpl<Object> implements P
                 fileImage.setRemark(remark);
             }
 
+            String parentPath;
+            if (StringUtils.isNotEmpty(createDate)) {
+                LocalDateTime createTime = LocalDateTime.parse(createDate, DateUtils.dateTimeFormatter);
+                parentPath = createTime.format(DateUtils.dateFormatter6);
+                fileImage.setCreateTime(createTime);
+            } else {
+                parentPath = fileImage.getCreateTime().format(DateUtils.dateFormatter6);
+            }
+
             if (file != null && file.getSize() > 0) {
                 try {
-                    boolean flag = FtpUtil.delFile(ftpBasePath + fileImage.getPath());
-                    FtpUtil.delFile(ftpThumbnailBasePath + 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(ftpBasePath, parentPath, newName, input);
-                        // 2.3缩略图
-                        BufferedImage originalImage = ImageIO.read(file.getInputStream());
-                        BufferedImage thumbnailImage = Thumbnails.of(originalImage).size(300, 200).asBufferedImage();
-                        ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
-                        ImageIO.write(thumbnailImage, "jpg", thumbnailOutputStream);
-                        ByteArrayInputStream thumbnailInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
-                        FtpUtil.uploadFile(ftpThumbnailBasePath, parentPath, newName, thumbnailInputStream);
-                        if (result) {
-                            imageUrl = parentPath + "/" + 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(), "上传新文件失败!");
-                        }
+                    // 1、给上传的图片生成新的文件名
+                    // 1.1获取原始文件名
+                    String oldName = file.getOriginalFilename();
+                    String newName = FtpUtil.genImageName();
+                    // 1.2使用FtpUtil工具类生成新的文件名,新文件名 = newName + 文件后缀
+                    newName = newName + oldName.substring(oldName.lastIndexOf("."));
+                    // 2、把图片上传到图片服务器
+                    // 2.1获取上传的io流
+                    InputStream input = file.getInputStream();
+                    // 2.2调用FtpUtil工具类进行上传
+                    boolean result1 = FtpUtil.uploadFile(ftpBasePath, parentPath, newName, input);
+                    delPathList.add(ftpBasePath + parentPath + "/" + newName);
+                    // 2.3缩略图
+                    BufferedImage originalImage = ImageIO.read(file.getInputStream());
+                    ByteArrayOutputStream thumbnailOutputStream = new ByteArrayOutputStream();
+                    Thumbnails.of(originalImage).size(300, 200).outputFormat(oldName.substring(oldName.lastIndexOf(".") + 1)).toOutputStream(thumbnailOutputStream);
+                    ByteArrayInputStream thumbnailInputStream = new ByteArrayInputStream(thumbnailOutputStream.toByteArray());
+                    boolean result2 = FtpUtil.uploadFile(ftpThumbnailBasePath, parentPath, newName, thumbnailInputStream);
+                    delPathList.add(ftpThumbnailBasePath + parentPath + "/" + newName);
+                    thumbnailOutputStream.close();
+                    thumbnailInputStream.close();
+
+                    if (result2 && result2) {
+                        String oriPath = fileImage.getPath();
+
+                        imageUrl = parentPath + "/" + 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);
+                        pictureInfoMapper.updateFileImage(fileImage);
+
+                        FtpUtil.delFile(ftpBasePath + oriPath);
+                        FtpUtil.delFile(ftpThumbnailBasePath + oriPath);
                     } else {
-                        throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "删除旧文件失败!");
+                        delPathList.forEach(FtpUtil::delFile);
+
+                        throw new BusinessException(ResultCodeEnum.UNKNOWN_ERROR.getCode(), "上传新文件失败!");
                     }
                 } catch (Exception e) {
-                    e.printStackTrace();
-                    return R.error().message(e.getMessage());
+                    // 异常,删除已经上传的文件
+                    if (!delPathList.isEmpty()) {
+                        delPathList.forEach(FtpUtil::delFile);
+                    }
+
+                    log.error("insertOrUpdateImg Exception,", e);
+                    throw new BusinessException(30000, e.getMessage());
                 }
+            } else {
+                pictureInfoMapper.updateFileImage(fileImage);
             }
 
-            pictureInfoMapper.updateFileImage(fileImage);
             return R.ok().data("success");
         }
     }

+ 2 - 0
src/main/java/top/lvzhiqiang/util/DateUtils.java

@@ -41,6 +41,7 @@ public class DateUtils {
     public static final String PATTERN_TO_DAYS4 = "yyyy/M/d";
 
     public static final String PATTERN_TO_DAYS5 = "yyyy/MM/dd";
+    public static final String PATTERN_TO_DAYS6 = "yyyy/MM";
 
     /**
      * (精确到秒的)日期样式
@@ -54,6 +55,7 @@ public class DateUtils {
     public static final DateTimeFormatter dateFormatter3 = DateTimeFormatter.ofPattern(PATTERN_TO_DAYS3);
     public static final DateTimeFormatter dateFormatter4 = DateTimeFormatter.ofPattern(PATTERN_TO_DAYS4);
     public static final DateTimeFormatter dateFormatter5 = DateTimeFormatter.ofPattern(PATTERN_TO_DAYS5);
+    public static final DateTimeFormatter dateFormatter6 = DateTimeFormatter.ofPattern(PATTERN_TO_DAYS6);
 
     public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(PATTERN_TO_SECONDS);
     public static final DateTimeFormatter dateTimeFormatter3 = DateTimeFormatter.ofPattern(PATTERN_TO_MINUTES);

+ 3 - 1
src/main/resources/static/coin.html

@@ -345,8 +345,8 @@
                 <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.modify_time">更新时间</option>
                 <option value="a.size">图片大小</option>
             </select>
             <select id="apis-quiet-div-image-sort" style="height: 24px;">
@@ -490,6 +490,8 @@
                 <select id="apis-quiet-div-uploadImgs-categoryField" style="height: 24px;" name="categoryId">
                     <option value="">--</option>
                 </select>
+                <span>创建日期</span>
+                <input type="text" name="createDate" placeholder="可为空" style="width: 200px;"/>
                 <span>备注</span>
                 <input type="text" name="remark" placeholder="可为空"/>
                 <span>file</span>

+ 14 - 7
src/main/resources/static/js/my-picture.js

@@ -156,23 +156,30 @@ function search(pageNo, startFlag, searchSelectFlag) {
                     var picInfo = picInfoList[i];
 
                     var date = picInfo.createTime;
-                    if (orderField == 'fci.modify_time') {
+                    if (orderField === 'fci.modify_time') {
                         date = picInfo.modifyTime;
                     }
-                    if (bigType == '爬虫') {
+                    if (bigType === '爬虫') {
                         date = picInfo.publishTime;
                     }
 
                     var scoreStr = '';
                     var orginUrl= '';
-                    if (bigType == '上传') {
-                        scoreStr += picInfo.categoryName + '|';
+                    var picTagTop2 = '';
+                    var title = '';
+                    if (bigType === '上传') {
                         scoreStr += picInfo.size + '|';
                         scoreStr += picInfo.modifyTime;
-                    } if (bigType == '爬虫') {
+
+                        picTagTop2 = picInfo.categoryName;
+                        title = picInfo.remark;
+                    } else if (bigType === '爬虫') {
                         scoreStr += picInfo.categoryName + '|';
                         scoreStr += picInfo.size + '|';
                         scoreStr += picInfo.modifyTime;
+
+                        picTagTop2 = picInfo.sort;
+                        title = picInfo.mainTitle;
                     } else {
                         scoreStr = picInfo.size;
                     }
@@ -183,12 +190,12 @@ function search(pageNo, startFlag, searchSelectFlag) {
                         "           <span class=\"bigpreview playvideo play hidden-xs\" imgUrl='" + picInfo.path + "'></span>" +
                         "           <span class=\"pic-tag pic-tag-top\">" +
                         "               <span class=\"tag identificationDate\" title=\"" + orginUrl + "\" style=\"background-color: #00C0FF;\">" + date + "</span>" +
-                        "               <span class=\"tag identificationDate\" title=\"" + orginUrl + "\" style=\"background-color: #00C0FF;\">" + picInfo.sort + "</span>" +
+                        "               <span class=\"tag identificationDate\" title=\"" + orginUrl + "\" style=\"background-color: #00C0FF;\">" + picTagTop2 + "</span>" +
                         "           </span>" +
                         "           <span class=\"pic-text text-right\" title=\"\" \">" + scoreStr + "</span>" +
                         "       </a>" +
                         "       <div class=\"myui-vodlist__detail\">" +
-                        "           <h4 class=\"videodetail title text-overflow\" orginUrl='" + picInfo.path + "'><a title=\" " + picInfo.remark + " \">" + picInfo.mainTitle + "</a></h4>" +
+                        "           <h4 class=\"videodetail title text-overflow\" orginUrl='" + picInfo.path + "'><a title=\" " + picInfo.remark + " \">" + picInfo.remark + "</a></h4>" +
                         "       </div>" +
                         "   </div>" +
                         "</li>";