zhijiezhao 2 年之前
父節點
當前提交
2099dc41b6

+ 5 - 0
src/main/java/com/caimei/config/FastDfsClient.java

@@ -47,4 +47,9 @@ public class FastDfsClient {
         outputStream.close();
         return true;
     }
+
+    public void deleteFile(String storagePath) {
+        String filepath = storagePath.substring(storagePath.lastIndexOf("group1/") + 7);
+        storageClient.deleteFile("group1", filepath);
+    }
 }

+ 4 - 0
src/main/java/com/caimei/mapper/cmMapper/FileMapper.java

@@ -76,4 +76,8 @@ public interface FileMapper {
     String findShopPrefix(Integer authUserId);
 
     void insertImgUrl(FileTreeVo fileTreeVo);
+
+    void updateFileByArticleId(FileTreeVo fileTreeVo);
+
+    FileTreeVo findFileByArticleId(Integer id);
 }

+ 60 - 32
src/main/java/com/caimei/service/async/AsyncService.java

@@ -1,13 +1,16 @@
 package com.caimei.service.async;
 
 import com.caimei.config.FastDfsClient;
+import com.caimei.mapper.cmMapper.ArticleMapper;
 import com.caimei.mapper.cmMapper.FileMapper;
 import com.caimei.model.po.ArticlePo;
+import com.caimei.model.vo.ArticleFormVo;
 import com.caimei.model.vo.FileTreeVo;
 import com.caimei.utils.FileZipUtils;
 import com.caimei.utils.OSSUtils;
 import lombok.extern.slf4j.Slf4j;
 import net.coobird.thumbnailator.Thumbnails;
