24282 2 anos atrás
pai
commit
c00a64b957

+ 11 - 0
src/main/java/com/fjhx/controller/EmailMessageController.java

@@ -73,4 +73,15 @@ public class EmailMessageController {
         return R.ok(result);
     }
 
+    /**
+     * 下载附件
+     */
+    @GetMapping("downloadAttachment")
+    public R downloadAttachment(@RequestParam("email") String email,
+                                @RequestParam("attachmentId") String attachmentId,
+                                @RequestParam("path") String path) {
+        emailMessageAttachmentService.downloadAttachment(email, attachmentId, path);
+        return R.ok();
+    }
+
 }

+ 2 - 0
src/main/java/com/fjhx/service/IEmailMessageAttachmentService.java

@@ -17,4 +17,6 @@ public interface IEmailMessageAttachmentService extends BaseService<EmailMessage
 
     List<EmailMessageAttachment> getAttachmentList(String messageId);
 
+    void downloadAttachment(String email, String attachmentId, String path);
+
 }

+ 26 - 0
src/main/java/com/fjhx/service/impl/EmailMessageAttachmentServiceImpl.java

@@ -1,12 +1,19 @@
 package com.fjhx.service.impl;
 
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.fjhx.config.TaskPoolConfig;
+import com.fjhx.config.exception.ServiceException;
 import com.fjhx.entity.EmailMessageAttachment;
 import com.fjhx.mapper.EmailMessageAttachmentMapper;
 import com.fjhx.service.IEmailMessageAttachmentService;
+import com.fjhx.utils.EmailEngineUtil;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.stereotype.Service;
 
+import java.io.FileNotFoundException;
 import java.util.List;
+import java.util.concurrent.Executor;
 
 /**
  * <p>
@@ -19,9 +26,28 @@ import java.util.List;
 @Service
 public class EmailMessageAttachmentServiceImpl extends ServiceImpl<EmailMessageAttachmentMapper, EmailMessageAttachment> implements IEmailMessageAttachmentService {
 
+    @Qualifier(TaskPoolConfig.EXECUTOR)
+    @Autowired
+    private Executor syncExecutor;
+
     @Override
     public List<EmailMessageAttachment> getAttachmentList(String messageId) {
         return list(q -> q.eq(EmailMessageAttachment::getMessageId, messageId));
     }
 
+    @Override
+    public void downloadAttachment(String email, String attachmentId, String path) {
+        try {
+            EmailEngineUtil.downloadAttachment(email, attachmentId, path);
+        } catch (FileNotFoundException e) {
+            throw new ServiceException("下载附件失败");
+        }
+        syncExecutor.execute(() -> {
+            EmailMessageAttachment attachment = new EmailMessageAttachment();
+            attachment.setAttachmentId(attachmentId);
+            attachment.setIsDownload(true);
+            updateById(attachment);
+        });
+    }
+
 }

+ 7 - 1
src/main/java/com/fjhx/utils/EmailEngineUtil.java

@@ -118,7 +118,13 @@ public class EmailEngineUtil {
             Assert.isTrue(mkdir, "创建文件夹失败");
         }
 
-        FileOutputStream fileOutputStream = new FileOutputStream(attachmentPath + fileName);
+        // 附件存在则取消下载
+        String filePath = attachmentPath + fileName;
+        File attachmentFile = new File(filePath);
+        if (attachmentFile.exists()) {
+            return;
+        }
+        FileOutputStream fileOutputStream = new FileOutputStream(filePath);
         download("v1/account/" + email + "/attachment/" + attachmentId, fileOutputStream);
     }