You've already forked FrameTour-BE
refactor(storage): 简化存储适配器配置逻辑并移除降级机制
- 移除默认存储配置常量 DEFAULT_STORAGE - 简化 UploadStage 中的存储适配器获取逻辑,直接使用 StorageFactory.use() - 移除降级到默认存储的处理机制 - 在 PuzzleGenerateServiceImpl 中复用存储适配器实例 - 移除 SourceRepository 中的 StorageUnsupportedException 导入 - 移除 GoodsServiceImpl 中的 StorageType 枚举导入 - 移除 SourceServiceImpl 中的 ScenicService 依赖注入 - 移除 PrinterServiceImpl 中的复杂存储适配器配置逻辑 - 在 TaskTaskServiceImpl 中统一使用景点存储适配器 - 在 FaceCleaner 中添加新的存储清理逻辑,使用独立的图片存储适配器 - 添加 sourceImageUrlMap 和 sourceScenicIdMap 来优化文件清理逻辑
This commit is contained in:
@@ -25,8 +25,6 @@ import java.io.File;
|
|||||||
)
|
)
|
||||||
public class UploadStage extends AbstractPipelineStage<PhotoProcessContext> {
|
public class UploadStage extends AbstractPipelineStage<PhotoProcessContext> {
|
||||||
|
|
||||||
private static final String DEFAULT_STORAGE = "assets-ext";
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getName() {
|
public String getName() {
|
||||||
return "UploadStage";
|
return "UploadStage";
|
||||||
@@ -39,17 +37,11 @@ public class UploadStage extends AbstractPipelineStage<PhotoProcessContext> {
|
|||||||
return StageResult.failed("没有可上传的文件");
|
return StageResult.failed("没有可上传的文件");
|
||||||
}
|
}
|
||||||
|
|
||||||
IStorageAdapter adapter = context.getStorageAdapter();
|
IStorageAdapter adapter;
|
||||||
boolean usingDefaultStorage = false;
|
try {
|
||||||
|
adapter = StorageFactory.use();
|
||||||
if (adapter == null) {
|
} catch (Exception e) {
|
||||||
log.debug("未配置存储适配器,使用默认存储: {}", DEFAULT_STORAGE);
|
return StageResult.failed("无法获取存储: " + e.getMessage(), e);
|
||||||
try {
|
|
||||||
adapter = StorageFactory.use(DEFAULT_STORAGE);
|
|
||||||
usingDefaultStorage = true;
|
|
||||||
} catch (Exception e) {
|
|
||||||
return StageResult.failed("无法获取默认存储: " + e.getMessage(), e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -57,32 +49,10 @@ public class UploadStage extends AbstractPipelineStage<PhotoProcessContext> {
|
|||||||
context.setResultUrl(uploadedUrl);
|
context.setResultUrl(uploadedUrl);
|
||||||
|
|
||||||
log.info("文件上传成功: {}", uploadedUrl);
|
log.info("文件上传成功: {}", uploadedUrl);
|
||||||
|
|
||||||
if (usingDefaultStorage) {
|
|
||||||
return StageResult.degraded("降级: 使用默认存储 " + DEFAULT_STORAGE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return StageResult.success("已上传");
|
return StageResult.success("已上传");
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("文件上传失败", e);
|
log.error("文件上传失败", e);
|
||||||
|
|
||||||
if (!usingDefaultStorage) {
|
|
||||||
log.warn("尝试降级到默认存储");
|
|
||||||
try {
|
|
||||||
IStorageAdapter defaultAdapter = StorageFactory.use(DEFAULT_STORAGE);
|
|
||||||
String uploadedUrl = uploadFile(defaultAdapter, fileToUpload);
|
|
||||||
context.setResultUrl(uploadedUrl);
|
|
||||||
|
|
||||||
log.info("降级上传成功: {}", uploadedUrl);
|
|
||||||
return StageResult.degraded("降级: 使用默认存储 " + DEFAULT_STORAGE);
|
|
||||||
|
|
||||||
} catch (Exception fallbackEx) {
|
|
||||||
log.error("降级上传也失败", fallbackEx);
|
|
||||||
return StageResult.failed("上传失败(包括降级): " + fallbackEx.getMessage(), fallbackEx);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return StageResult.failed("上传失败: " + e.getMessage(), e);
|
return StageResult.failed("上传失败: " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -468,12 +468,14 @@ public class PuzzleGenerateServiceImpl implements IPuzzleGenerateService {
|
|||||||
// 上传到OSS
|
// 上传到OSS
|
||||||
try (FileInputStream fis = new FileInputStream(qrcode)) {
|
try (FileInputStream fis = new FileInputStream(qrcode)) {
|
||||||
String fileName = String.format("qrcode_%d.jpg", faceId);
|
String fileName = String.format("qrcode_%d.jpg", faceId);
|
||||||
boolean exists = StorageFactory.use().isExists("puzzle", "wechat_qrcode", fileName);
|
var storageAdapter = StorageFactory.use();
|
||||||
|
boolean exists = storageAdapter.isExists("puzzle", "wechat_qrcode", fileName);
|
||||||
if (exists) {
|
if (exists) {
|
||||||
log.debug("微信小程序二维码已存在, 不重复上传: faceId={}, url={}", faceId, StorageFactory.use().getUrl("puzzle", "wechat_qrcode", fileName));
|
String url = storageAdapter.getUrl("puzzle", "wechat_qrcode", fileName);
|
||||||
return StorageFactory.use().getUrl("puzzle", "wechat_qrcode", fileName);
|
log.debug("微信小程序二维码已存在, 不重复上传: faceId={}, url={}", faceId, url);
|
||||||
|
return url;
|
||||||
}
|
}
|
||||||
return StorageFactory.use().uploadFile(
|
return storageAdapter.uploadFile(
|
||||||
"image/jpeg",
|
"image/jpeg",
|
||||||
fis,
|
fis,
|
||||||
"puzzle",
|
"puzzle",
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import com.ycwl.basic.pricing.enums.VoucherDiscountType;
|
|||||||
import com.ycwl.basic.pricing.service.IVoucherService;
|
import com.ycwl.basic.pricing.service.IVoucherService;
|
||||||
import com.ycwl.basic.storage.StorageFactory;
|
import com.ycwl.basic.storage.StorageFactory;
|
||||||
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
||||||
import com.ycwl.basic.storage.exceptions.StorageUnsupportedException;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
@@ -154,14 +153,7 @@ public class SourceRepository {
|
|||||||
.build();
|
.build();
|
||||||
context.enableStage("image_sr");
|
context.enableStage("image_sr");
|
||||||
context.enableStage("image_enhance");
|
context.enableStage("image_enhance");
|
||||||
ScenicConfigManager configManager = scenicRepository.getScenicConfigManager(source.getScenicId());
|
IStorageAdapter adapter = StorageFactory.use();
|
||||||
IStorageAdapter adapter;
|
|
||||||
try {
|
|
||||||
adapter = StorageFactory.get(configManager.getString("store_type"));
|
|
||||||
adapter.loadConfig(configManager.getObject("store_config_json", Map.class));
|
|
||||||
} catch (StorageUnsupportedException ignored) {
|
|
||||||
adapter = StorageFactory.use("assets-ext");
|
|
||||||
}
|
|
||||||
context.setStorageAdapter(adapter);
|
context.setStorageAdapter(adapter);
|
||||||
|
|
||||||
// 2. 设置结果URL回调 - 更新source记录
|
// 2. 设置结果URL回调 - 更新source记录
|
||||||
|
|||||||
@@ -51,7 +51,6 @@ import com.ycwl.basic.model.repository.TaskUpdateResult;
|
|||||||
import com.ycwl.basic.storage.StorageFactory;
|
import com.ycwl.basic.storage.StorageFactory;
|
||||||
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
||||||
import com.ycwl.basic.storage.enums.StorageAcl;
|
import com.ycwl.basic.storage.enums.StorageAcl;
|
||||||
import com.ycwl.basic.storage.enums.StorageType;
|
|
||||||
import com.ycwl.basic.utils.ApiResponse;
|
import com.ycwl.basic.utils.ApiResponse;
|
||||||
import com.ycwl.basic.utils.WxMpUtil;
|
import com.ycwl.basic.utils.WxMpUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
@@ -573,13 +572,7 @@ public class GoodsServiceImpl implements GoodsService {
|
|||||||
if (configManager != null && configManager.getString("watermark_type") != null && !isBuy.isBuy()) {
|
if (configManager != null && configManager.getString("watermark_type") != null && !isBuy.isBuy()) {
|
||||||
ImageWatermarkOperatorEnum type = ImageWatermarkOperatorEnum.getByCode(configManager.getString("watermark_type"));
|
ImageWatermarkOperatorEnum type = ImageWatermarkOperatorEnum.getByCode(configManager.getString("watermark_type"));
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
IStorageAdapter adapter;
|
IStorageAdapter adapter = StorageFactory.use();
|
||||||
if (configManager.getEnum("store_type", StorageType.class) != null) {
|
|
||||||
adapter = StorageFactory.get(configManager.getEnum("store_type", StorageType.class));
|
|
||||||
adapter.loadConfig(JacksonUtil.parseObject(configManager.getString("store_config_json"), Map.class));
|
|
||||||
} else {
|
|
||||||
adapter = StorageFactory.use("assets-ext");
|
|
||||||
}
|
|
||||||
List<SourceWatermarkEntity> watermarkEntityList = sourceMapper.listSourceWatermark(list.stream().map(SourceRespVO::getId).collect(Collectors.toList()), face.getId(), type.getType());
|
List<SourceWatermarkEntity> watermarkEntityList = sourceMapper.listSourceWatermark(list.stream().map(SourceRespVO::getId).collect(Collectors.toList()), face.getId(), type.getType());
|
||||||
|
|
||||||
// 边缘端处理:需要二维码和头像 URL
|
// 边缘端处理:需要二维码和头像 URL
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import com.ycwl.basic.model.pc.source.resp.SourceRespVO;
|
|||||||
import com.ycwl.basic.repository.DeviceRepository;
|
import com.ycwl.basic.repository.DeviceRepository;
|
||||||
import com.ycwl.basic.repository.ScenicRepository;
|
import com.ycwl.basic.repository.ScenicRepository;
|
||||||
import com.ycwl.basic.repository.SourceRepository;
|
import com.ycwl.basic.repository.SourceRepository;
|
||||||
import com.ycwl.basic.service.pc.ScenicService;
|
|
||||||
import com.ycwl.basic.service.pc.SourceService;
|
import com.ycwl.basic.service.pc.SourceService;
|
||||||
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
import com.ycwl.basic.storage.adapters.IStorageAdapter;
|
||||||
import com.ycwl.basic.task.VideoPieceGetter;
|
import com.ycwl.basic.task.VideoPieceGetter;
|
||||||
@@ -47,8 +46,6 @@ public class SourceServiceImpl implements SourceService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private SourceRepository sourceRepository;
|
private SourceRepository sourceRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
private ScenicService scenicService;
|
|
||||||
@Autowired
|
|
||||||
private ScenicRepository scenicRepository;
|
private ScenicRepository scenicRepository;
|
||||||
@Autowired
|
@Autowired
|
||||||
private DeviceRepository deviceRepository;
|
private DeviceRepository deviceRepository;
|
||||||
@@ -184,7 +181,7 @@ public class SourceServiceImpl implements SourceService {
|
|||||||
throw new BaseException("该素材不存在");
|
throw new BaseException("该素材不存在");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
IStorageAdapter adapter = scenicService.getScenicStorageAdapter(source.getScenicId());
|
IStorageAdapter adapter = StorageFactory.use();
|
||||||
String uploadedUrl = adapter.uploadFile("image/jpeg", file, PHOTO_PATH, id + "_q_.jpg");
|
String uploadedUrl = adapter.uploadFile("image/jpeg", file, PHOTO_PATH, id + "_q_.jpg");
|
||||||
|
|
||||||
SourceEntity sourceUpd = new SourceEntity();
|
SourceEntity sourceUpd = new SourceEntity();
|
||||||
|
|||||||
@@ -1103,25 +1103,7 @@ public class PrinterServiceImpl implements PrinterService {
|
|||||||
* 从context中的scenicConfigManager获取配置
|
* 从context中的scenicConfigManager获取配置
|
||||||
*/
|
*/
|
||||||
private void prepareStorageAdapter(PhotoProcessContext context) {
|
private void prepareStorageAdapter(PhotoProcessContext context) {
|
||||||
ScenicConfigManager scenicConfig = context.getScenicConfigManager();
|
context.setStorageAdapter(StorageFactory.use());
|
||||||
if (scenicConfig == null) {
|
|
||||||
log.warn("scenicConfigManager未设置,将使用默认存储");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
String storeType = scenicConfig.getString("store_type");
|
|
||||||
if (storeType != null) {
|
|
||||||
IStorageAdapter adapter = StorageFactory.get(storeType);
|
|
||||||
String storeConfigJson = scenicConfig.getString("store_config_json");
|
|
||||||
if (StringUtils.isNotBlank(storeConfigJson)) {
|
|
||||||
adapter.loadConfig(JacksonUtil.parseObject(storeConfigJson, Map.class));
|
|
||||||
}
|
|
||||||
context.setStorageAdapter(adapter);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.warn("准备存储适配器失败,将使用默认存储: {}", e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -615,14 +615,7 @@ public class TaskTaskServiceImpl implements TaskService {
|
|||||||
if (worker == null) {
|
if (worker == null) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
RenderWorkerConfigManager config = repository.getWorkerConfigManager(worker.getId());
|
IStorageAdapter adapter = scenicService.getScenicStorageAdapter(task.getScenicId());
|
||||||
IStorageAdapter adapter;
|
|
||||||
try {
|
|
||||||
adapter = StorageFactory.get(config.getString("store_type"));
|
|
||||||
adapter.loadConfig(config.getObject("store_config_json", Map.class));
|
|
||||||
} catch (Exception e) {
|
|
||||||
adapter = scenicService.getScenicStorageAdapter(task.getScenicId());
|
|
||||||
}
|
|
||||||
String hash = MD5.create().digestHex(task.getTaskParams() + task.getFaceId().toString());
|
String hash = MD5.create().digestHex(task.getTaskParams() + task.getFaceId().toString());
|
||||||
String filename = StorageUtil.joinPath(StorageConstant.VLOG_PATH, task.getTemplateId().toString() + "_" + hash + "_" + task.getScenicId() + ".mp4");
|
String filename = StorageUtil.joinPath(StorageConstant.VLOG_PATH, task.getTemplateId().toString() + "_" + hash + "_" + task.getScenicId() + ".mp4");
|
||||||
// 生成
|
// 生成
|
||||||
|
|||||||
@@ -34,7 +34,9 @@ import org.springframework.stereotype.Component;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
import static com.ycwl.basic.constant.FaceConstant.USER_FACE_DB_NAME;
|
import static com.ycwl.basic.constant.FaceConstant.USER_FACE_DB_NAME;
|
||||||
@@ -226,6 +228,16 @@ public class FaceCleaner {
|
|||||||
public void cleanSourceOss() {
|
public void cleanSourceOss() {
|
||||||
log.info("开始清理源视频素材文件");
|
log.info("开始清理源视频素材文件");
|
||||||
List<SourceRespVO> list = sourceMapper.list(new SourceReqQuery());
|
List<SourceRespVO> list = sourceMapper.list(new SourceReqQuery());
|
||||||
|
Map<Long, String> sourceImageUrlMap = new HashMap<>();
|
||||||
|
Map<Long, Long> sourceScenicIdMap = new HashMap<>();
|
||||||
|
list.forEach(item -> {
|
||||||
|
if (item.getId() != null && item.getScenicId() != null) {
|
||||||
|
sourceScenicIdMap.put(item.getId(), item.getScenicId());
|
||||||
|
}
|
||||||
|
if (item.getId() != null) {
|
||||||
|
sourceImageUrlMap.put(item.getId(), item.getUrl());
|
||||||
|
}
|
||||||
|
});
|
||||||
ArrayList<String> adapterIdentity = new ArrayList<>();
|
ArrayList<String> adapterIdentity = new ArrayList<>();
|
||||||
ScenicReqQuery query = new ScenicReqQuery();
|
ScenicReqQuery query = new ScenicReqQuery();
|
||||||
query.setPageSize(1000);
|
query.setPageSize(1000);
|
||||||
@@ -260,22 +272,45 @@ public class FaceCleaner {
|
|||||||
log.info("文件存在关系:{},未删除", fileObject);
|
log.info("文件存在关系:{},未删除", fileObject);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
log.info("开始清理图片文件");
|
});
|
||||||
fileObjectList = adapter.listDir(StorageConstant.PHOTO_PATH);
|
|
||||||
fileObjectList.parallelStream().forEach(fileObject -> {
|
log.info("开始清理图片文件");
|
||||||
if (fileObject.getModifyTime() != null) {
|
IStorageAdapter imageAdapter = StorageFactory.use();
|
||||||
// 如果是一天以内修改的,则跳过
|
List<StorageFileObject> fileObjectList = imageAdapter.listDir(StorageConstant.PHOTO_PATH);
|
||||||
if (DateUtil.between(fileObject.getModifyTime(), new Date(), DateUnit.DAY) <= 1) {
|
fileObjectList.parallelStream().forEach(fileObject -> {
|
||||||
return;
|
if (fileObject.getModifyTime() != null) {
|
||||||
}
|
// 如果是一天以内修改的,则跳过
|
||||||
|
if (DateUtil.between(fileObject.getModifyTime(), new Date(), DateUnit.DAY) <= 1) {
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
if (list.parallelStream().filter(videoRespVO -> Objects.nonNull(videoRespVO.getUrl())).noneMatch(videoRespVO -> videoRespVO.getUrl().contains(fileObject.getName()))){
|
}
|
||||||
log.info("删除文件:{}", fileObject);
|
|
||||||
adapter.deleteFile(fileObject.getFullPath());
|
String name = fileObject.getName();
|
||||||
} else {
|
if (name == null) {
|
||||||
log.info("文件存在关系:{},未删除", fileObject);
|
return;
|
||||||
}
|
}
|
||||||
});
|
int underscoreIndex = name.indexOf('_');
|
||||||
|
if (underscoreIndex <= 0) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Long sourceId;
|
||||||
|
try {
|
||||||
|
sourceId = Long.parseLong(name.substring(0, underscoreIndex));
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
Long scenicId = sourceScenicIdMap.get(sourceId);
|
||||||
|
if (scenicId == null || disableDeleteScenicIds.contains(scenicId.toString())) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
String imageUrl = sourceImageUrlMap.get(sourceId);
|
||||||
|
if (imageUrl != null && imageUrl.contains(name)) {
|
||||||
|
log.info("文件存在关系:{},未删除", fileObject);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
log.info("删除文件:{}", fileObject);
|
||||||
|
imageAdapter.deleteFile(fileObject.getFullPath());
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
public void cleanVideoOss() {
|
public void cleanVideoOss() {
|
||||||
|
|||||||
Reference in New Issue
Block a user