mirror of
https://github.com/continew-org/continew-admin.git
synced 2025-11-02 16:57:11 +08:00
feat: 文件管理适配后端文件上传、单个删除、批量删除 API
This commit is contained in:
@@ -76,18 +76,7 @@ public class FileRecorderImpl implements FileRecorder {
|
||||
if (null == file) {
|
||||
return null;
|
||||
}
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
String extension = file.getExtension();
|
||||
fileInfo.setOriginalFilename(
|
||||
StrUtil.isNotBlank(extension) ? file.getName() + StringConstants.DOT + extension : file.getName());
|
||||
fileInfo.setSize(file.getSize());
|
||||
fileInfo.setUrl(file.getUrl());
|
||||
fileInfo.setExt(extension);
|
||||
fileInfo.setBasePath(StringConstants.EMPTY);
|
||||
fileInfo.setPath(StringConstants.EMPTY);
|
||||
fileInfo.setFilename(StrUtil.subAfter(url, StringConstants.SLASH, true));
|
||||
fileInfo.setPlatform(storageMapper.lambdaQuery().eq(StorageDO::getId, file.getStorageId()).one().getCode());
|
||||
return fileInfo;
|
||||
return file.toFileInfo(storageMapper.lambdaQuery().eq(StorageDO::getId, file.getStorageId()).one().getCode());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -20,9 +20,15 @@ import java.io.Serial;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import org.dromara.x.file.storage.core.FileInfo;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
|
||||
import top.charles7c.continew.admin.system.enums.FileTypeEnum;
|
||||
import top.charles7c.continew.starter.core.constant.StringConstants;
|
||||
import top.charles7c.continew.starter.core.util.URLUtils;
|
||||
import top.charles7c.continew.starter.extension.crud.base.BaseDO;
|
||||
|
||||
/**
|
||||
@@ -70,4 +76,26 @@ public class FileDO extends BaseDO {
|
||||
* 存储库 ID
|
||||
*/
|
||||
private Long storageId;
|
||||
|
||||
/**
|
||||
* 转换为 X-File-Storage 文件信息对象
|
||||
*
|
||||
* @param storageCode
|
||||
* 存储库编码
|
||||
* @return X-File-Storage 文件信息对象
|
||||
*/
|
||||
public FileInfo toFileInfo(String storageCode) {
|
||||
FileInfo fileInfo = new FileInfo();
|
||||
fileInfo.setOriginalFilename(
|
||||
StrUtil.isNotBlank(this.extension) ? this.name + StringConstants.DOT + this.extension : this.name);
|
||||
fileInfo.setSize(this.size);
|
||||
fileInfo.setUrl(this.url);
|
||||
fileInfo.setExt(this.extension);
|
||||
fileInfo.setBasePath(StringConstants.EMPTY);
|
||||
fileInfo.setPath(StringConstants.EMPTY);
|
||||
fileInfo.setFilename(
|
||||
URLUtils.isHttpUrl(this.url) ? StrUtil.subAfter(this.url, StringConstants.SLASH, true) : this.url);
|
||||
fileInfo.setPlatform(storageCode);
|
||||
return fileInfo;
|
||||
}
|
||||
}
|
||||
@@ -18,22 +18,34 @@ package top.charles7c.continew.admin.system.model.req;
|
||||
|
||||
import java.io.Serial;
|
||||
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import top.charles7c.continew.starter.extension.crud.base.BaseReq;
|
||||
|
||||
/**
|
||||
* 创建或修改文件信息
|
||||
* 修改文件信息
|
||||
*
|
||||
* @author Charles7c
|
||||
* @since 2023/12/23 10:38
|
||||
*/
|
||||
@Data
|
||||
@Schema(description = "创建或修改文件信息")
|
||||
@Schema(description = "修改文件信息")
|
||||
public class FileReq extends BaseReq {
|
||||
|
||||
@Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 名称
|
||||
*/
|
||||
@Schema(description = "名称", example = "test123")
|
||||
@NotBlank(message = "文件名称不能为空")
|
||||
@Length(max = 255, message = "文件名称长度不能超过 {max} 个字符")
|
||||
private String name;
|
||||
}
|
||||
@@ -17,6 +17,8 @@
|
||||
package top.charles7c.continew.admin.system.service.impl;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
@@ -62,6 +64,20 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, FileDO, FileRes
|
||||
private StorageService storageService;
|
||||
private final FileStorageService fileStorageService;
|
||||
|
||||
@Override
|
||||
public void delete(List<Long> ids) {
|
||||
List<FileDO> fileList = baseMapper.lambdaQuery().in(FileDO::getId, ids).list();
|
||||
Map<Long, List<FileDO>> fileListGroup = fileList.stream().collect(Collectors.groupingBy(FileDO::getStorageId));
|
||||
for (Map.Entry<Long, List<FileDO>> entry : fileListGroup.entrySet()) {
|
||||
StorageDetailResp storage = storageService.get(entry.getKey());
|
||||
for (FileDO file : entry.getValue()) {
|
||||
FileInfo fileInfo = file.toFileInfo(storage.getCode());
|
||||
fileStorageService.delete(fileInfo);
|
||||
}
|
||||
}
|
||||
super.delete(ids);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileInfo upload(MultipartFile file, String storageCode) {
|
||||
StorageDO storage;
|
||||
|
||||
Reference in New Issue
Block a user