Skip to content

refactor: extract AudioFileService to consolidate audio upload logic#571

Closed
shenenqing wants to merge 1 commit intoLianjiaTech:developfrom
shenenqing:refactor/extract-audio-file-service
Closed

refactor: extract AudioFileService to consolidate audio upload logic#571
shenenqing wants to merge 1 commit intoLianjiaTech:developfrom
shenenqing:refactor/extract-audio-file-service

Conversation

@shenenqing
Copy link
Copy Markdown
Collaborator

Summary

  • 新建 AudioFileService,将 AudioControllerQwenAdaptor 中重复的"上传文件 → 获取URL"逻辑提取为共享服务
  • 新增 /v1/audio/transcriptions/file/upload 接口用于音频文件上传
  • callbackUrl 为 null 时自动填充空字符串,不再抛出异常

🤖 Generated with Claude Code

将 AudioController 和 QwenAdaptor 中重复的"上传文件 → 获取URL"逻辑
提取为共享的 AudioFileService。新增 /v1/audio/transcriptions/file/upload
接口用于音频文件上传。同时将 callbackUrl 为 null 时的行为从抛出异常改为
自动填充空字符串。

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@szl97
Copy link
Copy Markdown
Collaborator

szl97 commented Mar 27, 2026

@claude 检查提交的代码,使用中文回复

@szl97
Copy link
Copy Markdown
Collaborator

szl97 commented Mar 27, 2026

Claude finished @szl97's task —— View job


代码审查结果

正在检查提交的代码,任务列表:

  • 读取并分析新增的 AudioFileService.java
  • 检查 AudioController.java 的修改
  • 分析 QwenAdaptor.java 的简化
  • 评估重构的代码质量和设计
  • 提供详细的审查反馈

审查反馈

🟢 优点

  1. 良好的重构实践:成功提取了重复的"上传文件→获取URL"逻辑到独立的服务类 AudioFileService,遵循了DRY原则。

  2. API设计合理:新增的 /v1/audio/transcriptions/file/upload 接口设计简洁,返回格式清晰。

  3. 代码清理有效QwenAdaptor.java 从原来的重复代码减少到简洁的服务调用,代码更加可维护。

  4. 空值处理改进:在 validateRequestParams 中对 callbackUrl 为null时自动填充空字符串,避免异常抛出,提升了鲁棒性。

🟡 改进建议

  1. 异常处理AudioFileService.uploadAndGetUrl() 方法缺少异常处理。建议:

    public String uploadAndGetUrl(byte[] content, String filename) {
        try {
            String uniqueName = UUID.randomUUID() + "_" + filename;
            OpenAiService service = openAiServiceFactory.create();
            File file = service.uploadFile("temp", content, uniqueName);
            return service.retrieveFileUrl(file.getId()).getUrl();
        } catch (Exception e) {
            log.error("Failed to upload audio file: {}", filename, e);
            throw new BellaException("音频文件上传失败", e);
        }
    }
  2. 参数验证AudioFileService.uploadAndGetUrl() 应该验证输入参数:

    if (content == null || content.length == 0) {
        throw new IllegalArgumentException("Audio content cannot be null or empty");
    }
    if (StringUtils.isBlank(filename)) {
        throw new IllegalArgumentException("Filename cannot be blank");
    }
  3. 文件名安全性:filename 参数应该进行安全检查,防止路径遍历攻击:

    // 清理文件名,移除路径分隔符
    String safeName = filename.replaceAll("[/\\\\]", "_");
  4. 日志记录:建议在 AudioFileService 中添加 @Slf4j 注解并记录关键操作日志。

🔵 技术细节

  1. 代码复用效果:重构前 QwenAdaptor 有17行重复代码,重构后仅需4行调用,代码减少率约76%。

  2. Spring注入:正确使用了 @Service@Autowired 注解,符合Spring最佳实践。

  3. 方法命名uploadAndGetUrl 方法名清晰表达了方法功能。

🟢 整体评价

这次重构整体质量良好,成功消除了代码重复,提升了代码可维护性。新增的服务类职责单一,接口设计合理。唯一需要注意的是异常处理和参数验证的完善。

建议状态:条件通过 - 建议补充异常处理后合并。


@shenenqing shenenqing closed this Mar 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants