fix(文件编辑): 多次引用的文件支持编辑

This commit is contained in:
MAC 2022-04-05 12:41:38 +08:00
parent e1ed307806
commit 357f716de0
3 changed files with 68 additions and 29 deletions

View File

@ -33,8 +33,6 @@ import java.util.Map;
public class WebLogAcpect { public class WebLogAcpect {
@Resource @Resource
IOperationLogService operationLogService; IOperationLogService operationLogService;
@Resource
IUserService userService;
private String operation = ""; private String operation = "";
private String module = ""; private String module = "";

View File

@ -8,17 +8,28 @@ import com.qiwenshare.common.util.DateUtil;
import com.qiwenshare.file.api.*; import com.qiwenshare.file.api.*;
import com.qiwenshare.file.config.es.FileSearch; import com.qiwenshare.file.config.es.FileSearch;
import com.qiwenshare.file.domain.*; import com.qiwenshare.file.domain.*;
import com.qiwenshare.file.mapper.FileMapper;
import com.qiwenshare.file.mapper.UserFileMapper; import com.qiwenshare.file.mapper.UserFileMapper;
import com.qiwenshare.file.util.TreeNode; import com.qiwenshare.file.util.TreeNode;
import com.qiwenshare.ufop.factory.UFOPFactory; import com.qiwenshare.ufop.factory.UFOPFactory;
import com.qiwenshare.ufop.operation.copy.Copier;
import com.qiwenshare.ufop.operation.copy.domain.CopyFile;
import com.qiwenshare.ufop.operation.download.Downloader;
import com.qiwenshare.ufop.operation.download.domain.DownloadFile;
import com.qiwenshare.ufop.operation.write.Writer;
import com.qiwenshare.ufop.operation.write.domain.WriteFile;
import com.qiwenshare.ufop.util.UFOPUtils; import com.qiwenshare.ufop.util.UFOPUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -37,6 +48,8 @@ public class FileDealComp {
@Resource @Resource
UserFileMapper userFileMapper; UserFileMapper userFileMapper;
@Resource @Resource
FileMapper fileMapper;
@Resource
IUserService userService; IUserService userService;
@Resource @Resource
IShareService shareService; IShareService shareService;
@ -44,6 +57,8 @@ public class FileDealComp {
IShareFileService shareFileService; IShareFileService shareFileService;
@Resource @Resource
IUserFileService userFileService; IUserFileService userFileService;
@Resource
UFOPFactory ufopFactory;
@Autowired @Autowired
@Lazy @Lazy
@ -352,4 +367,48 @@ public class FileDealComp {
} }
return true; return true;
} }
/**
* 拷贝文件
* 场景修改的文件被多处引用时需要重新拷贝一份然后在新的基础上修改
* @param fileBean
* @param userFile
* @return
*/
public String copyFile(FileBean fileBean, UserFile userFile) {
Copier copier = ufopFactory.getCopier();
Downloader downloader = ufopFactory.getDownloader(fileBean.getStorageType());
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileBean.getFileUrl());
CopyFile copyFile = new CopyFile();
copyFile.setExtendName(userFile.getExtendName());
String fileUrl = copier.copy(downloader.getInputStream(downloadFile), copyFile);
if (downloadFile.getOssClient() != null) {
downloadFile.getOssClient().shutdown();
}
fileBean.setFileUrl(fileUrl);
fileBean.setFileId(null);
fileMapper.insert(fileBean);
userFile.setFileId(fileBean.getFileId());
userFile.setUploadTime(DateUtil.getCurrentTime());
userFileService.updateById(userFile);
return fileUrl;
}
public String getIdentifierByFile(String fileUrl, int storageType) throws IOException {
DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileUrl);
InputStream inputStream = ufopFactory.getDownloader(storageType).getInputStream(downloadFile);
String md5Str = DigestUtils.md5Hex(inputStream);
return md5Str;
}
public void saveFileInputStream(int storageType, String fileUrl, InputStream inputStream) throws IOException {
Writer writer1 = ufopFactory.getWriter(storageType);
WriteFile writeFile = new WriteFile();
writeFile.setFileUrl(fileUrl);
int fileSize = inputStream.available();
writeFile.setFileSize(fileSize);
writer1.write(inputStream, writeFile);
}
} }

View File

