Pārlūkot izejas kodu

临时文件使用临时目录

heavyrian2012 3 gadi atpakaļ
vecāks
revīzija
f14f308182

+ 1 - 0
.gitignore

@@ -9,3 +9,4 @@ application_server.mv.db
 open-server/.idea
 open-server/src/main/resources/static
 .idea
+open_server.mv.db

+ 0 - 3
open-server/config/application.properties

@@ -48,9 +48,6 @@ im.admin_secret=123456
 ## https://docs.wildfirechat.cn/server/media_server.html
 media.server.media_type=1
 
-# 使用这个目录作为临时目录,必须配置有效目录,必现使用绝对路径。
-local.media.temp_storage=/home/ubuntu/open_platform
-
 ## OSS配置,可以是七牛/阿里云OSS/野火私有OSS。
 ## 注意与IM服务的配置格式不太一样,这里是用"Key=Vaue"的格式,IM服务配置里是"Key Value",拷贝粘贴时要注意修改。
 

+ 9 - 6
open-server/src/main/java/cn/wildfirechat/app/ServiceImpl.java

@@ -88,9 +88,6 @@ public class ServiceImpl implements Service {
     @Value("${media.bucket_domain}")
     private String ossBucketDomain;
 
-    @Value("${local.media.temp_storage}")
-    private String ossTempPath;
-
     @Autowired
     private ApplicationEntityRepository applicationEntityRepository;
 
@@ -316,9 +313,14 @@ public class ServiceImpl implements Service {
 
     @Override
     public RestResult uploadMedia(MultipartFile file) throws Exception {
-        String uuid = UUID.randomUUID().toString();
-        String fileName = uuid + "-" + System.currentTimeMillis() + "-" + file.getOriginalFilename();
-        File localFile = new File(ossTempPath, fileName);
+        String fileName = file.getOriginalFilename();
+        String suffix = "";
+        String prefix = fileName;
+        if (fileName.contains(".")) {
+            suffix = fileName.substring(fileName.lastIndexOf("."));
+            prefix = fileName.substring(0, fileName.lastIndexOf("."));
+        }
+        File localFile = File.createTempFile(prefix, suffix);
 
         try {
             file.transferTo(localFile);
@@ -432,6 +434,7 @@ public class ServiceImpl implements Service {
 
         UploadFileResponse response = new UploadFileResponse();
         response.setUrl(url);
+        localFile.delete();
         return RestResult.ok(response);
     }