|
|
|
package com.teaching.backend.service.impl;
|
|
|
|
|
|
|
|
import com.aliyun.oss.OSS;
|
|
|
|
import com.aliyun.oss.OSSClientBuilder;
|
|
|
|
import com.aliyun.oss.model.OSSObject;
|
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
|
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
|
|
|
|
|
import com.teaching.backend.mapper.resource.CourseResourcesMapper;
|
|
|
|
import com.teaching.backend.model.entity.CourseResources;
|
|
|
|
import com.teaching.backend.model.entity.ResourceRelationship;
|
|
|
|
import com.teaching.backend.service.resource.CourseResourcesService;
|
|
|
|
import com.teaching.backend.service.resource.ResourcesRelationshipService;
|
|
|
|
import org.apache.commons.io.FileUtils;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ClassName: FileTableImpl
|
|
|
|
* Package: com.teaching.backend.service.impl
|
|
|
|
* Description:
|
|
|
|
*
|
|
|
|
* @Author 姜钧瀚
|
|
|
|
* @Create 2024/6/4 11:50
|
|
|
|
* @Version 1.0
|
|
|
|
*/
|
|
|
|
@Service
|
|
|
|
public class CourseResourcesServiceImpl extends ServiceImpl<CourseResourcesMapper, CourseResources> implements CourseResourcesService {
|
|
|
|
@Resource
|
|
|
|
ResourcesRelationshipService resourcesRelationshipService;
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
private CourseResourcesMapper courseResourcesMapper;
|
|
|
|
@Override
|
|
|
|
public void updateFile(Integer type, String fileName, String filePath) {
|
|
|
|
|
|
|
|
CourseResources courseResources = new CourseResources();
|
|
|
|
|
|
|
|
courseResources.setName(fileName);
|
|
|
|
courseResources.setPath(filePath);
|
|
|
|
courseResources.setType(type);
|
|
|
|
|
|
|
|
this.save(courseResources);
|
|
|
|
|
|
|
|
int resourceId=courseResources.getId();
|
|
|
|
System.out.println(resourceId);
|
|
|
|
|
|
|
|
ResourceRelationship resourcesRelationship=new ResourceRelationship();
|
|
|
|
|
|
|
|
resourcesRelationship.setResourceid(String.valueOf(resourceId));
|
|
|
|
|
|
|
|
resourcesRelationshipService.save(resourcesRelationship);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void download(String Id, String path, HttpServletResponse response, String endpoint, String accessKeyId, String accessKeySecret, String bucketName) throws IOException {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CourseResources courseResources=getById(Id);
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println(courseResources);
|
|
|
|
|
|
|
|
|
|
|
|
// 创建OSS客户端
|
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
|
|
|
String key = extractKeyFromFullPath(courseResources.getPath());
|
|
|
|
OSSObject object = ossClient.getObject(bucketName, key);
|
|
|
|
|
|
|
|
String fileName = courseResources.getName();
|
|
|
|
|
|
|
|
// 设置保存文件的路径
|
|
|
|
String savePath = path + "/" + fileName;
|
|
|
|
|
|
|
|
// 获取文件的输入流
|
|
|
|
InputStream inputStream = object.getObjectContent();
|
|
|
|
|
|
|
|
// 设置响应头
|
|
|
|
response.setContentType("application/octet-stream");
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
|
|
|
|
|
|
|
|
// 创建保存文件
|
|
|
|
File saveFile = new File(savePath);
|
|
|
|
|
|
|
|
// 将文件内容复制到保存文件中
|
|
|
|
FileUtils.copyInputStreamToFile(inputStream, saveFile);
|
|
|
|
|
|
|
|
// 关闭输入流和OSS客户端
|
|
|
|
inputStream.close();
|
|
|
|
ossClient.shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Page<CourseResources> getresourceRecords(Integer type, List<Integer> resourceIdList, int page, int pageSize) {
|
|
|
|
Page <CourseResources> pageInfo=new Page<>(page,pageSize);
|
|
|
|
|
|
|
|
LambdaQueryWrapper<CourseResources>lambdaQueryWrapper=new LambdaQueryWrapper<>();
|
|
|
|
lambdaQueryWrapper.in(CourseResources::getId,resourceIdList);
|
|
|
|
lambdaQueryWrapper.eq(CourseResources::getType,type);
|
|
|
|
|
|
|
|
return this.page(pageInfo,lambdaQueryWrapper);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private String extractKeyFromFullPath(String filePath) {
|
|
|
|
// 提取存储桶名称
|
|
|
|
String key = filePath.substring(filePath.indexOf("com") + 4, filePath.indexOf("?"));
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public List<CourseResources> getByType(String type) {
|
|
|
|
LambdaQueryWrapper<CourseResources> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
lambdaQueryWrapper.eq(CourseResources::getType, type);
|
|
|
|
|
|
|
|
List<CourseResources> list = this.list(lambdaQueryWrapper);
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void removeCourseResources(List<Long> ids) {
|
|
|
|
LambdaQueryWrapper<CourseResources> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
|
|
|
lambdaQueryWrapper.in(CourseResources::getId, ids);
|
|
|
|
List<CourseResources> list = this.list(lambdaQueryWrapper);
|
|
|
|
courseResourcesMapper.deleteBatchIds(list);
|
|
|
|
// System.out.println(this.updateBatchById(list));
|
|
|
|
// this.remove(lambdaQueryWrapper);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|