Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type config struct {
Password string `json:"password"`
Server string `json:"server"`
Port int `json:"port"`
Color bool
Compress bool
NoImage bool
ImageDir string
}
const DefaultTimeout = 120
const XdgConfigHome = "XDG_CONFIG_HOME"
Expand Down
54 changes: 50 additions & 4 deletions epubgen/epub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package epubgen

import (
"errors"
"github.com/nikhil1raghav/kindle-send/imageutil"
"log"
"os"
"path"
"sync"
Expand All @@ -18,7 +20,18 @@ type epubmaker struct {
Epub *epub.Epub
downloads map[string]string
}

func createTempImageDir() (string, error){
storePath:=config.GetInstance().StorePath
if len(storePath)==0{
curDir , err := os.Getwd()
if err!=nil{
storePath="./"
}else{
storePath=curDir
}
}
return os.MkdirTemp(storePath, "img")
}
func NewEpubmaker(title string) *epubmaker {
downloadMap := make(map[string]string)
return &epubmaker{
Expand All @@ -30,7 +43,10 @@ func NewEpubmaker(title string) *epubmaker {
func fetchReadable(url string) (readability.Article, error) {
return readability.FromURL(url, 30*time.Second)
}

//remove images from the document
func (e *epubmaker) removeImages(i int, img *goquery.Selection){
img.Remove()
}
//Point remote image link to downloaded image
func (e *epubmaker) changeRefs(i int, img *goquery.Selection) {
img.RemoveAttr("loading")
Expand Down Expand Up @@ -58,9 +74,15 @@ 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)
log.Println("Queuing for download ",imgSrc)
imageFileName, err:=imageutil.Process(imgSrc)
log.Println("ImageFIlename", imageFileName)
if err!=nil{
util.Red.Printf("Error processing image %s : %s\n", imgSrc, err)
return
}

imgRef, err := e.Epub.AddImage(imgSrc, imageFileName)
imgRef, err := e.Epub.AddImage(imageFileName,"")
if err != nil {
util.Red.Printf("Couldn't add image %s : %s\n", imgSrc, err)
return
Expand All @@ -77,6 +99,12 @@ func (e *epubmaker) embedImages(wg *sync.WaitGroup, article *readability.Article
defer wg.Done()
//TODO: Compress images before embedding to improve size
doc := goquery.NewDocumentFromNode(article.Node)
cfg:=config.GetInstance()
if cfg.NoImage{
doc.Find("img").Each(e.removeImages)
return
}


//download all images
doc.Find("img").Each(e.downloadImages)
Expand Down Expand Up @@ -120,6 +148,19 @@ func (e *epubmaker) addContent(articles *[]readability.Article) error {
func Make(pageUrls []string, title string) (string, error) {
//TODO: Parallelize fetching pages

cfg:=config.GetInstance()
//create directory to store images
if !cfg.NoImage{

imgdir, err:=createTempImageDir()
if err!=nil{
util.Red.Println("Couldn't create directory to store images, saving document without images")
cfg.NoImage=true
}else{
cfg.ImageDir=imgdir
defer os.RemoveAll(imgdir)
}
}
//Get readable article from urls
readableArticles := make([]readability.Article, 0)
for _, pageUrl := range pageUrls {
Expand Down Expand Up @@ -175,5 +216,10 @@ func Make(pageUrls []string, title string) (string, error) {
if err != nil {
return "", err
}
if !cfg.NoImage{
if len(cfg.ImageDir)!=0{
//os.RemoveAll(cfg.ImageDir)
}
}
return filename, nil
}
46 changes: 46 additions & 0 deletions imageutil/image.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package imageutil

import (
"image"
"image/png"
"log"
"os"
)
func WriteToGray(filePath string){
img, err:= loadImage(filePath)
if err!=nil{
log.Println(err)
return
}

grayImage := rgbaToGray(img)
os.Remove(filePath)
f,_:=os.Create(filePath)
defer f.Close()
png.Encode(f,grayImage)
}
func loadImage(filePath string)(image.Image, error){
infile, err:=os.Open(filePath)
if err!=nil{
return nil, err
}
defer infile.Close()
img, _ , err:=image.Decode(infile)
if err!=nil{
return nil, err
}
return img, nil
}
func rgbaToGray(img image.Image) *image.Gray{
var(
bounds = img.Bounds()
gray = image.NewGray(bounds)
)
for x:=0;x<bounds.Max.X;x++{
for y:=0;y<bounds.Max.Y;y++{
var rgba = img.At(x,y)
gray.Set(x, y, rgba)
}
}
return gray
}
57 changes: 57 additions & 0 deletions imageutil/process.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package imageutil

import (
"errors"
"fmt"
"github.com/nikhil1raghav/kindle-send/config"
"github.com/nikhil1raghav/kindle-send/util"
"io"
"log"
"net/http"
"os"
"path"
)

//Process the image and send the filename to add in epub
func Process(imageUrl string)(string, error){
cfg:=config.GetInstance()
downloaded, err :=download(imageUrl)
if err!=nil{
return "", err
}
if cfg.Color{
fmt.Println("Value of color is true")
}
if !cfg.Color{
WriteToGray(downloaded)
}
return downloaded, nil
}

func download(imageUrl string)(string, error){

response, err:=http.Get(imageUrl)
if err!=nil{
return "",err
}
defer response.Body.Close()
if response.StatusCode!=200{
log.Println("Got non 200 response")
return "", errors.New("Non 200 response")
}

log.Println("Got response for ", imageUrl)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file contains at least one console log. Please remove any present.


file, err:=os.Create(path.Join(config.GetInstance().ImageDir, util.GetHash(imageUrl)))
log.Println("Writing to file ", file.Name())
if err!=nil{
return "", err
}
defer file.Close()
_, err = io.Copy(file, response.Body)
if err!=nil{
return "", err
}
log.Println("Written successfully to ", file.Name())
return file.Name(), nil
}
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func main() {
mailTimeout :=flag.Int("mail-timeout",120, "Timeout for sending mail in Seconds" )
_ =flag.Bool("version", false, "Print version information")
_ =flag.Bool("dry-run", false, "Save epub locally and don't send to device")
_=flag.Bool("no-image", false, "Render epub without images")
_=flag.Bool("color", false, "By default epub is rendered with grayscale image to reduce size,\npass --color to render with colored image")

flag.Parse()
passed := 0
Expand Down Expand Up @@ -83,6 +85,13 @@ func main() {
util.Red.Println(err)
return
}
cfg:=config.GetInstance()
if flagPassed("no-image"){
cfg.NoImage=true
}
if flagPassed("color"){
cfg.Color=true
}
filesToSend := make([]string, 0)
if len(*filePath) != 0 {
filesToSend = append(filesToSend, *filePath)
Expand All @@ -96,6 +105,7 @@ func main() {
}
}


if flagPassed("dry-run"){
util.CyanBold.Println("Dry-run mode : Not sending files to device")
util.Cyan.Println("Following files are saved")
Expand Down