Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

Commit

Permalink
Test source files immediately to make it clearer where errors are
Browse files Browse the repository at this point in the history
  • Loading branch information
bmaupin committed Jun 16, 2016
1 parent 0c01751 commit 45f9b97
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 12 deletions.
77 changes: 70 additions & 7 deletions epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ package epub
import (
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"

Expand All @@ -38,6 +42,10 @@ import (
// filename is used more than once
var ErrFilenameAlreadyUsed = errors.New("Filename already used")

// ErrRetrievingFile is thrown by AddCSS, AddFont, AddImage, or SetCover if
// there was a problem retrieving the source file that was provided
var ErrRetrievingFile = errors.New("Error retrieving file from source")

const (
cssFileFormat = "css%04d%s"
defaultCoverBody = `<img src="%s" alt="Cover Image" />`
Expand Down Expand Up @@ -218,9 +226,16 @@ func (e *Epub) SetAuthor(author string) {
// The CSS content is the exact content that will be stored in the CSS file. It
// will not be validated. If the CSS content isn't provided, default content
// will be used.
func (e *Epub) SetCover(imageSource string, cssSource string) {
func (e *Epub) SetCover(imageSource string, cssSource string) error {
var err error

// Make sure the source files are valid before proceeding
if isFileSourceValid(imageSource) == false {
return ErrRetrievingFile
} else if isFileSourceValid(cssSource) == false {
return ErrRetrievingFile
}

// If a cover already exists
if e.cover.xhtmlFilename != "" {
// Remove the xhtml file
Expand All @@ -244,13 +259,16 @@ func (e *Epub) SetCover(imageSource string, cssSource string) {
)
imagePath, err := e.AddImage(imageSource, defaultImageFilename)
// If that doesn't work, generate a filename
if err != nil {
if err == ErrFilenameAlreadyUsed {
imagePath, err = e.AddImage(imageSource, "")
if err != nil {
if err == ErrFilenameAlreadyUsed {
// This shouldn't cause an error since we're not specifying a filename
panic(fmt.Sprintf("Error adding default cover image file: %s", err))
}
}
if err != nil && err != ErrFilenameAlreadyUsed {
return err
}
e.cover.imageFilename = filepath.Base(imagePath)

// Use default cover stylesheet if one isn't provided
Expand All @@ -259,28 +277,36 @@ func (e *Epub) SetCover(imageSource string, cssSource string) {
}
cssPath, err := e.AddCSS(cssSource, defaultCoverCSSFilename)
// If that doesn't work, generate a filename
if err != nil {
if err == ErrFilenameAlreadyUsed {
cssPath, err = e.AddCSS(cssSource, "")
if err != nil {
if err == ErrFilenameAlreadyUsed {
// This shouldn't cause an error since we're not specifying a filename
panic(fmt.Sprintf("Error adding default cover CSS file: %s", err))
}
}
if err != nil && err != ErrFilenameAlreadyUsed {
return err
}
e.cover.cssFilename = filepath.Base(cssPath)

coverBody := fmt.Sprintf(defaultCoverBody, imagePath)
// Title won't be used since the cover won't be added to the TOC
// First try to use the default cover filename
coverPath, err := e.AddSection(coverBody, "", defaultCoverXhtmlFilename, cssPath)
// If that doesn't work, generate a filename
if err != nil {
if err == ErrFilenameAlreadyUsed {
coverPath, err = e.AddSection(coverBody, "", "", cssPath)
if err != nil {
if err == ErrFilenameAlreadyUsed {
// This shouldn't cause an error since we're not specifying a filename
panic(fmt.Sprintf("Error adding default cover XHTML file: %s", err))
}
}
if err != nil && err != ErrFilenameAlreadyUsed {
return err
}
e.cover.xhtmlFilename = filepath.Base(coverPath)

return nil
}

// SetLang sets the language of the EPUB.
Expand Down Expand Up @@ -317,6 +343,11 @@ func (e *Epub) UUID() string {
// Add a media file to the EPUB and return the path relative to the EPUB section
// files
func addMedia(mediaSource string, mediaFilename string, mediaFileFormat string, mediaFolderName string, mediaMap map[string]string) (string, error) {
// Make sure the source file is valid before proceeding
if isFileSourceValid(mediaSource) == false {
return "", ErrRetrievingFile
}

if mediaFilename == "" {
// If a filename isn't provided, use the filename from the source
mediaFilename = filepath.Base(mediaSource)
Expand All @@ -342,3 +373,35 @@ func addMedia(mediaSource string, mediaFilename string, mediaFileFormat string,
mediaFilename,
), nil
}

func isFileSourceValid(source string) bool {
u, err := url.Parse(source)
if err != nil {
return false
}

var r io.ReadCloser
var resp *http.Response
// If it's a URL
if u.Scheme == "http" || u.Scheme == "https" {
resp, err = http.Get(source)
if err != nil {
return false
}
r = resp.Body

// Otherwise, assume it's a local file
} else {
r, err = os.Open(source)
}
if err != nil {
return false
}
defer func() {
if err := r.Close(); err != nil {
panic(err)
}
}()

return true
}
5 changes: 4 additions & 1 deletion epub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,10 @@ func TestEpubUUID(t *testing.T) {

func TestSetCover(t *testing.T) {
e := NewEpub(testEpubTitle)
e.SetCover(testImageFromFileSource, testCoverCSSSource)
err := e.SetCover(testImageFromFileSource, testCoverCSSSource)
if err != nil {
t.Errorf("Error setting cover: %s", err)
}

tempDir := writeAndExtractEpub(t, e, testEpubFilename)

Expand Down
4 changes: 0 additions & 4 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ import (
// EPUB file
var ErrUnableToCreateEpub = errors.New("Unable to create EPUB file")

// ErrRetrievingFile is thrown by Write if it cannot get the source file that
// was added using AddCSS, AddFont, or AddImage
var ErrRetrievingFile = errors.New("Error retrieving file from source")

var extensionMediaTypes = map[string]string{
".css": mediaTypeCSS,
".gif": "image/gif",
Expand Down

0 comments on commit 45f9b97

Please sign in to comment.