Commit 86bab055 authored by luzhuang's avatar luzhuang

pdf分节 默认400页一节

parent 99d98eb2
......@@ -137,11 +137,11 @@ public class BChapterController extends PaginationController<BChapter> {
@ApiOperation(value = "207 预览文件", notes = "(预览时传)")
@GetMapping("/getFileAtView")
public Result getFileAtView(CurUser curUser, @Valid BFile file, BindingResult br) throws IOException {
String newFileUrl = bChapterService.changeFileToPdfAtView(file);
FileToPdfAtViewDTO dto = bChapterService.changeFileToPdfAtView(file);
return Result.builder(new PersistModel(1),
MessageConstant.MESSAGE_ALERT_SUCCESS,
MessageConstant.MESSAGE_ALERT_ERROR,
newFileUrl);
dto);
}
@ApiOperation(value = "208 下载文件 返回服务器地址", notes = "下载文件 返回服务器地址")
......
......@@ -450,5 +450,35 @@ public interface BChapterRepository extends BaseMapper<BChapter> {
@Update("update b_chapter set chapter_name = #{name} where business_id = #{id}")
int updateChapterName(@Param("name")String name,@Param("id") String id);
/**
* 根据chapterid查询分割pdf章节的chapterIdList
* @param chapterId
* @return
*/
@Select("SELECT business_id " +
"from b_chapter where business_id in( " +
" SELECT chapter_id from b_file where video_url = ( " +
" select video_url from b_file where chapter_id = #{chapterId} and del_flag != 1 and flag =1) " +
" and del_flag != 1 and flag =1) " +
"ORDER BY sort")
List<String> getPdfIdList(@Param("chapterId")String chapterId);
/**
* 新增节时 pdf分节查询出之前 排序大于修改节的chapterId
* @param chapterId
* @return
*/
@Select("select business_id from b_chapter where sort > ( " +
"select sort from b_chapter where business_id = #{chapterId} and del_flag != 1 and flag =1) " +
"and del_flag != 1 and flag =1 " +
"and pid = #{pid}")
List<String> getPDFSortList(@Param("chapterId")String chapterId,@Param("pid")String pid);
@Update("<script>" +
"<foreach collection=\"ids\" item=\"item\" separator=\";\">" +
"update b_chapter set sort = (sort+#{num}) where business_id = #{item}" +
"</foreach></script>")
int updatePdfSort(@Param("ids")List<String> ids,@Param("num")int num);
}
package org.rcisoft.business.bchapter.dto;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
@Data
public class FileToPdfAtViewDTO {
@ApiModelProperty(value = "预览地址")
private String url;
@ApiModelProperty(value = "切割pdf数量")
private int pdfNum;
}
......@@ -97,7 +97,7 @@ public interface BChapterService{
* @param bFile
* @return
*/
String changeFileToPdfAtView(BFile bFile);
FileToPdfAtViewDTO changeFileToPdfAtView(BFile bFile);
/**
* 下载文件
......
......@@ -41,6 +41,7 @@ import org.rcisoft.core.model.PersistModel;
import org.rcisoft.core.result.ResultServiceEnums;
import org.rcisoft.core.util.*;
import org.rcisoft.sys.user.bean.CurUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
......@@ -59,6 +60,7 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
......@@ -108,6 +110,9 @@ public class BChapterServiceImpl implements BChapterService {
@Value("${upload.fileMaxPages:100}")
private Integer fileMaxPages;
@Value("${upload.pdfCutPage:400}")
private Integer pdfCutPage;
@Value("${upload.pdfMaxSize:150}")
private Long pdfMaxSize;
......@@ -283,8 +288,8 @@ public class BChapterServiceImpl implements BChapterService {
}
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
public PersistModel persist(ChapterDTO chapterDTO, String corpId, uploadDTO dto) {
if(chapterDTO.getChapterLevel() == 2 && "true".equals(replaceHttp)){
if(dto.getVideoUrl() != null){
......@@ -334,7 +339,6 @@ public class BChapterServiceImpl implements BChapterService {
model.setSort(sort);
if (model.getChapterLevel() == 1) {
chapterDTO.setBusinessId(model.getBusinessId());
chapterDTO.setPid(model.getPid());
//添加章
......@@ -354,27 +358,63 @@ public class BChapterServiceImpl implements BChapterService {
UserUtil.setCurrentPersistOperation(bFile);
//新建节数据插入到chapter表
chapterDTO.setIsTest(dto.getType());
line = bChapterRepository.insertSelective(model);
LogUtil.fileChangeLog("----------------------------文件时间:" + model.getCourseTime() + "---------------------");
if("3".equals(model.getIsTest()) && Integer.parseInt(dto.getFileTime().substring(0,dto.getFileTime().lastIndexOf("页"))) > pdfCutPage ){
//pdf 且页数大于每页限制
int num = Integer.parseInt(dto.getFileTime().substring(0,dto.getFileTime().lastIndexOf("页")))/pdfCutPage + 1;
List<BChapter> bChapterList = new ArrayList<>();
List<BFile> bFileList = new ArrayList<>();
int i = 1;
while(i <= num){
BChapter newBchapter = new BChapter();
BFile newBfile = new BFile();
BeanUtils.copyProperties(bFile,newBfile);
BeanUtils.copyProperties(model,newBchapter);
if(i > 1){
newBchapter.setCommonBusinessId();
}
newBchapter.setSort(model.getSort()+i-1);
newBchapter.setChapterName(newBchapter.getChapterName()+"-"+i);
bChapterList.add(newBchapter);
newBfile.setCommonBusinessId();
newBfile.setChapterId(newBchapter.getBusinessId());
bFileList.add(newBfile);
i++;
}
for(BChapter b:bChapterList){
line = bChapterRepository.insertSelective(b);
String cid = bChapterRepository.getFirstChapter(b.getLessonId());
if (cid.equals(b.getBusinessId())) {
bChapterRepository.updateVideoTimeInLesson(b.getCourseTime(), b.getLessonId());
}
bChapterRepository.updateClassHourInLesson(b.getLessonId());
this.updateLessonProgress(chapterDTO.getLessonId(), true, "");
}
//上传文件数据插入到bfile表中
bFileRepository.insertUploadFileList(bFileList);
}else{
line = bChapterRepository.insertSelective(model);
//先判断该课程的第一节是否是本节 更新blesson表中course_time字段
String cid = bChapterRepository.getFirstChapter(model.getLessonId());
if (cid.equals(model.getBusinessId())) {
bChapterRepository.updateVideoTimeInLesson(model.getCourseTime(), model.getLessonId());
LogUtil.fileChangeLog("-----------------------------更新lesson中time:" + model.getCourseTime() + "---------------------");
}
//上传文件数据插入到bfile表中
int x = bFileRepository.insertUploadFile(bFile);
bChapterRepository.updateClassHourInLesson(model.getLessonId());//更新b_lesson表中 class_hour
// 新增节 才更新进度
if (2 == chapterDTO.getChapterLevel()) {
// 更新课程总进度
this.updateLessonProgress(chapterDTO.getLessonId(), true, "");
}
LogUtil.fileChangeLog("-----------------------------更新课时成功-------------------");
//上传文件数据插入到bfile表中
bFileRepository.insertUploadFile(bFile);
}
} else {
}
} else {//修改
BFile bFileOld = bFileRepository.selectInfoByChapterId(chapterDTO.getBusinessId());
if("3".equals(model.getIsTest()) && Integer.parseInt(dto.getFileTime().substring(0,dto.getFileTime().lastIndexOf("页"))) > pdfCutPage ){
model.setChapterName(model.getChapterName()+"-1");
}
if (model.getChapterLevel() == 1) {
int x = bChapterRepository.updateChapterName(model.getChapterName(), model.getBusinessId());
return new PersistModel(x);
......@@ -386,6 +426,7 @@ public class BChapterServiceImpl implements BChapterService {
model.setCourseTime(dto.getFileTime());
model.setIsTest(dto.getType());
bFile.setFileTime(dto.getFileTime());
if (StringUtils.isNotEmpty(dto.getFileTime()) && dto.getFileTime().contains(".")) {
model.setCourseTime(dto.getFileTime().substring(0, dto.getFileTime().lastIndexOf(".")));
bFile.setFileTime(dto.getFileTime().substring(0, dto.getFileTime().lastIndexOf(".")));
......@@ -435,7 +476,43 @@ public class BChapterServiceImpl implements BChapterService {
if (model.getChapterLevel() == 1) {
return new PersistModel(line);
}
int x = bFileRepository.updateUploadFile(bFile);
bFileRepository.updateUploadFile(bFile);
//修改资料为 pdf 且页数大于切割数量
if("3".equals(model.getIsTest()) && Integer.parseInt(dto.getFileTime().substring(0,dto.getFileTime().lastIndexOf("页"))) > pdfCutPage ){
int num = Integer.parseInt(dto.getFileTime().substring(0,dto.getFileTime().lastIndexOf("页")))/pdfCutPage + 1;
List<BChapter> bChapterList = new ArrayList<>();
List<BFile> bFileList = new ArrayList<>();
BChapter chapter = bChapterRepository.selectByPrimaryKey(model.getBusinessId());
List<String> sortList = bChapterRepository.getPDFSortList(model.getBusinessId(),model.getPid());
int i = 2;
while(i <= num){
BChapter newBchapter = new BChapter();
BFile newBfile = new BFile();
BeanUtils.copyProperties(bFile,newBfile);
BeanUtils.copyProperties(chapter,newBchapter);
newBchapter.setCommonBusinessId();
newBchapter.setSort(chapter.getSort()+i-1);
newBchapter.setChapterName(newBchapter.getChapterName().substring(0,newBchapter.getChapterName().lastIndexOf("-"))+"-"+i);
bChapterList.add(newBchapter);
UserUtil.setCurrentPersistOperation(newBfile);
newBfile.setCommonBusinessId();
newBfile.setLessonId(chapter.getLessonId());
newBfile.setFileTime(dto.getFileTime());
newBfile.setChapterId(newBchapter.getBusinessId());
bFileList.add(newBfile);
i++;
}
for(BChapter b:bChapterList){
line = bChapterRepository.insertSelective(b);
bChapterRepository.updateClassHourInLesson(b.getLessonId());
this.updateLessonProgress(chapterDTO.getLessonId(), true, "");
}
//上传文件数据插入到bfile表中
bFileRepository.insertUploadFileList(bFileList);
int zx =bChapterRepository.updatePdfSort(sortList,num-1);
}
// 判断 新文件 更新 课程学习进度(总进度 此章节进度)
if (bFileOld != null && StringUtils.isNotEmpty(bFileOld.getVideoUrl()) && !(bFileOld.getVideoUrl().equals(dto.getVideoUrl()))) {
// 所有学习过此课程的 学生课程信息
......@@ -660,11 +737,11 @@ public class BChapterServiceImpl implements BChapterService {
}
//1. 获取文件信息
BFile bFile = bFileRepository.selectInfoByChapterId(chapterId);
return changeFile(bFile);
return changeFile(bFile).getUrl();
}
@Override
public String changeFileToPdfAtView(BFile bFile) {
public FileToPdfAtViewDTO changeFileToPdfAtView(BFile bFile) {
if (StringUtils.isEmpty(bFile.getVideoUrl()) || StringUtils.isEmpty(bFile.getLessonId()) || StringUtils.isEmpty(bFile.getType())) {
return null;
// throw new ServiceException(ResultServiceEnums.PARAM_NOT_NULL);
......@@ -787,7 +864,9 @@ public class BChapterServiceImpl implements BChapterService {
}
public String changeFile(BFile bFile) {
public FileToPdfAtViewDTO changeFile(BFile bFile) {
FileToPdfAtViewDTO dto = new FileToPdfAtViewDTO();
List<String> pdfOssUrls = new ArrayList<>();
LogUtil.fileChangeLog("****************request + begin***********" + DateFormatUtils.format(new Date(), "HH:mm:ss"));
String name = bFile.getVideoUrl().substring(bFile.getVideoUrl().lastIndexOf("/") + 1);
String filePath = global.getBASE_UPLOAD_SERVER_LOCATION() + global.getCOURSE_LOCATION() + File.separator + bFile.getLessonId();
......@@ -803,16 +882,110 @@ public class BChapterServiceImpl implements BChapterService {
File fileNew = new File(filePath + File.separator + newName);
LogUtil.fileChangeLog("------------------判断文件类型-----------------------------" + fileNew.getPath());
if (FileTypeEnum.PDF.getCode().equals(bFile.getType())) {
LogUtil.fileChangeLog("------------------判断是否下载过-----------------------------");
if (fileOld.exists()) {
LogUtil.fileChangeLog("------------------存在直接返回路径----------------------------" + fileOld);
return returnBasePath + File.separator + name;
int pages = FileReadableUtil.pdfReadable(filePath + File.separator + name);
if(pages > pdfCutPage){
if(bFile.getChapterId()==null || "".equals(bFile.getChapterId())){
//预览
try {
List<String> pdfUrls = CutPdfUtil.toCutPdf(filePath + File.separator + name,name,pdfCutPage);
for(String url : pdfUrls){
pdfOssUrls.add(url);
}
dto.setUrl(pdfUrls.get(0).replace(filePath,returnBasePath));
dto.setPdfNum(pdfUrls.size());
} catch (Exception e) {
e.printStackTrace();
}
return dto;
}
List<String> chapterIds = bChapterRepository.getPdfIdList(bFile.getChapterId());
int x = 0;
for(String s : chapterIds){
x++;
if(s.equals(bFile.getChapterId())){
break;
}
}
String cutPdf = filePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf";
File pdf = new File(cutPdf);
if(pdf.exists()){
dto.setUrl(returnBasePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf");
return dto;
}else{
try {
List<String> pdfUrls = CutPdfUtil.toCutPdf(filePath + File.separator + name,name,pdfCutPage);
for(String url : pdfUrls){
pdfOssUrls.add(url);
}
dto.setUrl(returnBasePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf");
dto.setPdfNum(pdfUrls.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
dto.setUrl(returnBasePath + File.separator + name);
}
return dto;
} else {
String downLoadName = downLoadFile(bFile, file, filePath);
LogUtil.fileChangeLog("------------------判断pdf是否可读----------------------------" + fileOld);
FileReadableUtil.fileReadable(filePath + File.separator + downLoadName, fileMaxSize,pdfMaxSize,txtMaxSize, fileMaxPages);
int pages = FileReadableUtil.fileReadable(filePath + File.separator + downLoadName, fileMaxSize,pdfMaxSize,txtMaxSize, fileMaxPages);
LogUtil.fileChangeLog("------------------pdf文件可读--返回下载后路径-----------------------------" + downLoadName);
return returnBasePath + File.separator + downLoadName;
if(pages > pdfCutPage){
if(bFile.getChapterId()==null || "".equals(bFile.getChapterId())){
//预览
try {
List<String> pdfUrls = CutPdfUtil.toCutPdf(filePath + File.separator + name,name,pdfCutPage);
for(String url : pdfUrls){
pdfOssUrls.add(url);
}
dto.setUrl(pdfUrls.get(0).replace(filePath,returnBasePath));
dto.setPdfNum(pdfUrls.size());
} catch (Exception e) {
e.printStackTrace();
}
return dto;
}
List<String> chapterIds = bChapterRepository.getPdfIdList(bFile.getChapterId());
int x = 0;
for(String s : chapterIds){
x++;
if(s.equals(bFile.getChapterId())){
break;
}
}
String cutPdf = filePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf";
File pdf = new File(cutPdf);
if(pdf.exists()){
dto.setUrl(returnBasePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf");
return dto;
}else{
try {
List<String> pdfUrls = CutPdfUtil.toCutPdf(filePath + File.separator + name,name,pdfCutPage);
for(String url : pdfUrls){
pdfOssUrls.add(url);
}
dto.setUrl(returnBasePath + File.separator + "pdf-"+name.substring(0, name.lastIndexOf(".")) +
File.separator+name.substring(0, name.lastIndexOf("."))+"-"+x+".pdf");
dto.setPdfNum(pdfUrls.size());
} catch (Exception e) {
e.printStackTrace();
}
}
}else{
dto.setUrl(returnBasePath + File.separator + name);
}
return dto;
}
/** 不下载pdf、文件转发方案(上传快,没有可读判断)
String hostAddress = "";
......@@ -835,7 +1008,9 @@ public class BChapterServiceImpl implements BChapterService {
LogUtil.fileChangeLog("------------------文件为ppt或word或txt----------------------------");
if (fileNew.exists()) {
LogUtil.fileChangeLog("------------------转换后文件-存在-直接返回-----------------------------");
return returnBasePath + File.separator + newName;
pdfOssUrls.add(bFile.getVideoUrl());
dto.setUrl(returnBasePath + File.separator + newName);
return dto;
} else {
LogUtil.fileChangeLog("------------------删除老文件----------------------------");
fileOld.delete();
......@@ -863,7 +1038,9 @@ public class BChapterServiceImpl implements BChapterService {
}
LogUtil.fileChangeLog("****************request + end***********" + DateFormatUtils.format(new Date(), "HH:mm:ss"));
return returnBasePath + File.separator + newName;
pdfOssUrls.add(bFile.getVideoUrl());
dto.setUrl(returnBasePath + File.separator + newName);
return dto;
}
} else {
return null;
......
......@@ -51,6 +51,20 @@ public interface BFileRepository extends BaseMapper<BFile> {
"VALUES (#{businessId}, #{lessonId}, #{chapterId}, #{fileName}, #{videoUrl}, #{fileUrl}, #{type}, #{flag}, #{delFlag}, #{remarks}, #{createBy}, #{updateBy}, #{updateDate}, #{createDate},#{fileSize},#{fileTime})")
int insertUploadFile(BFile bFile);
/**
* 添加文件信息到file表
* @return
*/
@Insert("<script>" +
"INSERT INTO `b_file` " +
"(business_id,lesson_id,chapter_id,file_name,video_url,file_url,type,flag,del_flag,remarks,create_by,update_by,update_date,create_date,file_size,file_time) " +
"VALUES " +
"<foreach collection=\"list\" item=\"item\" separator=\",\">" +
"(#{item.businessId}, #{item.lessonId}, #{item.chapterId}, #{item.fileName}, #{item.videoUrl}, #{item.fileUrl}, #{item.type}, " +
"#{item.flag}, #{item.delFlag}, #{item.remarks}, #{item.createBy}, #{item.updateBy}, #{item.updateDate}, #{item.createDate},#{item.fileSize},#{item.fileTime})" +
"</foreach></script>")
int insertUploadFileList(List<BFile> bFiles);
@Update("update b_file set " +
"file_name = #{fileName} , " +
"video_url = #{videoUrl} ," +
......
......@@ -8,6 +8,7 @@ import com.itextpdf.text.pdf.PdfReader;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
......@@ -23,7 +24,7 @@ import java.util.List;
public class CutPdfUtil {
public static void main(String[] args) {
try {
List<String> pdfList = toCutPdf("F:\\桌面整理\\文件归纳\\QQ下载\\xx文字+图片-100页.pdf", 3);
List<String> pdfList = toCutPdf("F:\\桌面整理\\文件\\测试资料\\06_API常用类.pdf","06_API常用类.pdf", 20);
} catch (Exception e) {
e.printStackTrace();
}
......@@ -37,7 +38,7 @@ public class CutPdfUtil {
* @return
* @throws Exception
*/
public static List<String> toCutPdf(String filePath, int page) throws Exception {
public static List<String> toCutPdf(String filePath,String fileName, int page) throws Exception {
List<String> pdfList = new ArrayList<>();
long l1 = System.currentTimeMillis();
int i = 0;
......@@ -46,7 +47,8 @@ public class CutPdfUtil {
PdfReader reader = new PdfReader(filePath);
int pageNum = reader.getNumberOfPages();
do {
String newFilePath = filePath.substring(0, filePath.lastIndexOf(".")) + "-" + j + ".pdf";
String path = filePath.replace(fileName,"pdf-"+fileName);
String newFilePath = path.substring(0, path.lastIndexOf(".")) +File.separator+fileName.substring(0, fileName.lastIndexOf("."))+ "-" + j + ".pdf";
int end = i + page;
if (end >= pageNum) {
end = pageNum;
......@@ -76,7 +78,11 @@ public class CutPdfUtil {
String newFilePath, int from, int end) {
Document document = null;
PdfCopy copy = null;
File newFile = new File(newFilePath);
try {
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
document = new Document(reader.getPageSize(1));
copy = new PdfCopy(document, new FileOutputStream(newFilePath));
document.open();
......
......@@ -136,7 +136,7 @@ public class FileReadableUtil {
}
private static Integer pdfReadable(String path){
public static Integer pdfReadable(String path){
Integer pages;
PDDocument doc = null;
try {
......@@ -155,7 +155,7 @@ public class FileReadableUtil {
}
FileReadableUtil.closeOtherStream(doc);
}
return 1;
return pages;
}
private static Integer docxReadable(String path){
Integer pages ;
......@@ -190,7 +190,7 @@ public class FileReadableUtil {
}
public static void fileReadable(String path,Long fileMaxSize,Long pdfMaxSize,Long txtMaxSize,Integer fileMaxPages) {
public static int fileReadable(String path,Long fileMaxSize,Long pdfMaxSize,Long txtMaxSize,Integer fileMaxPages) {
LogUtil.fileChangeLog("-----------FileReadableUtil-路径------------"+path);
File file = new File(path);
Long size = file.length();
......@@ -237,13 +237,10 @@ public class FileReadableUtil {
break;
}
if (pages != -1 && pages > fileMaxPages) {
if (pages != -1 && pages > fileMaxPages && !"pdf".equals(path.substring(path.lastIndexOf(".") + 1))) {
throw new ServiceException(400,"该文件超过最多"+fileMaxPages+"页限制");
}
// if (!CheckFileHeaderUtil.toCheck(path)){
// throw new ServiceException(ResultServiceEnums.FILE_UNREADABLE);
// }
return pages;
}
public File TXTHandler(File file) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment