424 lines
16 KiB
Java
424 lines
16 KiB
Java
package com.peanut.modules.book.controller;
|
|
|
|
import java.io.*;
|
|
import java.util.*;
|
|
import java.util.concurrent.ExecutorService;
|
|
import java.util.concurrent.Executors;
|
|
import com.alibaba.druid.util.StringUtils;
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
import com.peanut.common.utils.*;
|
|
import com.peanut.modules.book.service.*;
|
|
import com.peanut.modules.common.entity.*;
|
|
import com.peanut.modules.oss.service.OssService;
|
|
import lombok.SneakyThrows;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.mock.web.MockMultipartFile;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
/**
|
|
* @author yl
|
|
* @email yl328572838@163.com
|
|
* @date 2022-08-16 14:32:06
|
|
*/
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("book/bookchaptercontent")
|
|
public class BookChapterContentController {
|
|
@Autowired
|
|
private BookChapterContentService bookChapterContentService;
|
|
@Autowired
|
|
private BookService bookService;
|
|
@Autowired
|
|
private OssService ossService;
|
|
@Autowired
|
|
private BookChapterService bookChapterService;
|
|
@Autowired
|
|
private MyUserService myUserService;
|
|
@Autowired
|
|
private UserEbookBuyService userEbookBuyService;
|
|
@Autowired
|
|
private ShopProductBookService shopProductBookService;
|
|
|
|
|
|
//某一内容转成音频
|
|
@RequestMapping("/contentToVoices")
|
|
public R contentToVoices(@RequestBody Map<String,Object> params){
|
|
bookChapterContentService.contentToVoices(params.get("bookChapterContentId").toString());
|
|
return R.ok();
|
|
}
|
|
|
|
//书籍下所有内容转成音频
|
|
@RequestMapping("/bookToShortVoices")
|
|
public R bookToShortVoices(@RequestBody Map<String,Object> params){
|
|
ExecutorService service = Executors.newFixedThreadPool(10);
|
|
List<BookChapterContentEntity> list = bookChapterContentService.list(new LambdaQueryWrapper<BookChapterContentEntity>()
|
|
.eq(BookChapterContentEntity::getBookId,params.get("bookId")));
|
|
for (BookChapterContentEntity bcc:list){
|
|
if (!bcc.getContent().contains("https")&&!"".equals(bcc.getContent())){
|
|
service.execute(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
bookChapterContentService.contentToVoices(bcc.getId()+"");
|
|
}
|
|
});
|
|
}
|
|
}
|
|
return R.ok();
|
|
}
|
|
|
|
//某一章节下所有音频合并成一个音频
|
|
@RequestMapping("/mergeVoices")
|
|
public R mergeVoices(@RequestBody Map<String,Object> params){
|
|
List<BookChapterContentEntity> bccs = bookChapterContentService.list(new LambdaQueryWrapper<BookChapterContentEntity>()
|
|
.eq(BookChapterContentEntity::getBookChatperId,params.get("bookChapterId"))
|
|
.orderByAsc(BookChapterContentEntity::getNumber));
|
|
List<String> list = new ArrayList<>();
|
|
for (BookChapterContentEntity bcc : bccs) {
|
|
if (!StringUtils.isEmpty(bcc.getVoices())){
|
|
list.add(bcc.getVoices());
|
|
}
|
|
}
|
|
String url = BaiduVoicesUtils.mergeVoices(list);
|
|
if (!StringUtils.isEmpty(url)){
|
|
BookChapterEntity bc = bookChapterService.getById(params.get("bookChapterId").toString());
|
|
bc.setVoices(url);
|
|
bookChapterService.updateById(bc);
|
|
return R.ok();
|
|
}else {
|
|
return R.error();
|
|
}
|
|
}
|
|
|
|
//所有章节合并各自音频
|
|
@RequestMapping("/mergeVoicesByBookId")
|
|
@Transactional
|
|
public R mergeVoicesByBookId(@RequestBody Map<String,Object> params){
|
|
StringBuilder sb = new StringBuilder();
|
|
List<BookChapterEntity> bcs = bookChapterService.list(new LambdaQueryWrapper<BookChapterEntity>()
|
|
.eq(BookChapterEntity::getBookId,params.get("bookId"))
|
|
.orderByAsc(BookChapterEntity::getNumber));
|
|
ExecutorService service = Executors.newFixedThreadPool(10);
|
|
for (BookChapterEntity bc:bcs) {
|
|
service.execute(new Runnable() {
|
|
@Override
|
|
public void run() {
|
|
List<BookChapterContentEntity> bccs = bookChapterContentService.list(new LambdaQueryWrapper<BookChapterContentEntity>()
|
|
.eq(BookChapterContentEntity::getBookChatperId,bc.getId())
|
|
.orderByAsc(BookChapterContentEntity::getNumber));
|
|
List<String> list = new ArrayList<>();
|
|
for (BookChapterContentEntity bcc : bccs) {
|
|
if (!StringUtils.isEmpty(bcc.getVoices())){
|
|
list.add(bcc.getVoices());
|
|
}
|
|
}
|
|
String url = BaiduVoicesUtils.mergeVoices(list);
|
|
bc.setVoices(url);
|
|
bookChapterService.updateById(bc);
|
|
}
|
|
});
|
|
}
|
|
return R.ok();
|
|
}
|
|
|
|
//按章节转成长音频
|
|
@RequestMapping("/bookToLongVoices")
|
|
public R bookToLongVoices(@RequestBody Map<String,Object> params){
|
|
List<BookChapterEntity> bookChapters = bookChapterService.list(new LambdaQueryWrapper<BookChapterEntity>()
|
|
.eq(BookChapterEntity::getBookId,params.get("bookId")));
|
|
for (BookChapterEntity bc:bookChapters){
|
|
StringBuffer sb = new StringBuffer();
|
|
List<BookChapterContentEntity> bookChapterContents = bookChapterContentService.list(new LambdaQueryWrapper<BookChapterContentEntity>()
|
|
.eq(BookChapterContentEntity::getBookChatperId,bc.getId()));
|
|
for (BookChapterContentEntity bcc:bookChapterContents){
|
|
if (!bcc.getContent().contains("https")&&!"".equals(bcc.getContent())){
|
|
sb.append(bcc.getContent());
|
|
}
|
|
}
|
|
if (sb.length() > 0) {
|
|
//调用百度语音合成 API
|
|
String voices = BaiduVoicesUtils.longText(sb.toString());
|
|
bc.setVoices(voices);
|
|
bookChapterService.updateById(bc);
|
|
}
|
|
}
|
|
return R.ok();
|
|
}
|
|
|
|
//获取长音频
|
|
@RequestMapping("/queryBookVoices")
|
|
public R queryBookVoices(@RequestBody Map<String,Object> params){
|
|
List<BookChapterEntity> bookChapters = bookChapterService.list(new LambdaQueryWrapper<BookChapterEntity>()
|
|
.eq(BookChapterEntity::getBookId,params.get("bookId")));
|
|
for (BookChapterEntity bc:bookChapters){
|
|
if (!StringUtils.isEmpty(bc.getVoices())&&!bc.getVoices().contains("http")) {
|
|
//调用百度语音合成 API
|
|
String voices = BaiduVoicesUtils.queryVoice(bc.getVoices());
|
|
bc.setVoices(voices);
|
|
bookChapterService.updateById(bc);
|
|
}
|
|
}
|
|
return R.ok();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
* 列表
|
|
*/
|
|
@RequestMapping("/list")
|
|
public R list(@RequestParam Map<String, Object> params) {
|
|
PageUtils page = bookChapterContentService.queryPage(params);
|
|
|
|
return R.ok().put("page", page);
|
|
}
|
|
|
|
/**
|
|
* 信息
|
|
*/
|
|
@RequestMapping("/info/{id}")
|
|
public R info(@PathVariable("id") Integer id) {
|
|
BookChapterContentEntity bookChapterContent = bookChapterContentService.getById(id);
|
|
|
|
return R.ok().put("bookChapterContent", bookChapterContent);
|
|
}
|
|
|
|
/**
|
|
* 保存
|
|
*/
|
|
@RequestMapping("/save")
|
|
public R save(@RequestBody BookChapterContentEntity bookChapterContent) {
|
|
bookChapterContentService.save(bookChapterContent);
|
|
|
|
return R.ok();
|
|
}
|
|
|
|
/**
|
|
* 修改
|
|
*/
|
|
@RequestMapping("/update")
|
|
public R update(@RequestBody BookChapterContentEntity bookChapterContent) {
|
|
bookChapterContentService.updateById(bookChapterContent);
|
|
|
|
return R.ok();
|
|
}
|
|
|
|
/**
|
|
* 删除
|
|
*/
|
|
@RequestMapping("/delete")
|
|
public R delete(@RequestBody Integer[] ids) {
|
|
bookChapterContentService.removeByIds(Arrays.asList(ids));
|
|
|
|
return R.ok();
|
|
}
|
|
|
|
|
|
/**
|
|
* 章节拆分转换单句
|
|
*/
|
|
@RequestMapping("/getBookVoices")
|
|
public R getBookVoices(@RequestParam("id") Integer id) {
|
|
|
|
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
|
|
singleThreadExecutor.execute(() -> bookChapterContentService.getWordChapterParagraph(id));
|
|
|
|
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
|
|
bookEntity.setContentStatus("2");
|
|
bookService.updateById(bookEntity);
|
|
|
|
return R.ok();
|
|
}
|
|
|
|
/**
|
|
* 章节单句 转成音频
|
|
*/
|
|
@RequestMapping("/signVoices")
|
|
public R signVoices(@RequestParam("content") String content) throws Exception {
|
|
//调用百度语音合成 API
|
|
String voices = BaiduVoicesUtils.run(content);
|
|
File file = new File(voices);
|
|
if (!file.exists()) {
|
|
return R.error("语音文件未生成");
|
|
}
|
|
FileInputStream fileInputStream = new FileInputStream(file);
|
|
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(fileInputStream));
|
|
String path = ossService.uploadFileAvatar(multipartFile);
|
|
fileInputStream.close();
|
|
file.delete();
|
|
|
|
if (StringUtils.isEmpty(path)) {
|
|
return R.error("语音上传失败");
|
|
}
|
|
|
|
return R.ok().put("voices", path);
|
|
}
|
|
|
|
/**
|
|
* 全部章节单句 转成音频
|
|
*/
|
|
@RequestMapping("/allVoices")
|
|
public R allVoices(@RequestParam("id") Integer id) throws Exception {
|
|
|
|
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
|
|
//创建单线程
|
|
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
|
|
singleThreadExecutor.execute(new Runnable() {
|
|
@SneakyThrows
|
|
@Override
|
|
public void run() {
|
|
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
|
|
List<BookChapterContentEntity> book_id = bookChapterContentService.getBaseMapper().selectList(new QueryWrapper<BookChapterContentEntity>()
|
|
.eq("book_id", id));
|
|
|
|
for (BookChapterContentEntity bookContent : book_id) {
|
|
String content = bookContent.getContent();
|
|
//生成相应的语音文件
|
|
String voices = BaiduVoicesUtils.run(content);
|
|
|
|
File file = new File(voices);
|
|
if (!file.exists()) {
|
|
bookEntity.setVoicesStatus("3");
|
|
}
|
|
FileInputStream fileInputStream = new FileInputStream(file);
|
|
|
|
//把 voices 音频文件读入到内存中并将其转换成MultipartFile格式文件 multipartFile 用于OSS上传
|
|
// application/x-www-form-urlencoded form表单数据被编码为key/value格式发送到服务器
|
|
// text/plain 纯文本格式
|
|
// MultipartFile multipartFile =new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(fileInputStream));
|
|
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(fileInputStream));
|
|
//上传 multipartFile 文件到阿里云OSS上
|
|
String path = ossService.uploadFileAvatar(multipartFile);
|
|
|
|
bookContent.setVoices(path);
|
|
|
|
fileInputStream.close();
|
|
|
|
file.delete();
|
|
|
|
bookChapterContentService.updateById(bookContent);
|
|
if (StringUtils.isEmpty(path)) {
|
|
bookEntity.setVoicesStatus("3");
|
|
}
|
|
}
|
|
bookEntity.setVoicesStatus("2");
|
|
bookService.updateById(bookEntity);
|
|
}
|
|
});
|
|
bookEntity.setVoicesStatus("1");
|
|
bookService.updateById(bookEntity);
|
|
return R.ok();
|
|
}
|
|
|
|
//章节2.0转音频
|
|
@RequestMapping("/AllVOices")
|
|
public R allVoicess2(@RequestParam("id") Integer id) throws Exception {
|
|
|
|
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
|
|
//创建单线程
|
|
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
|
|
singleThreadExecutor.execute(new Runnable() {
|
|
@SneakyThrows
|
|
@Override
|
|
public void run() {
|
|
BookEntity bookEntity = bookService.getBaseMapper().selectById(id);
|
|
|
|
List<BookChapterEntity> book_id = bookChapterService.getBaseMapper().selectList(new QueryWrapper<BookChapterEntity>().eq("book_id", id));
|
|
|
|
|
|
for (BookChapterEntity bookChapter : book_id) {
|
|
String content = bookChapter.getContent();
|
|
//生成相应的语音文件
|
|
String voices = BaiduVoicesUtils.run(content);
|
|
|
|
File file = new File(voices);
|
|
if (!file.exists()) {
|
|
bookEntity.setVoicesStatus("3");
|
|
}
|
|
FileInputStream fileInputStream = new FileInputStream(file);
|
|
MultipartFile multipartFile = new MockMultipartFile("file", file.getName(), "text/plain", IOUtils.toByteArray(fileInputStream));
|
|
//上传 multipartFile 文件到阿里云OSS上
|
|
String path = ossService.uploadFileAvatar(multipartFile);
|
|
|
|
bookChapter.setVoices(path);
|
|
|
|
fileInputStream.close();
|
|
|
|
file.delete();
|
|
|
|
bookChapterService.updateById(bookChapter);
|
|
if (StringUtils.isEmpty(path)) {
|
|
bookEntity.setVoicesStatus("3");
|
|
}
|
|
}
|
|
bookEntity.setVoicesStatus("2");
|
|
bookService.updateById(bookEntity);
|
|
}
|
|
});
|
|
bookEntity.setVoicesStatus("1");
|
|
bookService.updateById(bookEntity);
|
|
return R.ok();
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 获取章节音频地址
|
|
*
|
|
* @return
|
|
*/
|
|
@RequestMapping("/getBooksCatalogue")
|
|
public R getBooksCatalogue(@RequestParam Integer chapterId,
|
|
@RequestParam Integer bookId,
|
|
@RequestParam Integer userId) {
|
|
//鉴权阶段
|
|
BookEntity book_info = bookService.getById(bookId);
|
|
BookChapterEntity chapter_info = bookChapterService.getById(chapterId);
|
|
|
|
UserEbookBuyEntity userEbookBuyEntity = userEbookBuyService.getBaseMapper().selectOne(new LambdaQueryWrapper<UserEbookBuyEntity>().
|
|
eq(UserEbookBuyEntity::getUserId, userId).eq(UserEbookBuyEntity::getBookId, bookId));
|
|
if (book_info.getBookType()==0&&chapter_info.getNumber() > book_info.getFreeChapterCount() && userEbookBuyEntity == null) {
|
|
Integer productByBookId = shopProductBookService.getProductByBookId(bookId);
|
|
return R.ok().put("jq", false).put("product", productByBookId);
|
|
}
|
|
|
|
|
|
return R.ok().put("chapter", chapter_info);
|
|
}
|
|
|
|
//生成电子书 目录
|
|
@RequestMapping("/getWordChapterParagraph")
|
|
public R getWordChapterParagraph(@RequestParam Map<String, Object> params) {
|
|
bookChapterContentService.getWordChapterParagraph(14);
|
|
return R.ok();
|
|
}
|
|
|
|
}
|