作业导出

This commit is contained in:
lu
2024-07-25 11:20:20 +08:00
parent 7b2217c017
commit f901a2897f
3 changed files with 107 additions and 5 deletions

View File

@@ -1,11 +1,18 @@
package com.xboe.module.course.api;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xboe.api.ThirdApi;
import com.xboe.api.vo.AuditList;
@@ -14,6 +21,12 @@ import com.xboe.api.vo.UserDynamic;
import com.xboe.api.vo.UserdynamicParam;
import com.xboe.common.OrderCondition;
import com.xboe.core.JsonResponseStatus;
import com.xboe.core.orm.FieldFilters;
import com.xboe.module.course.dao.CourseContentDao;
import com.xboe.module.course.dao.CourseDao;
import com.xboe.module.course.dao.CourseHomeWorkDao;
import com.xboe.module.course.dao.CourseSectionDao;
import com.xboe.module.course.entity.*;
import com.xboe.module.course.vo.TeacherVo;
import com.xboe.module.usergroup.entity.UserGroupItem;
import org.apache.commons.lang3.StringUtils;
@@ -34,11 +47,6 @@ import com.xboe.core.api.ApiBaseController;
import com.xboe.module.course.dto.CourseQueryDto;
import com.xboe.module.course.dto.CourseTeacherDto;
import com.xboe.module.course.dto.RankingDto;
import com.xboe.module.course.entity.Course;
import com.xboe.module.course.entity.CourseContent;
import com.xboe.module.course.entity.CourseCrowd;
import com.xboe.module.course.entity.CourseSection;
import com.xboe.module.course.entity.CourseTeacher;
import com.xboe.module.course.service.ICourseContentService;
import com.xboe.module.course.service.ICourseSectionService;
import com.xboe.module.course.service.ICourseService;
@@ -89,6 +97,18 @@ public class CoursePortalApi extends ApiBaseController{
@Resource
private ThirdApi thirdApi;
@Resource
private CourseHomeWorkDao homeworkDao;
@Resource
private CourseContentDao ccDao;
@Resource
private CourseSectionDao courseSectionDao;
@Resource
private CourseDao courseDao;
@Autowired
StringRedisTemplate redisTemplate;
@@ -347,7 +367,81 @@ public class CoursePortalApi extends ApiBaseController{
}
}
//作业导出
@GetMapping("/export")
public void export(String contentId,HttpServletResponse response) throws IOException {
//将courseId以逗号分割转换为list
List<String> contentIds = Arrays.asList(contentId.split(","));
Map<String, String>map=new HashMap<>();
try {
homeworkDao.findList(FieldFilters.in("content_id", contentIds)).stream().filter(Objects::nonNull).forEach(e -> {
//查询内容名称
CourseContent courseContent=contentService.getById(e.getContentId());
if(courseContent!=null &&courseContent.getContentName()==null){
courseContent.setContentName("作业");
}
//查询目录
List<CourseSection> courseSections = sectionService.getByCourseId(e.getCourseId());
//取作业后缀名
int dotIndex = e.getFile().lastIndexOf('.'); // 查找最后一个'.'的位置
String extension = e.getFile().substring(dotIndex);
//判断是否有目录
if(!courseSections.isEmpty()){
map.put(courseSections.get(0).getName()+"--"+courseContent.getContentName()+extension, e.getFile());
}else {
Course course=courseDao.get(e.getCourseId());
if(course==null || course.getDeleted()){
throw new RuntimeException("课程不存在或已被删除");
}
map.put(course.getName()+"--"+courseContent.getContentName()+extension, e.getFile());
}
});
} catch (Exception e) {
throw new RuntimeException("导出异常");
}
long totalCompressedSize = 0;
String encodedFilename = URLEncoder.encode("作业.zip")
.replace("+", "%20") // 空格替换为"%20"
.replace("%2F", "/"); // 解决斜杠问题
try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
for (Map.Entry<String, String> e : map.entrySet()) {
File fileToZip = new File(e.getValue());
// 检查加上当前文件大小后是否会超过2GB
long fileSizeInBytes = fileToZip.length();
if (totalCompressedSize + fileSizeInBytes > 2L * 1024 * 1024 * 1024) {
throw new RuntimeException("您要下载的作业过大,请分批下载或联系管理员!");
}
// 添加 ZIP 条目
ZipEntry entry = new ZipEntry(e.getKey());
entry.setSize(fileToZip.length());
zos.putNextEntry(entry);
try (FileInputStream fis = new FileInputStream(fileToZip)) {
byte[] buffer = new byte[4096];
int len;
while ((len = fis.read(buffer)) > 0) {
zos.write(buffer, 0, len);
totalCompressedSize += len;
}
}
zos.closeEntry();
StringBuilder contentDispositionValue = new StringBuilder();
contentDispositionValue.append("attachment; filename=")
.append(encodedFilename)
.append(";")
.append("filename*=")
.append("utf-8''")
.append(encodedFilename);
// 设置响应类型和Content-Disposition头
response.setContentType("application/zip");
response.setHeader("Content-Disposition", contentDispositionValue.toString());
}
}
}
@GetMapping("/detail-study")
public JsonResponse<List<CourseStudyVo>> detailStudy(String courseId, String aid){
if(StringUtil.isBlank(courseId)){

View File

@@ -82,4 +82,6 @@ public interface ICourseContentService{
* @return
*/
CourseAssess getAssess(String ccid);
CourseContent getById(String contentId);
}

View File

@@ -141,6 +141,12 @@ public class CourseContentServiceImpl implements ICourseContentService {
return assess;
}
@Override
public CourseContent getById(String contentId) {
CourseContent courseContent = ccDao.findOne(FieldFilters.eq("id", contentId));
return courseContent;
}
@Override
@Transactional
public void updateName(String id, String name) {