+import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.scheduling.annotation.Async;
@@ -44,44 +47,69 @@ public class AsyncService {
     @Resource
     private FastDfsClient client;
 
+    @Resource
+    private ArticleMapper articleMapper;
+
     @Async("taskExecutor")
     public void saveArticleImage(boolean insertFlag, ArticlePo article, Integer parentId) {
+        //保存文章相关属性
+        FileTreeVo fileTreeVo = new FileTreeVo();
+        fileTreeVo.setPackageType(1);
+        fileTreeVo.setMime("text/plain");
+        fileTreeVo.setFileName(article.getTitle() + ".txt");
+        fileTreeVo.setFileType("article");
+        fileTreeVo.setParentId(parentId);
+        fileTreeVo.setAuthUserId(article.getAuthUserId());
+        fileTreeVo.setArticleId(article.getId());
+        UUID uuid = UUID.randomUUID();
+        fileTreeVo.setOssName(uuid + ".txt");
+        //http://192.168.2.92:8888/:authUserId/:prefix/docs/article-detail?fileId=fileId
+        String prefix = fileMapper.findShopPrefix(article.getAuthUserId());
+        prefix = null == prefix ? "app" : prefix;
+        String path = "";
+        path = "/" + article.getAuthUserId() + "/" + prefix + "/docs/article-detail?articleId=" + article.getId();
+        if ("dev".equals(active)) {
+            path = "http://192.168.2.92:8888" + path;
+        } else {
+            path = zpapi + path;
+        }
+        if (2 == article.getArticleType()) {
+            path = article.getArticleLink();
+        }
+        fileTreeVo.setOssUrl(path);
+        //创建txt文件保存链接上传到阿里云
+        String delPath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempFile/" + uuid;
+        String filePath = delPath + "/";
+        if ("dev".equals(active)) {
+            delPath = "D:\\caimei-workSpace\\file\\" + uuid;
+            filePath = delPath + "\\";
+        }
+        File file = writeTxt(filePath, path);
+        OSSUtils.ossUpload(fileTreeVo.getOssName(), file, "text/plain", null);
+        try {
+            client.downloadFile(article.getImage(), filePath + "myShotImage.jpg");
+            log.info("下载成功???");
+            Thumbnails.of(filePath + "myShotImage.jpg").size(64, 64).toFile(filePath + "NewMyShotImage.jpg");
+            String imgUrl = imageDomain + "/" + client.uploadFile(filePath + "NewMyShotImage.jpg");
+            fileTreeVo.setScreenshot(imgUrl);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
         if (insertFlag) {
-            //保存文章相关属性
-            FileTreeVo fileTreeVo = new FileTreeVo();
-            fileTreeVo.setPackageType(1);
-            fileTreeVo.setMime("text/plain");
-            fileTreeVo.setFileName(article.getTitle());
-            fileTreeVo.setFileType("article");
-            fileTreeVo.setParentId(parentId);
-            fileTreeVo.setAuthUserId(article.getAuthUserId());
-            fileTreeVo.setArticleId(article.getId());
-            UUID uuid = UUID.randomUUID();
-            fileTreeVo.setOssName(uuid + ".txt");
-            //http://192.168.2.92:8888/:authUserId/:prefix/docs/article-detail?fileId=fileId
-            String prefix = fileMapper.findShopPrefix(article.getAuthUserId());
-            prefix = null == prefix ? "app" : prefix;
-            String path = "/" + article.getAuthUserId() + "/" + prefix + "/docs/article-detail?articleId=" + article.getId();
-            if ("dev".equals(active)) {
-                path = "http://192.168.2.92:8888" + path;
-            } else {
-                path = zpapi + path;
-            }
-            fileTreeVo.setOssUrl(path);
             fileMapper.insertNewFile(fileTreeVo);
-            //创建txt文件保存链接上传到阿里云
-            String delPath = "/mnt/newdatadrive/data/runtime/jar-instance/zplma/tempFile/" + uuid;
-            String filePath = delPath + "/";
-            if ("dev".equals(active)) {
-                delPath = "D:\\caimei-workSpace\\file\\" + uuid;
-                filePath = delPath + "\\";
+        } else {
+            FileTreeVo fil = fileMapper.findFileByArticleId(article.getId());
+            if (StringUtils.isNotBlank(fil.getScreenshot())) {
+                client.deleteFile(fil.getScreenshot());
             }
-            File file = writeTxt(filePath, path);
-            OSSUtils.ossUpload(fileTreeVo.getOssName(), file, "text/plain", null);
-            File fileDir = new File(delPath);
-            Boolean aBoolean = FileZipUtils.deleteFile(fileDir);
-            log.info("临时txt删除------------>" + aBoolean);
+            if (StringUtils.isNotBlank(fil.getOssName())) {
+                OSSUtils.deleteSingleFile(fil.getOssName());
+            }
+            fileMapper.updateFileByArticleId(fileTreeVo);
         }
+        File fileDir = new File(delPath);
+        Boolean aBoolean = FileZipUtils.deleteFile(fileDir);
+        log.info("临时txt删除------------>" + aBoolean);
     }
 
     private File writeTxt(String filePath, String content) {

+ 8 - 5
src/main/java/com/caimei/service/auth/impl/ArticleServiceImpl.java

@@ -1,5 +1,6 @@
 package com.caimei.service.auth.impl;
 
+import com.caimei.config.FastDfsClient;
 import com.caimei.mapper.cmMapper.ArticleMapper;
 import com.caimei.mapper.cmMapper.AuthMapper;
 import com.caimei.mapper.cmMapper.FileMapper;
@@ -43,6 +44,9 @@ public class ArticleServiceImpl implements ArticleService {
     @Resource
     private AsyncService asyncService;
 
+    @Resource
+    private FastDfsClient fastDfsClient;
+
     @Override
     public ResponseJson<PageInfo<ArticleListVo>> getArticleList(Integer listType, Integer authUserId, String articleTitle, Integer auditStatus, Integer status, Integer pageNum, Integer pageSize) {
         listType = null == listType ? 1 : listType;
@@ -72,9 +76,6 @@ public class ArticleServiceImpl implements ArticleService {
         if (2 == articleType && StringUtils.isBlank(articleLink)) {
             return ResponseJson.error("参数异常,请输入文章链接");
         }
-//        if (null == sort) {
-//            return ResponseJson.error("排序值不能为空");
-//        }
         boolean insertFlag = null == articleId;
         ArticleFormVo dbArticle = null;
         if (!insertFlag) {
@@ -89,7 +90,6 @@ public class ArticleServiceImpl implements ArticleService {
         article.setContent(articleContent);
         article.setArticleLink(articleLink);
         article.setArticleType(articleType);
-//        article.setSort(sort);
         // 供应商保存,直接上线;机构保存,需要供应商审核通过后才上线
         if (null != dbArticle && 1 != dbArticle.getAuditStatus()) {
             // 被驳回的数据,编辑后变为待审核状态
@@ -108,10 +108,13 @@ public class ArticleServiceImpl implements ArticleService {
             article.setCreateTime(new Date());
             // 插入文章
             articleMapper.insertArticle(article);
-            log.info("返回id------------》" + article.getId());
         } else {
             article.setId(articleId);
             // 更新文章
+            ArticleFormVo articleForm = articleMapper.getArticleForm(article.getId());
+            if (StringUtils.isNotBlank(articleForm.getArticleImage())) {
+                fastDfsClient.deleteFile(articleForm.getArticleImage());
+            }
             articleMapper.updateArticleByArticleId(article);
         }
         asyncService.saveArticleImage(insertFlag, article, parentId);

+ 37 - 2
src/main/resources/mapper/FileMapper.xml

@@ -19,9 +19,9 @@
     <insert id="insertNewFile" keyColumn="id" keyProperty="id" parameterType="com.caimei.model.vo.FileTreeVo"
             useGeneratedKeys="true">
         insert into cm_tree_file(parentId, fileType, saveTime, ossUrl, fileName, ossName, fileSize, packageType,
-                                 authUserId, mime, articleId)
+                                 authUserId, mime, articleId, screenshot)
         values (#{parentId}, #{fileType}, now(), #{ossUrl}, #{fileName}, #{ossName}, #{fileSize}, #{packageType},
-                #{authUserId}, #{mime}, #{articleId})
+                #{authUserId}, #{mime}, #{articleId}, #{screenshot})
     </insert>
     <insert id="insertImgUrl">
         update cm_tree_file
@@ -117,6 +117,24 @@
         </set>
         where id = #{id}
     </update>
+    <update id="updateFileByArticleId">
+        update cm_tree_file
+        <set>
+            <if test="null != ossName and ossName != ''">
+                ossName=#{ossName},
+            </if>
+            <if test="null != fileName and fileName != ''">
+                fileName=#{fileName},
+            </if>
+            <if test="null != ossUrl and ossUrl != ''">
+                ossUrl=#{ossUrl},
+            </if>
+            <if test="null != screenshot and screenshot != ''">
+                screenshot=#{screenshot}
+            </if>
+        </set>
+        where articleId = #{articleId}
+    </update>
     <delete id="deleteFileByFileId">
         delete
         from cm_brand_file
@@ -289,4 +307,21 @@
         from cm_brand_auth_user
         where authUserId = #{authUserId}
     </select>
+    <select id="findFileByArticleId" resultType="com.caimei.model.vo.FileTreeVo">
+        select id,
+               parentId,
+               fileType,
+               fileName,
+               ossUrl,
+               ossName,
+               fileSize,
+               saveTime,
+               packageType,
+               authUserId,
+               mime,
+               articleId,
+               screenshot
+        from cm_tree_file
+        where articleId = #{id}
+    </select>
 </mapper>