Skip to content
Snippets Groups Projects
Commit 45e7bfb0 authored by ale's avatar ale
Browse files

look for OPF metadata with alternative filenames

parent f1e244e1
No related branches found
No related tags found
No related merge requests found
......@@ -2,6 +2,7 @@ package liber
import (
"encoding/xml"
"errors"
"io"
"os"
"path/filepath"
......@@ -75,29 +76,27 @@ func opfOpen(path string) (*Metadata, error) {
return opfParse(file)
}
func opfMetadataPath(epubPath string) string {
return filepath.Join(filepath.Dir(epubPath), "metadata.opf")
}
func opfCoverPath(epubPath string) string {
return filepath.Join(filepath.Dir(epubPath), "cover.jpg")
func replaceExt(path, ext string) string {
return strings.TrimSuffix(path, filepath.Ext(path)) + ext
}
type opfProvider struct{}
func (p *opfProvider) Lookup(storage *FileStorage, path, filetype string) (*Metadata, error) {
if !storage.Exists(opfMetadataPath(path)) {
return nil, nil
paths := []string{
filepath.Join(filepath.Dir(path), "metadata.opf"),
replaceExt(path, ".opf"),
}
m, err := opfOpen(opfMetadataPath(storage.Abs(path)))
if err != nil {
return nil, err
for _, opfpath := range paths {
if m, err := opfOpen(storage.Abs(opfpath)); err == nil {
return m, err
}
}
return m, err
return nil, errors.New("no OPF metadata found")
}
func (p *opfProvider) GetBookCover(storage *FileStorage, path string) (string, error) {
coverPath := opfCoverPath(path)
coverPath := filepath.Join(filepath.Dir(path), "cover.jpg")
if storage.Exists(coverPath) {
return coverPath, nil
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment