Commit 45e7bfb0 authored by ale's avatar ale

look for OPF metadata with alternative filenames

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