Commit 4cbb6ebe authored by yanzg's avatar yanzg

文件处理

parent e0ce796c
package com.yanzuoguang.cloud.file; package com.yanzuoguang.cloud.file;
import com.yanzuoguang.cloud.file.vo.YzgFileMoveReqVo;
import com.yanzuoguang.mq.service.MqService; import com.yanzuoguang.mq.service.MqService;
import com.yanzuoguang.mq.vo.MessageVo; import com.yanzuoguang.mq.vo.MessageVo;
import com.yanzuoguang.mq.vo.QueueVo; import com.yanzuoguang.mq.vo.QueueVo;
import com.yanzuoguang.util.cache.MemoryCache; import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.DateHelper; import com.yanzuoguang.util.helper.DateHelper;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper; import com.yanzuoguang.util.helper.StringHelper;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -20,7 +22,9 @@ import java.util.Date; ...@@ -20,7 +22,9 @@ import java.util.Date;
@Component @Component
public class YzgFileProcedure implements InitializingBean { public class YzgFileProcedure implements InitializingBean {
public static final String REMOVE_TEMP_FOLDER = "REMOVE_TEMP_FOLDER"; public static final String YZG_REMOVE_TEMP_FOLDER = "YZG_REMOVE_TEMP_FOLDER";
public static final String YZG_MOVE_FILE = "YZG_MOVE_FILE";
@Autowired @Autowired
private MqService mqService; private MqService mqService;
...@@ -42,10 +46,16 @@ public class YzgFileProcedure implements InitializingBean { ...@@ -42,10 +46,16 @@ public class YzgFileProcedure implements InitializingBean {
*/ */
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
mqService.createQueue(new QueueVo(REMOVE_TEMP_FOLDER)); mqService.createQueue(new QueueVo(YZG_REMOVE_TEMP_FOLDER));
mqService.createQueue(new QueueVo(YZG_MOVE_FILE));
} }
public void removeFolder(String serverFolder) { /**
* 删除临时文件夹,会自动删除空目录的父文件夹
*
* @param serverFolder
*/
public void removeTempFolder(String serverFolder) {
synchronized (folderCache) { synchronized (folderCache) {
if (StringHelper.toBoolean(folderCache.get(serverFolder))) { if (StringHelper.toBoolean(folderCache.get(serverFolder))) {
return; return;
...@@ -59,6 +69,25 @@ public class YzgFileProcedure implements InitializingBean { ...@@ -59,6 +69,25 @@ public class YzgFileProcedure implements InitializingBean {
// 获取延迟时间 // 获取延迟时间
long time = DateHelper.getDateTime(dtToday).getTime() - dt.getTime(); long time = DateHelper.getDateTime(dtToday).getTime() - dt.getTime();
// 发送删除临时目录的命令 // 发送删除临时目录的命令
mqService.message(new MessageVo(REMOVE_TEMP_FOLDER, serverFolder, time)); mqService.message(new MessageVo(YZG_REMOVE_TEMP_FOLDER, serverFolder, time));
}
/**
* 移动文件
*
* @param req
*/
public void moveFile(YzgFileMoveReqVo req) {
moveFile(JsonHelper.serialize(req), 0);
}
/**
* 移动文件
*
* @param json
* @param dedTime
*/
public void moveFile(String json, int dedTime) {
mqService.message(new MessageVo(YZG_MOVE_FILE, json, dedTime));
} }
} }
package com.yanzuoguang.cloud.file; package com.yanzuoguang.cloud.file;
import com.yanzuoguang.cloud.file.vo.YzgFileMoveReqVo;
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;
...@@ -18,7 +19,6 @@ public interface YzgFileService { ...@@ -18,7 +19,6 @@ public interface YzgFileService {
*/ */
YzgFileUploadResVo upload(YzgFileUploadReqVo req); YzgFileUploadResVo upload(YzgFileUploadReqVo req);
/** /**
* 立即删除临时路径 * 立即删除临时路径
* *
...@@ -26,4 +26,10 @@ public interface YzgFileService { ...@@ -26,4 +26,10 @@ public interface YzgFileService {
*/ */
void removeTempFolder(String tempFolder); void removeTempFolder(String tempFolder);
/**
* 移动文件或文件夹
*
* @param req
*/
void moveFile(YzgFileMoveReqVo req);
} }
package com.yanzuoguang.cloud.file.vo;
import com.yanzuoguang.util.vo.BaseVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* 移动文件
*
* @author 颜佐光
*/
@ApiModel(description = "移动文件单项信息")
public class YzgFileMoveItemReqVo extends BaseVo {
/**
* 来源路径,当目标路径存在时,则认为已经移动成功
*/
@ApiModelProperty(notes = "来源路径,当目标路径存在时,则认为已经移动成功", position = 1000)
private String from;
/**
* 目标路径
*/
@ApiModelProperty(notes = "目标路径", position = 1010)
private String to;
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
}
package com.yanzuoguang.cloud.file.vo;
import com.yanzuoguang.util.vo.BaseVo;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import java.util.ArrayList;
import java.util.List;
/**
* 移动文件请求参数
*
* @author 颜佐光
*/
@ApiModel(description = "移动文件请求参数")
public class YzgFileMoveReqVo extends BaseVo {
/**
* 需要移动的文件列表
*/
@ApiModelProperty(notes = "需要移动的文件列表", position = 1020)
public List<YzgFileMoveItemReqVo> list = new ArrayList<>();
public List<YzgFileMoveItemReqVo> getList() {
return list;
}
public void setList(List<YzgFileMoveItemReqVo> list) {
this.list = list;
}
}
package com.yanzuoguang.cloud.file; package com.yanzuoguang.cloud.file;
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Channel;
import com.yanzuoguang.cloud.file.vo.YzgFileMoveReqVo;
import com.yanzuoguang.mq.service.MqService; import com.yanzuoguang.mq.service.MqService;
import com.yanzuoguang.util.exception.CodeException; import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.log.Log; import com.yanzuoguang.util.log.Log;
import org.springframework.amqp.core.Message; import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener; import org.springframework.amqp.rabbit.annotation.RabbitListener;
...@@ -48,7 +50,7 @@ public class YzgFileConsumer implements InitializingBean { ...@@ -48,7 +50,7 @@ public class YzgFileConsumer implements InitializingBean {
* @param message * @param message
* @param channel * @param channel
*/ */
@RabbitListener(queues = {YzgFileProcedure.REMOVE_TEMP_FOLDER}) @RabbitListener(queues = {YzgFileProcedure.YZG_REMOVE_TEMP_FOLDER})
public void notesCheckBack(String tempFolder, Message message, Channel channel) { public void notesCheckBack(String tempFolder, Message message, Channel channel) {
try { try {
fileService.removeTempFolder(tempFolder); fileService.removeTempFolder(tempFolder);
...@@ -56,7 +58,29 @@ public class YzgFileConsumer implements InitializingBean { ...@@ -56,7 +58,29 @@ public class YzgFileConsumer implements InitializingBean {
Log.error(YzgFileConsumer.class, ex); Log.error(YzgFileConsumer.class, ex);
} catch (Exception ex) { } catch (Exception ex) {
Log.error(YzgFileConsumer.class, ex); Log.error(YzgFileConsumer.class, ex);
fileProcedure.removeFolder(tempFolder); fileProcedure.removeTempFolder(tempFolder);
} finally {
mqService.basicAck(message, channel);
}
}
/**
* 移动临时目录
*
* @param json
* @param message
* @param channel
*/
@RabbitListener(queues = {YzgFileProcedure.YZG_MOVE_FILE})
public void moveFile(String json, Message message, Channel channel) {
try {
YzgFileMoveReqVo req = JsonHelper.deserialize(json, YzgFileMoveReqVo.class);
fileService.moveFile(req);
} catch (CodeException ex) {
Log.error(YzgFileConsumer.class, ex);
} catch (Exception ex) {
Log.error(YzgFileConsumer.class, ex);
fileProcedure.moveFile(json, 60 * 1000);
} finally { } finally {
mqService.basicAck(message, channel); mqService.basicAck(message, channel);
} }
......
package com.yanzuoguang.cloud.file; package com.yanzuoguang.cloud.file;
import com.yanzuoguang.cloud.file.vo.YzgFileUploadItemResVo; import com.yanzuoguang.cloud.file.vo.*;
import com.yanzuoguang.cloud.file.vo.YzgFileUploadReqVo;
import com.yanzuoguang.cloud.file.vo.YzgFileUploadResVo;
import com.yanzuoguang.util.cache.MemoryCache; 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;
...@@ -13,6 +11,9 @@ import org.springframework.stereotype.Component; ...@@ -13,6 +11,9 @@ import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import java.io.File; import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date; import java.util.Date;
/** /**
...@@ -57,7 +58,7 @@ public class YzgFileServiceImpl implements YzgFileService { ...@@ -57,7 +58,7 @@ public class YzgFileServiceImpl implements YzgFileService {
// 创建服务器路径 // 创建服务器路径
FileHelper.createDirectory(serverFolder); FileHelper.createDirectory(serverFolder);
// 删除目录 // 删除目录
procedure.removeFolder(serverFolder); procedure.removeTempFolder(serverFolder);
// 设置保存文件的路径 // 设置保存文件的路径
String serverFile = String.format("%s/%s", serverFolder, saveFileName); String serverFile = String.format("%s/%s", serverFolder, saveFileName);
...@@ -108,6 +109,65 @@ public class YzgFileServiceImpl implements YzgFileService { ...@@ -108,6 +109,65 @@ public class YzgFileServiceImpl implements YzgFileService {
} }
} }
/**
* 移动文件或文件夹
*
* @param req
*/
@Override
public void moveFile(YzgFileMoveReqVo req) {
if (req.getList() == null || req.getList().isEmpty()) {
throw new CodeException("请传入需要移动的文件");
}
// 先检查一遍参数
for (YzgFileMoveItemReqVo item : req.getList()) {
if (StringHelper.isEmpty(item.getFrom())) {
throw new CodeException("移动来源路径不能为空");
}
if (StringHelper.isEmpty(item.getTo())) {
throw new CodeException("移动目标路径不能为空");
}
checkFolder(item.getFrom());
checkFolder(item.getTo());
String fullFrom = fileConfig.getServerFullPath(item.getFrom());
String fullTo = fileConfig.getServerFullPath(item.getTo());
File fileFrom = new File(fullFrom);
File fileTo = new File(fullTo);
if (fileFrom.exists() && fileTo.exists()) {
if (fileFrom.isFile() && fileTo.isDirectory()) {
throw new CodeException(String.format("不能用文件 %s 覆盖文件夹 %s", item.getFrom(), item.getTo()));
}
}
}
// 然后再移动
for (YzgFileMoveItemReqVo item : req.getList()) {
String fullFrom = fileConfig.getServerFullPath(item.getFrom());
String fullTo = fileConfig.getServerFullPath(item.getTo());
File fileFrom = new File(fullFrom);
File fileTo = new File(fullTo);
// 防止另外一个线程移动中
if (fileFrom.exists() && fileTo.exists()) {
if (fileFrom.isFile() && fileTo.isDirectory()) {
throw new CodeException(String.format("不能用文件 %s 覆盖文件夹 %s", item.getFrom(), item.getTo()));
}
} else if (!fileFrom.exists()) {
continue;
}
try {
Files.move(Paths.get(fullFrom), Paths.get(fullTo), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception ex) {
}
}
}
/** /**
* 获取行记的显示的图片 * 获取行记的显示的图片
* *
......
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