Commit f93f94fe authored by yanzg's avatar yanzg

文件处理

parent 80c7d50e
...@@ -18,4 +18,12 @@ public interface YzgFileService { ...@@ -18,4 +18,12 @@ public interface YzgFileService {
*/ */
YzgFileUploadResVo upload(YzgFileUploadReqVo req); YzgFileUploadResVo upload(YzgFileUploadReqVo req);
/**
* 立即删除临时路径
*
* @param tempFolder
*/
void removeTempFolder(String tempFolder);
} }
package com.yanzuoguang.cloud.file;
import com.rabbitmq.client.Channel;
import com.yanzuoguang.mq.service.MqService;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.log.Log;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* 文件处理
*/
@Component
public class YzgFileConsumer implements InitializingBean {
@Autowired
private MqService mqService;
@Autowired
private YzgFileService fileService;
@Autowired
private YzgFileProcedure fileProcedure;
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
*
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
@Override
public void afterPropertiesSet() throws Exception {
}
/**
* 删除临时木经
*
* @param tempFolder
* @param message
* @param channel
*/
@RabbitListener(queues = {YzgFileProcedure.REMOVE_TEMP_FOLDER})
public void notesCheckBack(String tempFolder, Message message, Channel channel) {
try {
fileService.removeTempFolder(tempFolder);
} catch (CodeException ex) {
Log.error(YzgFileConsumer.class, ex);
} catch (Exception ex) {
Log.error(YzgFileConsumer.class, ex);
fileProcedure.removeFolder(tempFolder);
} finally {
mqService.basicAck(message, channel);
}
}
}
...@@ -3,6 +3,7 @@ package com.yanzuoguang.cloud.file; ...@@ -3,6 +3,7 @@ package com.yanzuoguang.cloud.file;
import com.yanzuoguang.cloud.file.vo.YzgFileUploadItemResVo; import com.yanzuoguang.cloud.file.vo.YzgFileUploadItemResVo;
import com.yanzuoguang.cloud.file.vo.YzgFileUploadReqVo; import com.yanzuoguang.cloud.file.vo.YzgFileUploadReqVo;
import com.yanzuoguang.cloud.file.vo.YzgFileUploadResVo; import com.yanzuoguang.cloud.file.vo.YzgFileUploadResVo;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.exception.CodeException; import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.DateHelper; import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.FileHelper; import com.yanzuoguang.util.helper.FileHelper;
...@@ -28,6 +29,9 @@ public class YzgFileServiceImpl implements YzgFileService { ...@@ -28,6 +29,9 @@ public class YzgFileServiceImpl implements YzgFileService {
@Autowired @Autowired
private YzgFileProcedure procedure; private YzgFileProcedure procedure;
@Autowired
private MemoryCache<Boolean> cacheRemoveTempFolder = new MemoryCache<>(10 * 60);
/** /**
* 压缩文件 * 压缩文件
* *
...@@ -81,6 +85,36 @@ public class YzgFileServiceImpl implements YzgFileService { ...@@ -81,6 +85,36 @@ public class YzgFileServiceImpl implements YzgFileService {
return res; return res;
} }
/**
* 立即删除临时路径
*
* @param tempFolder
*/
@Override
public void removeTempFolder(String tempFolder) {
// 设置标记
synchronized (cacheRemoveTempFolder) {
if (StringHelper.toBoolean(cacheRemoveTempFolder.get(tempFolder))) {
return;
}
cacheRemoveTempFolder.put(tempFolder, true);
}
try {
// 删除当前临时目录
FileHelper.deleteFolder(tempFolder);
// 父文件夹不存在子文件时则删除
File file = new File(tempFolder);
File parentFile = file.getParentFile();
if (parentFile.listFiles() == null || parentFile.listFiles().length == 0) {
removeTempFolder(parentFile.getAbsolutePath());
}
} finally {
// 设置已经删除完成
cacheRemoveTempFolder.remove(tempFolder);
}
}
/** /**
* 获取行记的显示的图片 * 获取行记的显示的图片
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment