AnthoLume/metadata/metadata.go

70 lines
1.4 KiB
Go
Raw Normal View History

2023-09-18 23:57:18 +00:00
package metadata
import (
"errors"
"fmt"
"path/filepath"
2023-09-18 23:57:18 +00:00
"github.com/gabriel-vasile/mimetype"
)
type Source int
const (
GBOOK Source = iota
OLIB
2023-09-18 23:57:18 +00:00
)
type MetadataInfo struct {
ID *string
Title *string
Author *string
Description *string
ISBN10 *string
ISBN13 *string
2023-09-18 23:57:18 +00:00
}
func CacheCover(gbid string, coverDir string, documentID string, overwrite bool) (*string, error) {
// Get Filepath
coverFile := "." + filepath.Clean(fmt.Sprintf("/%s.jpg", documentID))
coverFilePath := filepath.Join(coverDir, coverFile)
// Save Google Books
if err := saveGBooksCover(gbid, coverFilePath, overwrite); err != nil {
return nil, err
2023-09-18 23:57:18 +00:00
}
// TODO - Refactor & Allow Open Library / Alternative Sources
2023-09-18 23:57:18 +00:00
return &coverFile, nil
}
func SearchMetadata(s Source, metadataSearch MetadataInfo) ([]MetadataInfo, error) {
switch s {
case GBOOK:
return getGBooksMetadata(metadataSearch)
case OLIB:
return nil, errors.New("Not implemented")
default:
return nil, errors.New("Not implemented")
}
}
func GetWordCount(filepath string) (int64, error) {
fileMime, err := mimetype.DetectFile(filepath)
if err != nil {
return 0, err
}
if fileExtension := fileMime.Extension(); fileExtension == ".epub" {
2023-10-24 00:18:16 +00:00
totalWords, err := countEPUBWords(filepath)
if err != nil {
return 0, err
}
return totalWords, nil
} else {
return 0, errors.New("Invalid Extension")
}
2023-09-18 23:57:18 +00:00
}