Skip to content

Commit

Permalink
Merge pull request #30 from przemekd/fix/sanitaze-file-names
Browse files Browse the repository at this point in the history
Fix/sanitaze file names
  • Loading branch information
nikhil1raghav authored Aug 19, 2023
2 parents 3e8a628 + a546157 commit db376ac
Show file tree
Hide file tree
Showing 19 changed files with 48,243 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
dist/
.idea/
.vscode/
29 changes: 18 additions & 11 deletions epubgen/epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/PuerkitoBio/goquery"
"github.com/bmaupin/go-epub"
"github.com/go-shiori/go-readability"
"github.com/gosimple/slug"
"github.com/nikhil1raghav/kindle-send/config"
"github.com/nikhil1raghav/kindle-send/util"
)
Expand All @@ -31,7 +32,7 @@ func fetchReadable(url string) (readability.Article, error) {
return readability.FromURL(url, 30*time.Second)
}

//Point remote image link to downloaded image
// Point remote image link to downloaded image
func (e *epubmaker) changeRefs(i int, img *goquery.Selection) {
img.RemoveAttr("loading")
img.RemoveAttr("srcset")
Expand All @@ -44,7 +45,7 @@ func (e *epubmaker) changeRefs(i int, img *goquery.Selection) {
}
}

//Download images and add to epub zip
// Download images and add to epub zip
func (e *epubmaker) downloadImages(i int, img *goquery.Selection) {
util.CyanBold.Println("Downloading Images")
imgSrc, exists := img.Attr("src")
Expand All @@ -58,7 +59,7 @@ func (e *epubmaker) downloadImages(i int, img *goquery.Selection) {

//pass unique and safe image names here, then it will not crash on windows
//use murmur hash to generate file name
imageFileName:=util.GetHash(imgSrc)
imageFileName := util.GetHash(imgSrc)

imgRef, err := e.Epub.AddImage(imgSrc, imageFileName)
if err != nil {
Expand All @@ -71,7 +72,7 @@ func (e *epubmaker) downloadImages(i int, img *goquery.Selection) {
}
}

//Fetches images in article and then embeds them into epub
// Fetches images in article and then embeds them into epub
func (e *epubmaker) embedImages(wg *sync.WaitGroup, article *readability.Article) {
util.Cyan.Println("Embedding images in ", article.Title)
defer wg.Done()
Expand All @@ -93,12 +94,12 @@ func (e *epubmaker) embedImages(wg *sync.WaitGroup, article *readability.Article
}
}

//TODO: Look for better formatting, this is bare bones
// TODO: Look for better formatting, this is bare bones
func prepare(article *readability.Article) string {
return "<h1>" + article.Title + "</h1>" + article.Content
}

//Add articles to epub
// Add articles to epub
func (e *epubmaker) addContent(articles *[]readability.Article) error {
added := 0
for _, article := range *articles {
Expand All @@ -116,7 +117,7 @@ func (e *epubmaker) addContent(articles *[]readability.Article) error {
return nil
}

//Generates a single epub from a slice of urls, returns file path
// Generates a single epub from a slice of urls, returns file path
func Make(pageUrls []string, title string) (string, error) {
//TODO: Parallelize fetching pages

Expand Down Expand Up @@ -169,11 +170,17 @@ func Make(pageUrls []string, title string) (string, error) {
storeDir = config.GetInstance().StorePath
}


filename := path.Join(storeDir, title+".epub")
err = book.Epub.Write(filename)
titleSlug := slug.Make(title)
var filename string
if len(titleSlug) == 0 {
filename = "kindle-send-doc-" + util.GetHash(readableArticles[0].Content) + ".epub"
} else {
filename = titleSlug + ".epub"
}
filepath := path.Join(storeDir, filename)
err = book.Epub.Write(filepath)
if err != nil {
return "", err
}
return filename, nil
return filepath, nil
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/bmaupin/go-epub v1.0.1
github.com/fatih/color v1.13.0
github.com/go-shiori/go-readability v0.0.0-20220215145315-dd6828d2f09b
github.com/gosimple/slug v1.13.1
github.com/lithammer/dedent v1.1.0
github.com/spf13/cobra v1.0.0
gopkg.in/mail.v2 v2.3.1
Expand All @@ -18,6 +19,7 @@ require (
github.com/go-shiori/dom v0.0.0-20210627111528-4e4722cd0d65 // indirect
github.com/gofrs/uuid v3.1.0+incompatible // indirect
github.com/gogs/chardet v0.0.0-20191104214054-4b6791f73a28 // indirect
github.com/gosimple/unidecode v1.0.1 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/mattn/go-colorable v0.1.9 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
Expand Down
3 changes: 3 additions & 0 deletions vendor/github.com/gosimple/slug/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit db376ac

Please sign in to comment.