1
0

feat: 增加书籍导出功能

This commit is contained in:
2024-12-22 22:58:08 +08:00
parent 327fa30ab8
commit a3985ed087
3 changed files with 44 additions and 2 deletions

8
.idea/GitCommitMessageStorage.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GitCommitMessageStorage">
<option name="messageStorage">
<MessageStorage />
</option>
</component>
</project>

View File

@@ -1,10 +1,14 @@
package com.lanyuanxiaoyao.bookstore.controller package com.lanyuanxiaoyao.bookstore.controller
import cn.hutool.core.io.IoUtil
import cn.hutool.core.util.IdUtil import cn.hutool.core.util.IdUtil
import cn.hutool.core.util.StrUtil
import cn.hutool.core.util.URLUtil
import com.lanyuanxiaoyao.bookstore.Book import com.lanyuanxiaoyao.bookstore.Book
import com.lanyuanxiaoyao.bookstore.BookRepository import com.lanyuanxiaoyao.bookstore.BookRepository
import com.lanyuanxiaoyao.bookstore.PageResponse import com.lanyuanxiaoyao.bookstore.PageResponse
import jakarta.annotation.Resource import jakarta.annotation.Resource
import jakarta.servlet.http.HttpServletResponse
import jakarta.transaction.Transactional import jakarta.transaction.Transactional
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.springframework.data.domain.PageRequest import org.springframework.data.domain.PageRequest
@@ -27,7 +31,7 @@ class BookController {
@GetMapping("list") @GetMapping("list")
fun list( fun list(
@RequestParam("page", defaultValue = "1") page: Int, @RequestParam("page", defaultValue = "1") page: Int,
@RequestParam("size", defaultValue = "10") size: Int @RequestParam("size", defaultValue = "10") size: Int,
): PageResponse<ViewItem> { ): PageResponse<ViewItem> {
val pageable = bookRepository.findAll(PageRequest.of(0.coerceAtLeast(page - 1), size)) val pageable = bookRepository.findAll(PageRequest.of(0.coerceAtLeast(page - 1), size))
return PageResponse( return PageResponse(
@@ -67,6 +71,30 @@ class BookController {
return bookRepository.findAllTag() return bookRepository.findAllTag()
} }
@Transactional
@GetMapping("export/{bookId}")
fun export(@PathVariable("bookId") bookId: String, response: HttpServletResponse) {
val book = bookRepository.findById(bookId).orElseThrow()
val builder = StringBuilder()
for (chapter in book.chapters.sortedBy { it.sequence }) {
if (chapter.name.isNullOrBlank()) {
builder.appendLine("${chapter.sequence}")
} else {
builder.appendLine("${chapter.sequence}${chapter.name}")
}
builder.appendLine()
for (line in chapter.content.sortedBy { it.sequence }) {
builder.appendLine(line.text)
}
builder.appendLine()
}
response.setHeader("Access-Control-Expose-Headers", "Content-Type")
response.setHeader("Content-Type", "text/plain")
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition")
response.setHeader("Content-Disposition", StrUtil.format("attachment; filename={}", URLUtil.encodeAll("${book.name}.txt")))
IoUtil.copy(builder.toString().byteInputStream(), response.outputStream)
}
data class ViewItem( data class ViewItem(
val bookId: String?, val bookId: String?,
val name: String, val name: String,

View File

@@ -76,7 +76,7 @@
label: '操作', label: '操作',
fixed: 'right', fixed: 'right',
className: 'nowrap', className: 'nowrap',
width: 250, width: 100,
buttons: [ buttons: [
{ {
type: 'action', type: 'action',
@@ -85,6 +85,12 @@
url: '${source}', url: '${source}',
blank: true, blank: true,
}, },
{
type: 'action',
label: '导出',
actionType: 'download',
api: '${base}/book/export/${bookId}',
},
{ {
type: 'action', type: 'action',
label: '编辑', label: '编辑',