Skip to content

Commit

Permalink
feat: download blog images locally and replace the path
Browse files Browse the repository at this point in the history
  • Loading branch information
LinceMathew committed Apr 2, 2024
1 parent 68d6953 commit 20db622
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
4 changes: 3 additions & 1 deletion glee.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ import (

var config *toml.Tree
var log = logrus.New()
var filePath string

var opts struct {
ShowConfig bool `short:"c" long:"config" description:"Show glee configuration global file"`
Debug bool `short:"d" long:"debug" description:"debug mode"`
File string `description:"Markdown file to process"`
Help bool `short:"h" long:"help" description:"Show this help message"`
Version bool `short:"v" long:"version" description:"Show version"`
DownloadImage bool `short:"i" long:"download-image" description:"Download images from markdown file"`
}

var flagParser = flags.NewParser(&opts, flags.Default)
Expand Down Expand Up @@ -73,7 +75,7 @@ func main() {
}
checkConfigurationsExist(log)
if len(args) == 1 {
filePath := args[0]
filePath = args[0]
content, err := os.ReadFile(filePath)

if err != nil {
Expand Down
54 changes: 54 additions & 0 deletions imageProcessing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
Expand Down Expand Up @@ -83,6 +84,26 @@ func imageToHash(image string) (string, string, error) {
if err != nil {
return "", "", err
}
// Create the "images" directory if it doesn't exist
if opts.DownloadImage {
if !strings.Contains(image, "https://karma-src-x02msdf8-23.s3.ap-south-1.amazonaws.com/product-menu-logo") {

err = os.MkdirAll("images", os.ModePerm)
if err != nil {
return "", "", err
}

// Copy the temporary file into the "images" directory
fileName := filepath.Base(image)
destination := filepath.Join("images", fileName)
if err := copyFile(image,tp, destination); err != nil {
return "", "", err
}
}


}

fileExtension = iext
hashValue, err = sha256Sum(tp)
} else {
Expand All @@ -105,3 +126,36 @@ func getTempDir() string {
}
return "/tmp/"
}




func copyFile(image,src, dst string) error {
sourceFile, err := os.Open(src)
if err != nil {
return err
}
defer sourceFile.Close()

destFile, err := os.Create(dst)
if err != nil {
return err
}
defer destFile.Close()

_, err = io.Copy(destFile, sourceFile)


content, err := ioutil.ReadFile(filePath)
if err != nil {
return err
}
newContent := strings.Replace(string(content),image, dst, -1)
err = ioutil.WriteFile(filePath, []byte(newContent), 0644)
if err != nil {
return err
}
return err


}
3 changes: 3 additions & 0 deletions install_local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
go build .
sudo rm -f /usr/local/bin/glee /usr/bin/glee
sudo mv glee /usr/local/bin/glee

0 comments on commit 20db622

Please sign in to comment.