提交 4b48b7ae authored 作者: renjiancai's avatar renjiancai

--no commit message

上级 1f50a18d
package com.zrqx.file.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import nl.siegmann.epublib.domain.TOCReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.zrqx.core.exception.BaseException;
import com.zrqx.core.model.file.FileInfo;
import com.zrqx.core.model.resource.articlelibrary.ArticleLibrary;
import com.zrqx.core.model.resource.articlelibrary.ChapterLibrary;
import com.zrqx.core.model.resource.ebook.Book;
import com.zrqx.core.model.resource.ebook.Ebook;
import com.zrqx.core.model.resource.imagelibrary.ImageLibrary;
import com.zrqx.core.util.datatype.DateUtils;
import com.zrqx.core.util.datatype.UUIDUtil;
import com.zrqx.core.vo.resource.EpubVo;
import com.zrqx.file.commons.Redis;
import com.zrqx.file.service.FileService;
import com.zrqx.file.util.EpubResolve;
import com.zrqx.file.util.EpubUtil;
import com.zrqx.file.util.MockMultipartFile;
@RestController
@RequestMapping(value = "/epub")
@Api(description = "文件上传下载服务")
public class EpubController {
private final static Logger logger = LoggerFactory.getLogger(EpubController.class);
@Autowired
private FileService service;
@Value("${file-root-path}")
private String rootPath;
@Autowired
private Redis redis;
private EpubVo ev = new EpubVo();
@ApiOperation(value = "epub上传 返回token")
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public String upload(Integer dtId ,String code, Integer nationsType, @RequestParam("file") MultipartFile file) throws Exception {
// 获取原文件的全名称
String originalFilename = file.getOriginalFilename();
// 源文件的名称,不带后缀
String fileName = originalFilename.substring(0, originalFilename.lastIndexOf("."));
FileInfo fileInfo = new FileInfo();
fileInfo.setOriginalFileName(originalFilename);
int count = service.selectCount(fileInfo);
if (count > 0) {
throw new BaseException("epub已存在");
}
ev.setCode(code);
ev.setDtId(dtId);
Ebook ebook = new Ebook();
Book book = new Book();
book.setId(UUIDUtil.getUUID());
// epub工具类
EpubUtil epubUtil = new EpubUtil();
// epub解析方法类
EpubResolve er = new EpubResolve();
//book.setNationsType(nationsType);
epubUtil.setEpubFile(file.getInputStream());
FileInfo entity = er.uploadFile(file, rootPath + fileName);
if (!service.insert(entity)) {
throw new BaseException("上传失败!");
} else {
String path = entity.getPath();
// 载入电子书资源
epubUtil.setEpubFile(file.getInputStream());
/**
* 保存元数据相关内容
*/
// 名称
book.setName(epubUtil.getBookTitle());
// 作者
book.setAuthor(epubUtil.getAuthor());
// 出版时间
book.setPublishTime(DateUtils.dateTimeToStrYMD(epubUtil.getPublishDate()));
// 出版单位
book.setPublisher(epubUtil.getPublisher());
// isbn
book.setIsbn(epubUtil.getISBN());
ev.setBook(book);
/**
* 保存电子书相关内容
*/
ebook.setId(book.getId());
ebook.setUploadTime(new Date());
// 修改时间4
ebook.setUpdateTime(new Date());// 设置为上传时间
// 关键字(图书的名字)20
ebook.setKeywords(epubUtil.getBookTitle());
ebook.setEpubFile(path);
ebook.setFileName(entity.getFileName());
ebook.setSynopsis(epubUtil.getDescription());
// css路径
FileInfo cssentity = er.getCss(epubUtil, rootPath + fileName);
if (!service.insert(cssentity)) {
throw new BaseException("css文件上传失败!");
}
ebook.setCssPath(cssentity.getPath());
// 保存封面
if(epubUtil.getCover() != null){
String imgName = epubUtil.getCover().getHref().substring(epubUtil.getCover().getHref().lastIndexOf("/") + 1);
MultipartFile multipartFile = new MockMultipartFile(imgName, imgName, "image/jpeg", epubUtil.getCover().getInputStream());
FileInfo cover = er.uploadFile(multipartFile, rootPath + fileName);
ebook.setImg(cover.getFileName());
}
ev.setEbook(ebook);
// ---------获取图书所有内容------------
List<TOCReference> listReferences = epubUtil.getTocReferences();
// 当前目录
TOCReference tocRef = new TOCReference();
// 初始化顶级目录id 为 "0"
tocRef.setFragmentId("0");
// 初始化顶级目录层级为 0
int level = 0;
// *******************解析章节信息*************
Set<ChapterLibrary> chapterSet = new HashSet<>();
chapterSet = er.setChapter(listReferences, tocRef, level, ebook, chapterSet);
ev.setChapterSet(chapterSet);
// ******************解析内容信息**************
Set<ArticleLibrary> contentSet = new HashSet<>();
Map<String, String> imgtzMap = new HashMap<>();// 图片图注信息
Object[] data = new Object[2];
data = er.setContent(listReferences, contentSet, ebook, imgtzMap, data);
ev.setArticleSet(contentSet);
// ************导入图书中的图片资源************
if (data[1] instanceof Map && data[1] != null) {
imgtzMap = (Map<String, String>) data[1];
// 图片info
List<FileInfo> imageInfoList = er.getImages(epubUtil, rootPath + fileName);
if (!service.insertList(imageInfoList)) {
throw new BaseException("图片上传失败!");
}
importBookImgs(imgtzMap, book, code, rootPath + fileName, imageInfoList);
}
redis.set(entity.getFileName(), ev);
}
return entity.getFileName();
}
/**
* (后台) 导入epub中的图片资源到resource
*
* @throws Exception
*/
public void importBookImgs(Map<String, String> imgtzMap, Book book, String classifyCode, String relativeBookFolderPath, List<FileInfo> imageInfoList) throws Exception {
Set<ImageLibrary> imageSet = new HashSet<ImageLibrary>();
try {
// 保存图书中的图片资源到资源表中
for (FileInfo file : imageInfoList) {
ImageLibrary il = new ImageLibrary();
String imgName = file.getPath().substring(file.getPath().lastIndexOf("/") + 1);
String showName = imgtzMap.get(file.getPath());
// 图片的页面显示名称(图注),没有默认为原文件名
showName = (showName != null && !showName.equals("")) ? showName : imgName;
il.setName(showName);
// il.setImage(relativeBookFolderPath + file.getPath());
il.setUploadTime(new Date());
il.setBookId(book.getId());
//il.setNationsType(book.getNationsType());
il.setBookName(book.getName());
il.setStatus(0);
il.setImage(file.getFileName());
imageSet.add(il);
}
ev.setImageSet(imageSet);
} catch (Exception e) {
// 保存操作日志
throw new Exception();
}
}
}
...@@ -195,14 +195,6 @@ public class FileController { ...@@ -195,14 +195,6 @@ public class FileController {
DownloadUtil.start(response, f, fileName); DownloadUtil.start(response, f, fileName);
} }
} }
@ApiOperation(value = "生成二维码zip")
@RequestMapping(value = "/download/zip", method = RequestMethod.POST)
public String downLoad(HttpServletResponse response,@RequestBody List<String> fileNames) throws Exception {
Example example =service.createExample();
example.createCriteria().andIn("fileName", fileNames);
List<FileInfo> list = service.selectByExample(example);
return ZipUtil.zip(rootPath, list);
}
@ApiOperation(value = "导出二维码zip") @ApiOperation(value = "导出二维码zip")
@RequestMapping(value = "/download/zip", method = RequestMethod.GET) @RequestMapping(value = "/download/zip", method = RequestMethod.GET)
public String downLoad(HttpServletResponse response,String fileName) throws Exception { public String downLoad(HttpServletResponse response,String fileName) throws Exception {
......
package com.zrqx.file.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.zip.ZipInputStream;
import nl.siegmann.epublib.domain.Author;
import nl.siegmann.epublib.domain.Book;
import nl.siegmann.epublib.domain.Date;
import nl.siegmann.epublib.domain.Identifier;
import nl.siegmann.epublib.domain.MediaType;
import nl.siegmann.epublib.domain.Metadata;
import nl.siegmann.epublib.domain.Resource;
import nl.siegmann.epublib.domain.Resources;
import nl.siegmann.epublib.domain.SpineReference;
import nl.siegmann.epublib.domain.TOCReference;
import nl.siegmann.epublib.epub.EpubReader;
import org.apache.commons.lang3.StringUtils;
import com.zrqx.core.util.datatype.ArrayUtils;
/**
* epub 数字书籍格式 数据工具包
* @author Administrator
*
*/
public class EpubUtil {
private Book book=null;
private EpubReader epubReader=null;
/**
* 设置Book实例对象
* @return
*/
public EpubUtil setEpubFile(File file){
try {
return setEpubFile(new FileInputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return this;
}
/**
* 设置Book实例对象
* @return
*/
public EpubUtil setEpubFile(InputStream in){
try {
epubReader=getEpubReader();
book=epubReader.readEpub(in);
} catch (IOException e) {
e.printStackTrace();
}
return this;
}
/**
* 设置Book实例对象
* @return
*/
public EpubUtil setEpubFile(InputStream in, String encoding){
try {
epubReader=getEpubReader();
book=epubReader.readEpub(in,encoding);
} catch (IOException e) {
e.printStackTrace();
}
return this;
}
/**
* 设置Book实例对象
* @return
*/
public EpubUtil setEpubFile(ZipInputStream in){
try {
epubReader=getEpubReader();
book=epubReader.readEpub(in);
} catch (IOException e) {
e.printStackTrace();
}
return this;
}
/**
* 设置Book实例对象
* @return
*/
public EpubUtil setEpubFile(ZipInputStream in,String encoding){
try {
epubReader=getEpubReader();
book=epubReader.readEpub(in,encoding);
} catch (IOException e) {
e.printStackTrace();
}
return this;
}
/**
* 获取所有的资源对象
* @return
*/
public Resources getResources(){
return book.getResources();
}
/**
* 获取封面
*/
public Resource getCover(){
//epub格式的书籍中都没有将封面图片的信息放到<metadata>标签下的cover标签中。所以用第二种fangs
Resource res = book.getCoverImage();
if(res!=null){
return res;
}else{
Resources ress = book.getResources();
res = ress.getById("cover");
return res;
}
}
/**
* 获取制定的文件资源
* @param mediaTypes
* @return
*/
public List<Resource> getResources(MediaType ... mediaTypes){
return getResources().getResourcesByMediaTypes(mediaTypes);
}
/**
* 获取图书的css资源文件
* @return
*/
public Resource getCssResource(){
List<Resource> list=getResources(new MediaType("text/css","css"));
if(list!=null && list.size()>0){
return list.get(0);
}
return null;
}
/**
* 获取图书的图片资源
* @return
*/
public List<Resource> getImagesResources(){
return getResources(
new MediaType("image/jpeg","jpg"),
new MediaType("image/gif","gif"),
new MediaType("image/png","png")
);
}
/**
*获取根据阅读顺序获取图书的内容资源(html)文件资源
* @return
*/
public List<SpineReference> getSpineReferences(){
return book.getSpine().getSpineReferences();
}
/**
*获取图书的目录结构标题及对应的资源内容
* @return
*/
public List<TOCReference> getTocReferences(){
return book.getTableOfContents().getTocReferences();
}
/**
* 获取图书
* @return
* author:haopeng
*/
public Book getBook(){
return book;
}
/**
* 获取图书信息
* @return
* author:haopeng
* @throws Exception
*/
public Metadata getMetadata() throws Exception{
if(book==null){
throw new NullPointerException("图书Book属性为null");
}
return book.getMetadata();
}
/**
* 获取文件读取器
* @return
*/
private EpubReader getEpubReader(){
if(epubReader==null){
epubReader=new EpubReader();
}
return epubReader;
}
/**
* 获取图书ISBN号
* @throws Exception
*/
public String getISBN() throws Exception{
String isbn=null;
List<Identifier> identifiers=getMetadata().getIdentifiers();
for(Identifier identifier:identifiers){
String value=identifier.getValue();
// if(value.matches("^97[8|9]\\d{10}$") ){//新版isbn号格式
if(value.contains("-")){
value=value.replaceAll("-", "");
isbn=value;
break;
}
}
return isbn;
}
/**
* 获取图书作者
*/
public String getAuthor(){
List<Author> authors = null;
try {
authors = getMetadata().getAuthors();
} catch (Exception e) {
e.printStackTrace();
}
String author = "";
if(authors.size()>0){
author = authors.get(0).getFirstname()+authors.get(0).getLastname();
author = author.replaceAll("[\\[\\],]"," ");
}
return author;
}
/**
* 获取出版日期
*/
public java.util.Date getPublishDate(){
List<Date> dates = null;
try {
dates = getMetadata().getDates();
} catch (Exception e) {
e.printStackTrace();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyy");
String tempDate = "";
if(dates.size()>0){
if(dates.get(0).toString().contains(".")){
sdf= new SimpleDateFormat("yyy.MM");
}
if(dates.get(0).toString().contains("-")){
sdf= new SimpleDateFormat("yyy-MM");
}
tempDate= dates.get(0).toString();
}
java.util.Date publicDate = null;
try {
if(tempDate!=null && !tempDate.equals("")){
publicDate = sdf.parse(tempDate);
}
} catch (ParseException e) {
System.out.println("日期转换错误!");
e.printStackTrace();
}
return publicDate;
}
/**
* 出版单位
*/
public String getPublisher(){
String publisher = "";
List<String> publishers;
try {
publishers = getMetadata().getPublishers();
for(String p : publishers){
if(p.contains("-")){
publisher = publisher + "、" + p;
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
if(StringUtils.isBlank(publisher)){
return null;
}
return publisher;
}
/**
* 获取图书的名字
*/
public String getBookTitle(){
String bookTitle = "";
//List<String> bookTitle2 = null;
try {
bookTitle = getMetadata().getFirstTitle();
//bookTitle2 = getMetadata().getTitles();
} catch (Exception e) {
e.printStackTrace();
}
return bookTitle;
}
/**
* 简介
*/
public String getDescription(){
String description = "";
List<String> descriptions;
try {
descriptions = getMetadata().getDescriptions();
/*for(String p : descriptions){
if(p.contains("-")){
publisher = publisher + "、" + p;
break;
}
}*/
if(ArrayUtils.isNotEmpty(descriptions)){
description = descriptions.get(0);
}
} catch (Exception e) {
e.printStackTrace();
}
if(StringUtils.isBlank(description)){
return null;
}
return description;
}
}
Manifest-Version: 1.0
Implementation-Title: com.zrqx.file
Implementation-Version: 4.0.0
Built-By: Administrator
Implementation-Vendor-Id: com.zrqx.file
Build-Jdk: 1.8.0_152
Implementation-URL: https://projects.spring.io/spring-boot/#/spring-bo
ot-starter-parent/com.zrqx.pom/com.zrqx.file
Created-By: Maven Integration for Eclipse
#Generated by Maven Integration for Eclipse
#Thu Mar 07 10:12:00 CST 2019
version=4.0.0
groupId=com.zrqx.file
m2e.projectName=com.zrqx.file
m2e.projectLocation=D\:\\work\\workspacezcq\\com.zrqx.file
artifactId=com.zrqx.file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zrqx</groupId>
<artifactId>com.zrqx.pom</artifactId>
<version>4.0.0</version>
<relativePath>../com.zrqx.pom</relativePath>
</parent>
<groupId>com.zrqx.file</groupId>
<artifactId>com.zrqx.file</artifactId>
<dependencies>
<!--视频时长 -->
<dependency>
<groupId>it.sauronsoftware.jave</groupId>
<artifactId>jave</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>
<!-- <dependency> <groupId>com.positiondev.epublib</groupId> <artifactId>epublib-core</artifactId>
<version>3.1</version> </dependency> -->
<dependency>
<groupId>com.github.junrar</groupId>
<artifactId>junrar</artifactId>
<version>0.7</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>com.zrqx</groupId>
<artifactId>com.zrqx.core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<!--熔断器 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--表示为web工程 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Springsecurity给服务端提供安全访问 -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency> -->
<!-- 用于健康监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId>
</dependency> -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--分布式事务 -->
<!-- <dependency> <groupId>com.codingapi</groupId> <artifactId>transaction-springcloud</artifactId>
<exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>*</artifactId>
</exclusion> </exclusions> </dependency> <dependency> <groupId>com.codingapi</groupId>
<artifactId>tx-plugins-db</artifactId> <exclusions> <exclusion> <groupId>org.slf4j</groupId>
<artifactId>*</artifactId> </exclusion> </exclusions> </dependency> -->
<!--swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
</dependency>
<!--用于测试的 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 引入epublib -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.8.3</version>
</dependency>
<dependency>
<groupId>com.positiondev.epublib</groupId>
<artifactId>epublib-core</artifactId>
<version>3.1</version>
</dependency>
<!-- 热部署工具 -->
<!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId>
</dependency> -->
</dependencies>
</project>
\ No newline at end of file
...@@ -41,4 +41,4 @@ ...@@ -41,4 +41,4 @@
</includes> </includes>
</fileSet> </fileSet>
</fileSets> </fileSets>
</assembly> </assembly>
\ No newline at end of file \ No newline at end of file
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论