@ -12,6 +12,7 @@ import com.qiwenshare.common.util.security.SessionUtil;
import com.qiwenshare.file.api.IFileService; import com.qiwenshare.file.api.IFileService;
import com.qiwenshare.file.api.IUserFileService; import com.qiwenshare.file.api.IUserFileService;
import com.qiwenshare.file.api.IUserService; import com.qiwenshare.file.api.IUserService;
import com.qiwenshare.file.component.FileDealComp;
import com.qiwenshare.file.domain.FileBean; import com.qiwenshare.file.domain.FileBean;
import com.qiwenshare.file.domain.UserFile; import com.qiwenshare.file.domain.UserFile;
import com.qiwenshare.file.dto.file.CreateOfficeFileDTO; import com.qiwenshare.file.dto.file.CreateOfficeFileDTO;
@ -55,7 +56,8 @@ public class OfficeController {
IUserService userService; IUserService userService;
@Resource @Resource
UFOPFactory ufopFactory; UFOPFactory ufopFactory;
@Resource
FileDealComp fileDealComp;
@Value("${deployment.host}") @Value("${deployment.host}")
private String deploymentHost; private String deploymentHost;
@Value("${server.port}") @Value("${server.port}")
@ -231,51 +233,31 @@ public class OfficeController {
UserFile userFile = userFileService.getById(userFileId); UserFile userFile = userFileService.getById(userFileId);
FileBean fileBean = fileService.getById(userFile.getFileId()); FileBean fileBean = fileService.getById(userFile.getFileId());
Long pointCount = fileService.getFilePointCount(userFile.getFileId()); Long pointCount = fileService.getFilePointCount(userFile.getFileId());
String fileUrl = fileBean.getFileUrl();
if (pointCount > 1) { if (pointCount > 1) {
//该场景暂不支持编辑修改 fileUrl = fileDealComp.copyFile(fileBean, userFile);
writer.write("{\"error\":1}");
return ;
} }
URL url = new URL(downloadUri); URL url = new URL(downloadUri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int fileLength = 0;
try { try {
InputStream stream = connection.getInputStream(); InputStream stream = connection.getInputStream();
fileDealComp.saveFileInputStream(fileBean.getStorageType(), fileUrl, stream);
Writer writer1 = ufopFactory.getWriter(fileBean.getStorageType());
WriteFile writeFile = new WriteFile();
writeFile.setFileUrl(fileBean.getFileUrl());
writeFile.setFileSize(connection.getContentLength());
writer1.write(stream, writeFile);
} catch (Exception e) { } catch (Exception e) {
log.error(e.getMessage()); log.error(e.getMessage());
} finally { } finally {
if ("2".equals(status)) {
LambdaUpdateWrapper<UserFile> userFileUpdateWrapper = new LambdaUpdateWrapper<>(); int fileLength = connection.getContentLength();
userFileUpdateWrapper
.set(UserFile::getUploadTime, DateUtil.getCurrentTime())
.eq(UserFile::getUserFileId, userFileId);
userFileService.update(userFileUpdateWrapper);
}
LambdaUpdateWrapper<FileBean> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
fileLength = connection.getContentLength();
log.info("当前修改文件大小为:" + Long.valueOf(fileLength)); log.info("当前修改文件大小为:" + Long.valueOf(fileLength));
DownloadFile downloadFile = new DownloadFile(); DownloadFile downloadFile = new DownloadFile();
downloadFile.setFileUrl(fileBean.getFileUrl()); downloadFile.setFileUrl(fileBean.getFileUrl());
InputStream inputStream = ufopFactory.getDownloader(fileBean.getStorageType()).getInputStream(downloadFile); InputStream inputStream = ufopFactory.getDownloader(fileBean.getStorageType()).getInputStream(downloadFile);
String md5Str = DigestUtils.md5Hex(inputStream); String md5Str = DigestUtils.md5Hex(inputStream);
lambdaUpdateWrapper
.set(FileBean::getIdentifier, md5Str)
.set(FileBean::getFileSize, Long.valueOf(fileLength))
.set(FileBean::getModifyTime, DateUtil.getCurrentTime())
.set(FileBean::getModifyUserId, userId)
.eq(FileBean::getFileId, fileBean.getFileId());
fileService.update(lambdaUpdateWrapper);
fileService.updateFileDetail(userFile.getUserFileId(), md5Str, fileLength, userId);
connection.disconnect(); connection.disconnect();
} }
} }