tests(all): improve tests, refactor(api): saving books
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
110
metadata/_test_files/gbooks_id_response.json
Normal file
110
metadata/_test_files/gbooks_id_response.json
Normal file
@@ -0,0 +1,110 @@
|
||||
{
|
||||
"kind": "books#volume",
|
||||
"id": "ZxwpakTv_MIC",
|
||||
"etag": "mhqr3GsebaQ",
|
||||
"selfLink": "https://www.googleapis.com/books/v1/volumes/ZxwpakTv_MIC",
|
||||
"volumeInfo": {
|
||||
"title": "Alice in Wonderland",
|
||||
"authors": [
|
||||
"Lewis Carroll"
|
||||
],
|
||||
"publisher": "The Floating Press",
|
||||
"publishedDate": "2009-01-01",
|
||||
"description": "Alice in Wonderland (also known as Alice's Adventures in Wonderland), from 1865, is the peculiar and imaginative tale of a girl who falls down a rabbit-hole into a bizarre world of eccentric and unusual creatures. Lewis Carroll's prominent example of the genre of \"literary nonsense\" has endured in popularity with its clever way of playing with logic and a narrative structure that has influence generations of fiction writing.",
|
||||
"industryIdentifiers": [
|
||||
{
|
||||
"type": "ISBN_10",
|
||||
"identifier": "1877527815"
|
||||
},
|
||||
{
|
||||
"type": "ISBN_13",
|
||||
"identifier": "9781877527814"
|
||||
}
|
||||
],
|
||||
"readingModes": {
|
||||
"text": true,
|
||||
"image": false
|
||||
},
|
||||
"pageCount": 104,
|
||||
"printedPageCount": 112,
|
||||
"printType": "BOOK",
|
||||
"categories": [
|
||||
"Fiction / Classics",
|
||||
"Juvenile Fiction / General"
|
||||
],
|
||||
"averageRating": 5,
|
||||
"ratingsCount": 1,
|
||||
"maturityRating": "NOT_MATURE",
|
||||
"allowAnonLogging": true,
|
||||
"contentVersion": "0.2.3.0.preview.2",
|
||||
"panelizationSummary": {
|
||||
"containsEpubBubbles": false,
|
||||
"containsImageBubbles": false
|
||||
},
|
||||
"imageLinks": {
|
||||
"smallThumbnail": "http://books.google.com/books/content?id=ZxwpakTv_MIC&printsec=frontcover&img=1&zoom=5&edge=curl&imgtk=AFLRE71e5b-TeAKTiPSvXNUPeUi8rItzur2xSzwH8QU3qjKH0A2opmoq1o5I9RqJFt1BtcCCqILhnYRcB2aFLJmEvom11gx3Qn3PNN1iBLj2H5y2JHjM8wIwGT7iWFQmEn0Od7s6sOdk&source=gbs_api",
|
||||
"thumbnail": "http://books.google.com/books/content?id=ZxwpakTv_MIC&printsec=frontcover&img=1&zoom=1&edge=curl&imgtk=AFLRE70QORt9J_DmKJgfyf9UEjQkdDMZ0qAu0GP315a1Q4CRS3snEjKnJJO2fYFdxjMwsSpmHoXDFPZbsy4gw-kMvF7lL8LtwxGbJGlfETHw_jbQBKBlKTrneK4XFvvV-EXNrZRgylxj&source=gbs_api",
|
||||
"small": "http://books.google.com/books/content?id=ZxwpakTv_MIC&printsec=frontcover&img=1&zoom=2&edge=curl&imgtk=AFLRE70r1pAUt6VhuEEW8vXFhu8LvKln3yj0mdlaWPO4ZQuODLFQnH0fTebKMMX4ANR5i4PtC0oaI48XkwF-EdzlEM1WmUcR5383N4kRMXcta_i9nmb2y38dnh3hObwQW5VoAxbc9psn&source=gbs_api",
|
||||
"medium": "http://books.google.com/books/content?id=ZxwpakTv_MIC&printsec=frontcover&img=1&zoom=3&edge=curl&imgtk=AFLRE7019EVuXvhzbhmtbz1QFh-ajB6kTKRHGhqijFf8big_GPRMMdpCdKlklFbkCfXvy8F64t5NKlThUHb3tFP-51bbDXkrVErFbCqKGzGnDSSm8cewqT8HiYDNHqn0hXYnuYvN4vYf&source=gbs_api",
|
||||
"large": "http://books.google.com/books/content?id=ZxwpakTv_MIC&printsec=frontcover&img=1&zoom=4&edge=curl&imgtk=AFLRE72I15XZqp_8c8BAj4EskxkdC6nQz8F0Fs6VJhkykwIqfjzwuM34tUSQa3UnMGbx-UYjZjSLmCNFlePS8aR7yy-0UP9BRnYD-h5Qbesnnt_xdOb3u7Wdiobi6VbciNCBwUwbCyeH&source=gbs_api",
|
||||
"extraLarge": "http://books.google.com/books/content?id=ZxwpakTv_MIC&printsec=frontcover&img=1&zoom=6&edge=curl&imgtk=AFLRE70rC6ktY6U0K_hqG1HxPl_9hMjpKb10p9DryVIwQgUjoJfWQOjpNA3EQ-5yk167yYDlO27gylqNAdJBYWu7ZHr3GuqkjTDpXjDvzBBppVyWaVNxKwhOz3gfJ-gzM6cC4kLHP26R&source=gbs_api"
|
||||
},
|
||||
"language": "en",
|
||||
"previewLink": "http://books.google.com/books?id=ZxwpakTv_MIC&hl=&source=gbs_api",
|
||||
"infoLink": "https://play.google.com/store/books/details?id=ZxwpakTv_MIC&source=gbs_api",
|
||||
"canonicalVolumeLink": "https://play.google.com/store/books/details?id=ZxwpakTv_MIC"
|
||||
},
|
||||
"layerInfo": {
|
||||
"layers": [
|
||||
{
|
||||
"layerId": "geo",
|
||||
"volumeAnnotationsVersion": "2"
|
||||
}
|
||||
]
|
||||
},
|
||||
"saleInfo": {
|
||||
"country": "US",
|
||||
"saleability": "FOR_SALE",
|
||||
"isEbook": true,
|
||||
"listPrice": {
|
||||
"amount": 3.99,
|
||||
"currencyCode": "USD"
|
||||
},
|
||||
"retailPrice": {
|
||||
"amount": 3.99,
|
||||
"currencyCode": "USD"
|
||||
},
|
||||
"buyLink": "https://play.google.com/store/books/details?id=ZxwpakTv_MIC&rdid=book-ZxwpakTv_MIC&rdot=1&source=gbs_api",
|
||||
"offers": [
|
||||
{
|
||||
"finskyOfferType": 1,
|
||||
"listPrice": {
|
||||
"amountInMicros": 3990000,
|
||||
"currencyCode": "USD"
|
||||
},
|
||||
"retailPrice": {
|
||||
"amountInMicros": 3990000,
|
||||
"currencyCode": "USD"
|
||||
},
|
||||
"giftable": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"accessInfo": {
|
||||
"country": "US",
|
||||
"viewability": "PARTIAL",
|
||||
"embeddable": true,
|
||||
"publicDomain": false,
|
||||
"textToSpeechPermission": "ALLOWED",
|
||||
"epub": {
|
||||
"isAvailable": true,
|
||||
"acsTokenLink": "http://books.google.com/books/download/Alice_in_Wonderland-sample-epub.acsm?id=ZxwpakTv_MIC&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
|
||||
},
|
||||
"pdf": {
|
||||
"isAvailable": false
|
||||
},
|
||||
"webReaderLink": "http://play.google.com/books/reader?id=ZxwpakTv_MIC&hl=&source=gbs_api",
|
||||
"accessViewStatus": "SAMPLE",
|
||||
"quoteSharingAllowed": false
|
||||
}
|
||||
}
|
||||
105
metadata/_test_files/gbooks_query_response.json
Normal file
105
metadata/_test_files/gbooks_query_response.json
Normal file
@@ -0,0 +1,105 @@
|
||||
{
|
||||
"kind": "books#volumes",
|
||||
"totalItems": 1,
|
||||
"items": [
|
||||
{
|
||||
"kind": "books#volume",
|
||||
"id": "ZxwpakTv_MIC",
|
||||
"etag": "F2eR9VV6VwQ",
|
||||
"selfLink": "https://www.googleapis.com/books/v1/volumes/ZxwpakTv_MIC",
|
||||
"volumeInfo": {
|
||||
"title": "Alice in Wonderland",
|
||||
"authors": [
|
||||
"Lewis Carroll"
|
||||
],
|
||||
"publisher": "The Floating Press",
|
||||
"publishedDate": "2009-01-01",
|
||||
"description": "Alice in Wonderland (also known as Alice's Adventures in Wonderland), from 1865, is the peculiar and imaginative tale of a girl who falls down a rabbit-hole into a bizarre world of eccentric and unusual creatures. Lewis Carroll's prominent example of the genre of \"literary nonsense\" has endured in popularity with its clever way of playing with logic and a narrative structure that has influence generations of fiction writing.",
|
||||
"industryIdentifiers": [
|
||||
{
|
||||
"type": "ISBN_13",
|
||||
"identifier": "9781877527814"
|
||||
},
|
||||
{
|
||||
"type": "ISBN_10",
|
||||
"identifier": "1877527815"
|
||||
}
|
||||
],
|
||||
"readingModes": {
|
||||
"text": true,
|
||||
"image": false
|
||||
},
|
||||
"pageCount": 104,
|
||||
"printType": "BOOK",
|
||||
"categories": [
|
||||
"Fiction"
|
||||
],
|
||||
"averageRating": 5,
|
||||
"ratingsCount": 1,
|
||||
"maturityRating": "NOT_MATURE",
|
||||
"allowAnonLogging": true,
|
||||
"contentVersion": "0.2.3.0.preview.2",
|
||||
"panelizationSummary": {
|
||||
"containsEpubBubbles": false,
|
||||
"containsImageBubbles": false
|
||||
},
|
||||
"imageLinks": {
|
||||
"smallThumbnail": "http://books.google.com/books/content?id=ZxwpakTv_MIC&printsec=frontcover&img=1&zoom=5&edge=curl&source=gbs_api",
|
||||
"thumbnail": "http://books.google.com/books/content?id=ZxwpakTv_MIC&printsec=frontcover&img=1&zoom=1&edge=curl&source=gbs_api"
|
||||
},
|
||||
"language": "en",
|
||||
"previewLink": "http://books.google.com/books?id=ZxwpakTv_MIC&printsec=frontcover&dq=isbn:1877527815&hl=&cd=1&source=gbs_api",
|
||||
"infoLink": "https://play.google.com/store/books/details?id=ZxwpakTv_MIC&source=gbs_api",
|
||||
"canonicalVolumeLink": "https://play.google.com/store/books/details?id=ZxwpakTv_MIC"
|
||||
},
|
||||
"saleInfo": {
|
||||
"country": "US",
|
||||
"saleability": "FOR_SALE",
|
||||
"isEbook": true,
|
||||
"listPrice": {
|
||||
"amount": 3.99,
|
||||
"currencyCode": "USD"
|
||||
},
|
||||
"retailPrice": {
|
||||
"amount": 3.99,
|
||||
"currencyCode": "USD"
|
||||
},
|
||||
"buyLink": "https://play.google.com/store/books/details?id=ZxwpakTv_MIC&rdid=book-ZxwpakTv_MIC&rdot=1&source=gbs_api",
|
||||
"offers": [
|
||||
{
|
||||
"finskyOfferType": 1,
|
||||
"listPrice": {
|
||||
"amountInMicros": 3990000,
|
||||
"currencyCode": "USD"
|
||||
},
|
||||
"retailPrice": {
|
||||
"amountInMicros": 3990000,
|
||||
"currencyCode": "USD"
|
||||
},
|
||||
"giftable": true
|
||||
}
|
||||
]
|
||||
},
|
||||
"accessInfo": {
|
||||
"country": "US",
|
||||
"viewability": "PARTIAL",
|
||||
"embeddable": true,
|
||||
"publicDomain": false,
|
||||
"textToSpeechPermission": "ALLOWED",
|
||||
"epub": {
|
||||
"isAvailable": true,
|
||||
"acsTokenLink": "http://books.google.com/books/download/Alice_in_Wonderland-sample-epub.acsm?id=ZxwpakTv_MIC&format=epub&output=acs4_fulfillment_token&dl_type=sample&source=gbs_api"
|
||||
},
|
||||
"pdf": {
|
||||
"isAvailable": false
|
||||
},
|
||||
"webReaderLink": "http://play.google.com/books/reader?id=ZxwpakTv_MIC&hl=&source=gbs_api",
|
||||
"accessViewStatus": "SAMPLE",
|
||||
"quoteSharingAllowed": false
|
||||
},
|
||||
"searchInfo": {
|
||||
"textSnippet": "Alice in Wonderland (also known as Alice's Adventures in Wonderland), from 1865, is the peculiar and imaginative tale of a girl who falls down a rabbit-hole into a bizarre world of eccentric and unusual creatures."
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -16,6 +16,7 @@ func getEPUBMetadata(filepath string) (*MetadataInfo, error) {
|
||||
rf := rc.Rootfiles[0]
|
||||
|
||||
parsedMetadata := &MetadataInfo{
|
||||
Type: TYPE_EPUB,
|
||||
Title: &rf.Title,
|
||||
Author: &rf.Creator,
|
||||
Description: &rf.Description,
|
||||
|
||||
130
metadata/gbooks_test.go
Normal file
130
metadata/gbooks_test.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/jarcoal/httpmock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
// const GBOOKS_QUERY_URL string = "https://www.googleapis.com/books/v1/volumes?q=%s"
|
||||
// const GBOOKS_GBID_INFO_URL string = "https://www.googleapis.com/books/v1/volumes/%s"
|
||||
// const GBOOKS_GBID_COVER_URL string = "https://books.google.com/books/content/images/frontcover/%s?fife=w480-h690"
|
||||
|
||||
//go:embed _test_files/gbooks_id_response.json
|
||||
var idResp string
|
||||
|
||||
//go:embed _test_files/gbooks_query_response.json
|
||||
var queryResp string
|
||||
|
||||
type details struct {
|
||||
URLs []string
|
||||
}
|
||||
|
||||
// Hook API Helper
|
||||
func hookAPI() *details {
|
||||
// Start HTTPMock
|
||||
httpmock.Activate()
|
||||
|
||||
// Create details struct
|
||||
d := &details{
|
||||
URLs: []string{},
|
||||
}
|
||||
|
||||
// Create Hook
|
||||
matchRE := regexp.MustCompile(`^https://www\.googleapis\.com/books/v1/volumes.*`)
|
||||
httpmock.RegisterRegexpResponder("GET", matchRE, func(req *http.Request) (*http.Response, error) {
|
||||
// Append URL
|
||||
d.URLs = append(d.URLs, req.URL.String())
|
||||
|
||||
// Get Raw Response
|
||||
var rawResp string
|
||||
if req.URL.Query().Get("q") != "" {
|
||||
rawResp = queryResp
|
||||
} else {
|
||||
rawResp = idResp
|
||||
}
|
||||
|
||||
// Convert to JSON Response
|
||||
var responseData map[string]interface{}
|
||||
json.Unmarshal([]byte(rawResp), &responseData)
|
||||
|
||||
// Return Response
|
||||
return httpmock.NewJsonResponse(200, responseData)
|
||||
})
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func TestGBooksGBIDMetadata(t *testing.T) {
|
||||
hookDetails := hookAPI()
|
||||
defer httpmock.DeactivateAndReset()
|
||||
|
||||
GBID := "ZxwpakTv_MIC"
|
||||
expectedURL := fmt.Sprintf(GBOOKS_GBID_INFO_URL, GBID)
|
||||
metadataResp, err := getGBooksMetadata(MetadataInfo{ID: &GBID})
|
||||
|
||||
assert.Nil(t, err, "should not have error")
|
||||
assert.Contains(t, hookDetails.URLs, expectedURL, "should have intercepted URL")
|
||||
assert.Equal(t, 1, len(metadataResp), "should have single result")
|
||||
|
||||
mResult := metadataResp[0]
|
||||
validateResult(t, &mResult)
|
||||
}
|
||||
|
||||
func TestGBooksISBNQuery(t *testing.T) {
|
||||
hookDetails := hookAPI()
|
||||
defer httpmock.DeactivateAndReset()
|
||||
|
||||
ISBN10 := "1877527815"
|
||||
expectedURL := fmt.Sprintf(GBOOKS_QUERY_URL, "isbn:"+ISBN10)
|
||||
metadataResp, err := getGBooksMetadata(MetadataInfo{
|
||||
ISBN10: &ISBN10,
|
||||
})
|
||||
|
||||
assert.Nil(t, err, "should not have error")
|
||||
assert.Contains(t, hookDetails.URLs, expectedURL, "should have intercepted URL")
|
||||
assert.Equal(t, 1, len(metadataResp), "should have single result")
|
||||
|
||||
mResult := metadataResp[0]
|
||||
validateResult(t, &mResult)
|
||||
}
|
||||
|
||||
func TestGBooksTitleQuery(t *testing.T) {
|
||||
hookDetails := hookAPI()
|
||||
defer httpmock.DeactivateAndReset()
|
||||
|
||||
title := "Alice in Wonderland 1877527815"
|
||||
expectedURL := fmt.Sprintf(GBOOKS_QUERY_URL, url.QueryEscape(strings.TrimSpace(title)))
|
||||
metadataResp, err := getGBooksMetadata(MetadataInfo{
|
||||
Title: &title,
|
||||
})
|
||||
|
||||
assert.Nil(t, err, "should not have error")
|
||||
assert.Contains(t, hookDetails.URLs, expectedURL, "should have intercepted URL")
|
||||
assert.NotEqual(t, 0, len(metadataResp), "should not have no results")
|
||||
|
||||
mResult := metadataResp[0]
|
||||
validateResult(t, &mResult)
|
||||
}
|
||||
|
||||
func validateResult(t *testing.T, m *MetadataInfo) {
|
||||
expectedTitle := "Alice in Wonderland"
|
||||
expectedAuthor := "Lewis Carroll"
|
||||
expectedDesc := "Alice in Wonderland (also known as Alice's Adventures in Wonderland), from 1865, is the peculiar and imaginative tale of a girl who falls down a rabbit-hole into a bizarre world of eccentric and unusual creatures. Lewis Carroll's prominent example of the genre of \"literary nonsense\" has endured in popularity with its clever way of playing with logic and a narrative structure that has influence generations of fiction writing."
|
||||
expectedISBN10 := "1877527815"
|
||||
expectedISBN13 := "9781877527814"
|
||||
|
||||
assert.Equal(t, expectedTitle, *m.Title, "should have title")
|
||||
assert.Equal(t, expectedAuthor, *m.Author, "should have author")
|
||||
assert.Equal(t, expectedDesc, *m.Description, "should have description")
|
||||
assert.Equal(t, expectedISBN10, *m.ISBN10, "should have ISBN10")
|
||||
assert.Equal(t, expectedISBN13, *m.ISBN13, "should have ISBN10")
|
||||
}
|
||||
@@ -1,76 +0,0 @@
|
||||
//go:build integration
|
||||
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGBooksGBIDMetadata(t *testing.T) {
|
||||
GBID := "ZxwpakTv_MIC"
|
||||
metadataResp, err := getGBooksMetadata(MetadataInfo{
|
||||
ID: &GBID,
|
||||
})
|
||||
|
||||
if len(metadataResp) != 1 {
|
||||
t.Fatalf(`Expected: %v, Got: %v, Error: %v`, 1, len(metadataResp), err)
|
||||
}
|
||||
|
||||
mResult := metadataResp[0]
|
||||
validateResult(&mResult, t)
|
||||
}
|
||||
|
||||
func TestGBooksISBNQuery(t *testing.T) {
|
||||
ISBN10 := "1877527815"
|
||||
metadataResp, err := getGBooksMetadata(MetadataInfo{
|
||||
ISBN10: &ISBN10,
|
||||
})
|
||||
|
||||
if len(metadataResp) != 1 {
|
||||
t.Fatalf(`Expected: %v, Got: %v, Error: %v`, 1, len(metadataResp), err)
|
||||
}
|
||||
|
||||
mResult := metadataResp[0]
|
||||
validateResult(&mResult, t)
|
||||
}
|
||||
|
||||
func TestGBooksTitleQuery(t *testing.T) {
|
||||
title := "Alice in Wonderland 1877527815"
|
||||
metadataResp, err := getGBooksMetadata(MetadataInfo{
|
||||
Title: &title,
|
||||
})
|
||||
|
||||
if len(metadataResp) == 0 {
|
||||
t.Fatalf(`Expected: %v, Got: %v, Error: %v`, "> 0", len(metadataResp), err)
|
||||
}
|
||||
|
||||
mResult := metadataResp[0]
|
||||
validateResult(&mResult, t)
|
||||
}
|
||||
|
||||
func validateResult(m *MetadataInfo, t *testing.T) {
|
||||
expect := "Lewis Carroll"
|
||||
if *m.Author != expect {
|
||||
t.Fatalf(`Expected: %v, Got: %v`, expect, *m.Author)
|
||||
}
|
||||
|
||||
expect = "Alice in Wonderland"
|
||||
if *m.Title != expect {
|
||||
t.Fatalf(`Expected: %v, Got: %v`, expect, *m.Title)
|
||||
}
|
||||
|
||||
expect = "Alice in Wonderland (also known as Alice's Adventures in Wonderland), from 1865, is the peculiar and imaginative tale of a girl who falls down a rabbit-hole into a bizarre world of eccentric and unusual creatures. Lewis Carroll's prominent example of the genre of \"literary nonsense\" has endured in popularity with its clever way of playing with logic and a narrative structure that has influence generations of fiction writing."
|
||||
if *m.Description != expect {
|
||||
t.Fatalf(`Expected: %v, Got: %v`, expect, *m.Description)
|
||||
}
|
||||
|
||||
expect = "1877527815"
|
||||
if *m.ISBN10 != expect {
|
||||
t.Fatalf(`Expected: %v, Got: %v`, expect, *m.ISBN10)
|
||||
}
|
||||
|
||||
expect = "9781877527814"
|
||||
if *m.ISBN13 != expect {
|
||||
t.Fatalf(`Expected: %v, Got: %v`, expect, *m.ISBN13)
|
||||
}
|
||||
}
|
||||
@@ -3,27 +3,47 @@ package metadata
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/gabriel-vasile/mimetype"
|
||||
"reichard.io/antholume/utils"
|
||||
)
|
||||
|
||||
type MetadataHandler func(string) (*MetadataInfo, error)
|
||||
|
||||
type DocumentType string
|
||||
|
||||
const (
|
||||
TYPE_EPUB DocumentType = ".epub"
|
||||
)
|
||||
|
||||
var extensionHandlerMap = map[DocumentType]MetadataHandler{
|
||||
TYPE_EPUB: getEPUBMetadata,
|
||||
}
|
||||
|
||||
type Source int
|
||||
|
||||
const (
|
||||
GBOOK Source = iota
|
||||
OLIB
|
||||
SOURCE_GBOOK Source = iota
|
||||
SOURCE_OLIB
|
||||
)
|
||||
|
||||
type MetadataInfo struct {
|
||||
ID *string
|
||||
ID *string
|
||||
MD5 *string
|
||||
PartialMD5 *string
|
||||
WordCount *int64
|
||||
|
||||
Title *string
|
||||
Author *string
|
||||
Description *string
|
||||
ISBN10 *string
|
||||
ISBN13 *string
|
||||
Type DocumentType
|
||||
}
|
||||
|
||||
// Downloads the Google Books cover file and saves it to the provided directory.
|
||||
func CacheCover(gbid string, coverDir string, documentID string, overwrite bool) (*string, error) {
|
||||
// Get Filepath
|
||||
coverFile := "." + filepath.Clean(fmt.Sprintf("/%s.jpg", documentID))
|
||||
@@ -39,11 +59,12 @@ func CacheCover(gbid string, coverDir string, documentID string, overwrite bool)
|
||||
return &coverFile, nil
|
||||
}
|
||||
|
||||
// Searches source for metadata based on the provided information.
|
||||
func SearchMetadata(s Source, metadataSearch MetadataInfo) ([]MetadataInfo, error) {
|
||||
switch s {
|
||||
case GBOOK:
|
||||
case SOURCE_GBOOK:
|
||||
return getGBooksMetadata(metadataSearch)
|
||||
case OLIB:
|
||||
case SOURCE_OLIB:
|
||||
return nil, errors.New("Not implemented")
|
||||
default:
|
||||
return nil, errors.New("Not implemented")
|
||||
@@ -51,32 +72,112 @@ func SearchMetadata(s Source, metadataSearch MetadataInfo) ([]MetadataInfo, erro
|
||||
}
|
||||
}
|
||||
|
||||
func GetWordCount(filepath string) (int64, error) {
|
||||
fileMime, err := mimetype.DetectFile(filepath)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
if fileExtension := fileMime.Extension(); fileExtension == ".epub" {
|
||||
totalWords, err := countEPUBWords(filepath)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return totalWords, nil
|
||||
} else {
|
||||
return 0, errors.New("Invalid Extension")
|
||||
}
|
||||
}
|
||||
|
||||
func GetMetadata(filepath string) (*MetadataInfo, error) {
|
||||
// Returns the word count of the provided filepath. An error will be returned
|
||||
// if the file is not supported.
|
||||
func GetWordCount(filepath string) (*int64, error) {
|
||||
fileMime, err := mimetype.DetectFile(filepath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if fileExtension := fileMime.Extension(); fileExtension == ".epub" {
|
||||
return getEPUBMetadata(filepath)
|
||||
totalWords, err := countEPUBWords(filepath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &totalWords, nil
|
||||
} else {
|
||||
return nil, errors.New("Invalid Extension")
|
||||
return nil, fmt.Errorf("Invalid extension")
|
||||
}
|
||||
}
|
||||
|
||||
// Returns embedded metadata of the provided file. An error will be returned if
|
||||
// the file is not supported.
|
||||
func GetMetadata(filepath string) (*MetadataInfo, error) {
|
||||
// Detect Extension Type
|
||||
fileMime, err := mimetype.DetectFile(filepath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Get Extension Type Metadata Handler
|
||||
fileExtension := fileMime.Extension()
|
||||
handler, ok := extensionHandlerMap[DocumentType(fileExtension)]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("invalid extension %s", fileExtension)
|
||||
}
|
||||
|
||||
// Acquire Metadata
|
||||
metadataInfo, err := handler(filepath)
|
||||
|
||||
// Calculate MD5 & Partial MD5
|
||||
partialMD5, err := utils.CalculatePartialMD5(filepath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to calculate partial MD5")
|
||||
}
|
||||
|
||||
// Calculate Actual MD5
|
||||
MD5, err := utils.CalculateMD5(filepath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to calculate MD5")
|
||||
}
|
||||
|
||||
// Calculate Word Count
|
||||
wordCount, err := GetWordCount(filepath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("unable to calculate word count")
|
||||
}
|
||||
|
||||
metadataInfo.WordCount = wordCount
|
||||
metadataInfo.PartialMD5 = partialMD5
|
||||
metadataInfo.MD5 = MD5
|
||||
|
||||
return metadataInfo, nil
|
||||
}
|
||||
|
||||
// Returns the extension of the provided filepath (e.g. ".epub"). An error
|
||||
// will be returned if the file is not supported.
|
||||
func GetDocumentType(filepath string) (*DocumentType, error) {
|
||||
// Detect Extension Type
|
||||
fileMime, err := mimetype.DetectFile(filepath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Detect
|
||||
fileExtension := fileMime.Extension()
|
||||
docType, ok := ParseDocumentType(fileExtension)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("filetype not supported")
|
||||
}
|
||||
|
||||
return &docType, nil
|
||||
}
|
||||
|
||||
// Returns the extension of the provided file reader (e.g. ".epub"). An error
|
||||
// will be returned if the file is not supported.
|
||||
func GetDocumentTypeReader(r io.Reader) (*DocumentType, error) {
|
||||
// Detect Extension Type
|
||||
fileMime, err := mimetype.DetectReader(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Detect
|
||||
fileExtension := fileMime.Extension()
|
||||
docType, ok := ParseDocumentType(fileExtension)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("filetype not supported")
|
||||
}
|
||||
|
||||
return &docType, nil
|
||||
}
|
||||
|
||||
// Given a filetype string, attempt to resolve a DocumentType
|
||||
func ParseDocumentType(input string) (DocumentType, bool) {
|
||||
validTypes := map[string]DocumentType{
|
||||
string(TYPE_EPUB): TYPE_EPUB,
|
||||
}
|
||||
found, ok := validTypes[input]
|
||||
return found, ok
|
||||
}
|
||||
|
||||
@@ -1,36 +1,46 @@
|
||||
package metadata
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGetWordCount(t *testing.T) {
|
||||
var want int64 = 30080
|
||||
wordCount, err := countEPUBWords("../_test_files/alice.epub")
|
||||
var desiredCount int64 = 30080
|
||||
actualCount, err := countEPUBWords("../_test_files/alice.epub")
|
||||
|
||||
assert.Nil(t, err, "should have no error")
|
||||
assert.Equal(t, desiredCount, actualCount, "should be correct word count")
|
||||
|
||||
if wordCount != want {
|
||||
t.Fatalf(`Expected: %v, Got: %v, Error: %v`, want, wordCount, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetMetadata(t *testing.T) {
|
||||
metadataInfo, err := getEPUBMetadata("../_test_files/alice.epub")
|
||||
if err != nil {
|
||||
t.Fatalf(`Expected: *MetadataInfo, Got: nil, Error: %v`, err)
|
||||
}
|
||||
desiredTitle := "Alice's Adventures in Wonderland / Illustrated by Arthur Rackham. With a Proem by Austin Dobson"
|
||||
desiredAuthor := "Lewis Carroll"
|
||||
desiredDescription := ""
|
||||
|
||||
want := "Alice's Adventures in Wonderland / Illustrated by Arthur Rackham. With a Proem by Austin Dobson"
|
||||
if *metadataInfo.Title != want {
|
||||
t.Fatalf(`Expected: %v, Got: %v, Error: %v`, want, *metadataInfo.Title, err)
|
||||
}
|
||||
metadataInfo, err := GetMetadata("../_test_files/alice.epub")
|
||||
|
||||
want = "Lewis Carroll"
|
||||
if *metadataInfo.Author != want {
|
||||
t.Fatalf(`Expected: %v, Got: %v, Error: %v`, want, *metadataInfo.Author, err)
|
||||
}
|
||||
|
||||
want = ""
|
||||
if *metadataInfo.Description != want {
|
||||
t.Fatalf(`Expected: %v, Got: %v, Error: %v`, want, *metadataInfo.Description, err)
|
||||
}
|
||||
assert.Nil(t, err, "should have no error")
|
||||
assert.Equal(t, desiredTitle, *metadataInfo.Title, "should be correct title")
|
||||
assert.Equal(t, desiredAuthor, *metadataInfo.Author, "should be correct author")
|
||||
assert.Equal(t, desiredDescription, *metadataInfo.Description, "should be correct author")
|
||||
assert.Equal(t, TYPE_EPUB, metadataInfo.Type, "should be correct type")
|
||||
}
|
||||
|
||||
func TestGetExtension(t *testing.T) {
|
||||
docType, err := GetDocumentType("../_test_files/alice.epub")
|
||||
|
||||
assert.Nil(t, err, "should have no error")
|
||||
assert.Equal(t, TYPE_EPUB, *docType)
|
||||
}
|
||||
|
||||
func TestGetExtensionReader(t *testing.T) {
|
||||
file, _ := os.Open("../_test_files/alice.epub")
|
||||
docType, err := GetDocumentTypeReader(file)
|
||||
|
||||
assert.Nil(t, err, "should have no error")
|
||||
assert.Equal(t, TYPE_EPUB, *docType)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user