图谱-后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.3 KiB

10 months ago
package com.teaching.backend.controller.resource;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.teaching.backend.common.BaseResponse;
import com.teaching.backend.common.ResultUtils;
10 months ago
import com.teaching.backend.service.resource.CourseResourcesService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.UUID;
/**
* ClassName: CommonController
* Package: com.teaching.backend.controller
* Description:
*
* @Author 姜钧瀚
* @Create 2024/6/1 10:30
* @Version 1.0
*/
@RestController
10 months ago
public class ResourceController {
@Resource
private CourseResourcesService courseResourcesService;
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.oss.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyun.oss.bucketName}")
private String bucketName;
@PostMapping("/upload")
public BaseResponse<String> upload(MultipartFile file) throws IOException {
10 months ago
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String originalFilename = file.getOriginalFilename();
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
Integer Filetype;
String subDir;
if (suffix.equalsIgnoreCase(".jpg") || suffix.equalsIgnoreCase(".jpeg") || suffix.equalsIgnoreCase(".png")) {
subDir = "img/";
Filetype=1 ;
} else if (suffix.equalsIgnoreCase(".mp4") || suffix.equalsIgnoreCase(".avi") || suffix.equalsIgnoreCase(".mov")) {
subDir = "video/";
Filetype=2 ;
} else if (suffix.equalsIgnoreCase(".mp3") || suffix.equalsIgnoreCase(".wav") || suffix.equalsIgnoreCase(".flac")) {
subDir = "video/";
Filetype = 3;
}else {
subDir = "others/";
Filetype=4;
}
String fileName = UUID.randomUUID().toString().replace("-", "") + suffix;
ossClient.putObject(bucketName, subDir + fileName, file.getInputStream());
String objectUrl = "https://" + bucketName + "." + endpoint + "/" + subDir + fileName;
Integer type=Filetype;
courseResourcesService.updateFile(type, fileName, objectUrl); // 更新文件路径为OSS生成的URL
ossClient.shutdown();
System.out.println("文件的地址是:" + objectUrl);
return ResultUtils.success(objectUrl);
}
//文件下载
@GetMapping("/download")
public BaseResponse<String> download(@RequestParam String Id, @RequestParam String path, HttpServletResponse response) throws IOException {
System.out.println("执行了下载的方法");
courseResourcesService.download(Id,path,response,endpoint,accessKeyId,accessKeySecret,bucketName);
return ResultUtils.success("成功");
}
}