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