|
|
@@ -0,0 +1,152 @@
|
|
|
+package cn.seecoder.seecoderportalserver.serviceImpl;
|
|
|
+
|
|
|
+import cn.seecoder.seecoderportalcommon.vo.*;
|
|
|
+import cn.seecoder.seecoderportalcommon.vo.dto.CourseWareDTO;
|
|
|
+import cn.seecoder.seecoderportalserver.domain.CourseWare;
|
|
|
+import cn.seecoder.seecoderportalserver.mapper.CourseWareMapper;
|
|
|
+import cn.seecoder.seecoderportalserver.repository.CourseWareRepository;
|
|
|
+import cn.seecoder.seecoderportalserver.service.CourseWareService;
|
|
|
+import cn.seecoder.seecoderportalserver.utility.OssUtil;
|
|
|
+import org.springframework.core.io.ByteArrayResource;
|
|
|
+import org.springframework.http.HttpHeaders;
|
|
|
+import org.springframework.http.MediaType;
|
|
|
+import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.web.multipart.MultipartFile;
|
|
|
+
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.*;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Path;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Objects;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class CourseWareServiceImpl implements CourseWareService {
|
|
|
+ private final CourseWareRepository courseWareRepository;
|
|
|
+ private final CourseWareMapper courseWareMapper;
|
|
|
+ private final OssUtil ossUtil;
|
|
|
+
|
|
|
+ public CourseWareServiceImpl(CourseWareRepository courseWareRepository, CourseWareMapper courseWareMapper, OssUtil ossUtil) {
|
|
|
+ this.courseWareRepository = courseWareRepository;
|
|
|
+ this.courseWareMapper = courseWareMapper;
|
|
|
+ this.ossUtil = ossUtil;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CourseWareVO createCourseWare(CourseWareVO courseWareVO) {
|
|
|
+ System.out.println("create");
|
|
|
+ CourseWare courseWare = courseWareMapper.toCourseWare(courseWareVO);
|
|
|
+ courseWare.setState(ObjectState.NORMAL);
|
|
|
+ CourseWare result = courseWareRepository.save(courseWare);
|
|
|
+ System.out.println(result);
|
|
|
+ return courseWareMapper.toCourseWareVO(result);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Boolean checkCourseWareNameUnique(Long courseId, String name) {
|
|
|
+ System.out.println("checkname");
|
|
|
+ List<CourseWare> courseWares = courseWareRepository.findCourseWaresByCourseIdAndStateNot(courseId, ObjectState.DELETED);
|
|
|
+ for (CourseWare courseWare: courseWares) {
|
|
|
+ if (courseWare.getName().equals(name)) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String deleteCourseWare(Long id) {
|
|
|
+ System.out.println("delete");
|
|
|
+ CourseWare courseWare = courseWareRepository.findCourseWareById(id);
|
|
|
+ courseWare.setState(ObjectState.DELETED);
|
|
|
+ courseWareRepository.save(courseWare);
|
|
|
+ return "delete success";
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<CourseWareDTO> getCourseWaresByCourseId(Long courseId) {
|
|
|
+ System.out.println("get");
|
|
|
+ List<CourseWareDTO> result = courseWareRepository.findCourseWaresByCourseIdAndStateNot(courseId, ObjectState.DELETED).stream()
|
|
|
+ .map(courseWare -> new CourseWareDTO(courseWareMapper.toCourseWareVO(courseWare)))
|
|
|
+ .collect(Collectors.toList());
|
|
|
+ return result;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CourseWareVO getCourseWareById(Long id) {
|
|
|
+ CourseWare courseWare = courseWareRepository.findCourseWareById(id);
|
|
|
+ return courseWareMapper.toCourseWareVO(courseWare);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String uploadCourseWareFile(String name, MultipartFile file) {
|
|
|
+ try {
|
|
|
+ System.out.println("upload");
|
|
|
+ String courseWareUrl = ossUtil.upload(name, file.getInputStream());
|
|
|
+ System.out.println(courseWareUrl);
|
|
|
+ return courseWareUrl;
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String downloadCourseWare(HttpServletRequest req, HttpServletResponse res, Long id) throws IOException {
|
|
|
+ System.out.println("download");
|
|
|
+ CourseWare courseWare = courseWareRepository.findCourseWareById(id);
|
|
|
+ String fileName = courseWare.getName();
|
|
|
+ String filePath = courseWare.getFileUrl();
|
|
|
+
|
|
|
+ OutputStream os = null;
|
|
|
+ InputStream is = null;
|
|
|
+ try{
|
|
|
+ res.reset();
|
|
|
+ res.setCharacterEncoding("utf-8");
|
|
|
+ res.setContentType("application/octet-stream");
|
|
|
+ res.setHeader("Content-Disposition", "attachment;" + new String(fileName.getBytes("gb2312"), "ISO8859-1"));
|
|
|
+ File f = new File(filePath);
|
|
|
+ is = new FileInputStream(f);
|
|
|
+ ServletOutputStream sout = res.getOutputStream();
|
|
|
+ BufferedInputStream bis = null;
|
|
|
+ BufferedOutputStream bos = null;
|
|
|
+ bis = new BufferedInputStream(is);
|
|
|
+ bos = new BufferedOutputStream(sout);
|
|
|
+ byte[] buff = new byte[2048];
|
|
|
+ int bytesRead;
|
|
|
+ while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
|
|
|
+ bos.write(buff, 0, bytesRead);
|
|
|
+ }
|
|
|
+ bos.flush();
|
|
|
+ bos.close();
|
|
|
+ bis.close();
|
|
|
+ is.close();
|
|
|
+ os.close();
|
|
|
+ }catch (Exception e){
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ return fileName;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getSlideUrl(Long slideId) {
|
|
|
+ System.out.println("geturl");
|
|
|
+ CourseWare courseWare = courseWareRepository.findCourseWareById(slideId);
|
|
|
+ return courseWare.getFileUrl();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CourseWareVO getOneSlide(Long slideId) {
|
|
|
+ System.out.println("getinfo");
|
|
|
+ CourseWare courseWare = courseWareRepository.findCourseWareById(slideId);
|
|
|
+ return courseWareMapper.toCourseWareVO(courseWare);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|