use of com.moxi.mougblog.base.exception.exceptionType.UpdateException in project mogu_blog_v2 by moxi624.
the class NetworkDiskServiceImpl method updateFilepathByFilepath.
@Override
public void updateFilepathByFilepath(NetworkDiskVO networkDiskVO) {
String oldFilePath = networkDiskVO.getOldFilePath();
String newFilePath = networkDiskVO.getNewFilePath();
String fileName = networkDiskVO.getFileName();
String fileOldName = networkDiskVO.getFileOldName();
String extendName = networkDiskVO.getExtendName();
if ("null".equals(networkDiskVO.getExtendName())) {
extendName = null;
}
// 判断移动的路径是否相同【拼接出原始目录】
String fileOldPath = oldFilePath + fileOldName + "/";
if (fileOldPath.equals(newFilePath)) {
throw new UpdateException("不能选择自己");
}
// 移动根目录
QueryWrapper<NetworkDisk> queryWrapper = new QueryWrapper<>();
queryWrapper.eq(SQLConf.FILE_PATH, networkDiskVO.getOldFilePath());
queryWrapper.eq(SQLConf.FILE_NAME, networkDiskVO.getFileName());
if (StringUtils.isNotEmpty(extendName)) {
queryWrapper.eq(SQLConf.EXTEND_NAME, extendName);
} else {
queryWrapper.isNull(SQLConf.EXTEND_NAME);
}
List<NetworkDisk> networkDiskList = networkDiskService.list(queryWrapper);
for (NetworkDisk networkDisk : networkDiskList) {
// 修改新的路径
networkDisk.setFilePath(newFilePath);
// 修改旧文件名
networkDisk.setFileOldName(networkDiskVO.getFileOldName());
// 如果扩展名为空,代表是文件夹,还需要修改文件名
if (StringUtils.isEmpty(extendName)) {
networkDisk.setFileName(networkDiskVO.getFileOldName());
}
}
if (networkDiskList.size() > 0) {
networkDiskService.updateBatchById(networkDiskList);
}
// 移动子目录
oldFilePath = oldFilePath + fileName + "/";
newFilePath = newFilePath + fileOldName + "/";
oldFilePath = oldFilePath.replace("\\", "\\\\\\\\");
oldFilePath = oldFilePath.replace("'", "\\'");
oldFilePath = oldFilePath.replace("%", "\\%");
oldFilePath = oldFilePath.replace("_", "\\_");
// 为null说明是目录,则需要移动子目录
if (extendName == null) {
// 移动根目录
QueryWrapper<NetworkDisk> childQueryWrapper = new QueryWrapper<>();
childQueryWrapper.likeRight(SQLConf.FILE_PATH, oldFilePath);
List<NetworkDisk> childList = networkDiskService.list(childQueryWrapper);
for (NetworkDisk networkDisk : childList) {
String filePath = networkDisk.getFilePath();
networkDisk.setFilePath(filePath.replace(oldFilePath, newFilePath));
}
if (childList.size() > 0) {
networkDiskService.updateBatchById(childList);
}
}
}
use of com.moxi.mougblog.base.exception.exceptionType.UpdateException in project mogu_blog_v2 by moxi624.
the class StorageServiceImpl method uploadFile.
@Override
public String uploadFile(NetworkDisk networkDisk, List<MultipartFile> fileDatas) {
HttpServletRequest request = RequestHolder.getRequest();
SystemConfig systemConfig = feignUtil.getSystemConfig();
if (systemConfig == null) {
throw new QueryException(ErrorCode.SYSTEM_CONFIG_IS_NOT_EXIST, MessageConf.SYSTEM_CONFIG_NOT_EXIST);
}
// 计算文件大小
Long newStorageSize = 0L;
Long storageSize = 0L;
for (MultipartFile fileData : fileDatas) {
newStorageSize += fileData.getSize();
}
Storage storage = getStorageByAdmin();
if (storage != null) {
storageSize = storage.getStorageSize() + newStorageSize;
// 判断上传的文件是否超过了剩余空间
if (storage.getMaxStorageSize() < storageSize) {
throw new UpdateException(ErrorCode.UPDATE_DEFAULT_ERROR, "上传失败,您可用的空间已经不足!");
} else {
storage.setStorageSize(storageSize);
}
} else {
throw new UpdateException(ErrorCode.UPDATE_DEFAULT_ERROR, "上传失败,您没有分配可用的上传空间!");
}
// 上传文件
String result = fileService.batchUploadFile(request, fileDatas, systemConfig);
List<File> fileList = WebUtils.getAllList(result, File.class);
List<NetworkDisk> networkDiskList = new ArrayList<>();
for (File file : fileList) {
NetworkDisk saveNetworkDisk = new NetworkDisk();
saveNetworkDisk.setAdminUid(request.getAttribute(SysConf.ADMIN_UID).toString());
saveNetworkDisk.setFilePath(networkDisk.getFilePath());
saveNetworkDisk.setQiNiuUrl(file.getQiNiuUrl());
saveNetworkDisk.setLocalUrl(file.getPicUrl());
saveNetworkDisk.setMinioUrl(file.getMinioUrl());
saveNetworkDisk.setFileSize(file.getFileSize());
saveNetworkDisk.setFileName(file.getPicName());
saveNetworkDisk.setExtendName(file.getPicExpandedName());
saveNetworkDisk.setFileOldName(file.getFileOldName());
saveNetworkDisk.setCreateTime(new Date());
networkDiskList.add(saveNetworkDisk);
}
// 上传文件
networkDiskService.saveBatch(networkDiskList);
// 更新容量大小
storageService.updateById(storage);
return ResultUtil.successWithMessage(MessageConf.OPERATION_SUCCESS);
}
Aggregations