Skip to content

Commit

Permalink
feat: read epub file (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathieu Keller committed Apr 22, 2023
1 parent b23ad3c commit 225f1bc
Show file tree
Hide file tree
Showing 10 changed files with 97 additions and 65 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ name: Build

on:
push:
branches:
- 'main'

jobs:
test:
Expand All @@ -24,6 +22,7 @@ jobs:
run: go test -v ./...
release:
needs: [test]
if: github.ref == 'refs/heads/main'
name: create release
runs-on: ubuntu-latest
steps:
Expand Down
4 changes: 0 additions & 4 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ name: "CodeQL"

on:
push:
pull_request:
branches:
- 'dev'
- 'main'
schedule:
- cron: '42 15 * * 1'
jobs:
Expand Down
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Epub parser

This parser only supports epub version 3.0 so far.
This parser also only reads the MetaData.

### How to use?
```go
zipReader, err := zip.OpenReader("./test_epub_v3_0.epub")
if err != nil {
t.Log(err.Error())
t.Fail()
}
defer zipReader.Close()

book, err := OpenBook(zipReader)
if err != nil {
t.Log(err.Error())
t.Fail()
}
```
After that, all metadata can be found in the book Object
58 changes: 58 additions & 0 deletions book.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package epub

import (
"archive/zip"
"encoding/xml"
"fmt"
"io"
"path"
)

type Book struct {
Opf Package
Container Container
ZipReader *zip.ReadCloser
}

func OpenBook(reader *zip.ReadCloser) (*Book, error) {
book := &Book{ZipReader: reader}
err := book.readXML("META-INF/container.xml", &book.Container)
if err != nil {
return nil, err
}
err = book.readXML(book.Container.Rootfile.Path, &book.Opf)
if err != nil {
return nil, err
}
if book.Opf.Version != "3.0" {
return nil, fmt.Errorf("%s not supported yet!", book.Opf.Version)
}
return book, nil
}

func (book *Book) Open(fileName string) (io.ReadCloser, error) {
return book.open(book.getFileFromRootPath(fileName))
}

func (book *Book) getFileFromRootPath(fileName string) string {
return path.Join(path.Dir(book.Container.Rootfile.Path), fileName)
}

func (book *Book) readXML(fileName string, targetStruct interface{}) error {
reader, err := book.open(fileName)
if err != nil {
return err
}
defer reader.Close()
dec := xml.NewDecoder(reader)
return dec.Decode(targetStruct)
}

func (book *Book) open(fileName string) (io.ReadCloser, error) {
for _, file := range book.ZipReader.File {
if file.Name == fileName {
return file.Open()
}
}
return nil, fmt.Errorf("file %s not exist", fileName)
}
10 changes: 10 additions & 0 deletions container.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package epub

type Container struct {
Rootfile Rootfile `xml:"rootfiles>rootfile"`
}

type Rootfile struct {
Path string `xml:"full-path,attr"`
Type string `xml:"media-type,attr"`
}
17 changes: 0 additions & 17 deletions opfReader.go

This file was deleted.

File renamed without changes.
14 changes: 7 additions & 7 deletions parser_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
package epub

import (
"io/ioutil"
"os"
"archive/zip"
"strconv"
"testing"
)

func Test_parse_epub_3_0_opf(t *testing.T) {
b, err := os.Open("./test_opf.xml")
zipReader, err := zip.OpenReader("./test_epub_v3_0.epub")
if err != nil {
t.Log(err.Error())
t.Fail()
}
defer b.Close()
reader := ioutil.NopCloser(b)
defer reader.Close()
opf, err := ReadOpf(reader)
defer zipReader.Close()

book, err := OpenBook(zipReader)
if err != nil {
t.Log(err.Error())
t.Fail()
}
opf := book.Opf

assertEquals(t, opf.Lang, "us")
assertEquals(t, opf.UniqueIdentifier, "p1234")
assertEquals(t, opf.Version, "3.0")
Expand Down
Binary file added test_epub_v3_0.epub
Binary file not shown.
35 changes: 0 additions & 35 deletions test_opf.xml

This file was deleted.

0 comments on commit 225f1bc

Please sign in to comment.