Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Migrate ePub generation to go-epub #679

Merged
merged 21 commits into from Sep 16, 2023
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 29 additions & 3 deletions internal/core/ebook.go
Expand Up @@ -5,12 +5,14 @@ import (
"fmt"
"io"
"log"
"math/rand"
"net/http"
"os"
fp "path/filepath"
"regexp"
"strconv"
"strings"
"time"

"github.com/go-shiori/shiori/internal/model"
"github.com/pkg/errors"
Expand Down Expand Up @@ -60,6 +62,8 @@ func GenerateEbook(req ProcessRequest) (book model.Bookmark, err error) {
}
}
// create epub file
ebookIdentifier := GenerateEPUBIdentifier()

epubFile, err := os.Create(ebookfile)
if err != nil {
return book, errors.Wrap(err, "can't create ebook")
Expand Down Expand Up @@ -101,9 +105,10 @@ func GenerateEbook(req ProcessRequest) (book model.Bookmark, err error) {
return book, errors.Wrap(err, "can't create content.opf")
}
_, err = contentOpfWriter.Write([]byte(`<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="BookId">
<metadata>
<package xmlns="http://www.idpf.org/2007/opf" version="2.0" unique-identifier="` + ebookIdentifier + `">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
Monirzadeh marked this conversation as resolved.
Show resolved Hide resolved
<dc:title>` + book.Title + `</dc:title>
<dc:identifier xmlns:opf="http://www.idpf.org/2007/opf" id="` + ebookIdentifier + `" opf:scheme="uuid">` + ebookIdentifier + `</dc:identifier>
</metadata>
<manifest>
<item id="ncx" href="toc.ncx" media-type="application/x-dtbncx+xml"/>
Expand Down Expand Up @@ -149,7 +154,7 @@ img {
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
<head>
<meta name="dtb:uid" content="urn:uuid:12345678-1234-5678-1234-567812345678"/>
<meta name="dtb:uid" content="urn:uuid:` + ebookIdentifier + `"/>
<meta name="dtb:depth" content="1"/>
<meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
Expand Down Expand Up @@ -252,3 +257,24 @@ func GetImages(html string) (map[string]string, error) {

return images, nil
}

func GenerateEPUBIdentifier() string {
Monirzadeh marked this conversation as resolved.
Show resolved Hide resolved
src := rand.NewSource(time.Now().UnixNano())
rnd := rand.New(src)

// Define the valid characters for the identifier
validChars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_:."

// Generate a random identifier of length 24
identifier := make([]byte, 24)
for i := range identifier {
identifier[i] = validChars[rnd.Intn(len(validChars))]
}

// Ensure the identifier starts with a letter
if identifier[0] >= '0' && identifier[0] <= '9' {
identifier[0] = validChars[rnd.Intn(52)] // Generate a random letter
}

return string(identifier)
}