| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- package com.njuzr.eaibackend.controller;
- import com.njuzr.eaibackend.utils.OssUtil;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.IOException;
- /**
- * @author Liululin
- * @date 2025/1/21 - 18:54
- */
- @RestController
- @Slf4j
- @RequestMapping("/api/audio")
- public class AudioController {
- private final OssUtil ossUtil;
- @Autowired
- public AudioController(OssUtil ossUtil) {
- this.ossUtil = ossUtil;
- }
- /**
- * 上传音频文件
- * @param file MultipartFile 音频文件
- * @return 上传后的文件 URL
- * @throws IOException 上传失败时抛出
- */
- @PostMapping
- public MyResponse uploadAudio(@RequestParam("file") MultipartFile file) throws IOException {
- // 校验文件格式
- String originalFilename = file.getOriginalFilename();
- if (originalFilename == null ||
- (!originalFilename.endsWith(".mp3") && !originalFilename.endsWith(".wav"))) {
- return MyResponse.error(500,"文件格式不支持,仅支持 mp3 和 wav 格式");
- }
- // 定义存储路径
- String filePath = "audio/" + originalFilename;
- // 上传文件到 OSS
- String url = ossUtil.uploadFile(file.getInputStream(), filePath);
- log.info("url:" + url);
- // 返回文件 URL
- return MyResponse.success(url);
- }
- /**
- * 获取音频文件列表
- * @return 文件列表
- */
- @GetMapping
- public MyResponse getAudioFiles() {
- // 获取 OSS 中 audio 目录下的文件列表
- return MyResponse.success(ossUtil.listFiles());
- }
- /**
- * 删除指定音频文件
- * @param filePath 文件路径
- * @return 删除结果
- */
- @DeleteMapping
- public MyResponse deleteAudio(@RequestParam String filePath) {
- // 删除文件
- ossUtil.deleteFile(filePath);
- return MyResponse.success("音频文件删除成功");
- }
- }
|