Bläddra i källkod

cache icon file on local

Help-14 3 år sedan
förälder
incheckning
1b57a5b2fe
2 ändrade filer med 39 tillägg och 0 borttagningar
  1. 16 0
      src/main.go
  2. 23 0
      src/modules/file.go

+ 16 - 0
src/main.go

@@ -6,6 +6,7 @@ import (
 	"log"
 	"net/http"
 	"os"
+	"path"
 	"path/filepath"
 	"time"
 
@@ -51,6 +52,7 @@ func prepare() {
 
 	dataPath := filepath.Join(pwd, "data")
 	os.MkdirAll(dataPath, os.ModePerm)
+	os.MkdirAll(filepath.Join(dataPath, "icon"), os.ModePerm)
 	modules.CopyDir(filepath.Join(pwd, "common"), dataPath, false)
 }
 
@@ -60,6 +62,20 @@ func loadData() {
 	websiteData.Language = modules.LoadLanguage(appConfig.Website.Language)
 	websiteData.Contents = modules.LoadContent().Data
 
+	// Download icon to local
+	for group := 0; group < len(websiteData.Contents); group++ {
+		for col := 0; col < len(websiteData.Contents[group].Columns); col++ {
+			for bookmark := 0; bookmark < len(websiteData.Contents[group].Columns[col].Bookmarks); bookmark++ {
+				iconPath := websiteData.Contents[group].Columns[col].Bookmarks[bookmark].Icon
+				fileName := path.Base(iconPath)
+				if modules.DownloadFile(iconPath, filepath.Join(pwd, "data", "icon", fileName)) {
+					websiteData.Contents[group].Columns[col].Bookmarks[bookmark].Icon = "/common/icon/" + fileName
+				}
+			}
+		}
+	}
+
+	// Load template engine
 	themefs = http.FileServer(http.Dir(filepath.Join(pwd, "themes", appConfig.Website.Theme)))
 	tmpl, _ := template.ParseFiles(filepath.Join(pwd, "themes", appConfig.Website.Theme, "index.html"))
 	webTemplate = tmpl

+ 23 - 0
src/modules/file.go

@@ -3,6 +3,7 @@ package modules
 import (
 	"fmt"
 	"io"
+	"net/http"
 	"os"
 )
 
@@ -75,3 +76,25 @@ func CopyDir(source string, dest string, override bool) (err error) {
 	}
 	return
 }
+
+func DownloadFile(url string, filePath string) bool {
+	out, err := os.Create(filePath)
+	if err != nil {
+		return false
+	}
+	defer out.Close()
+
+	resp, err := http.Get(url)
+	if err != nil {
+		return false
+	}
+	defer resp.Body.Close()
+
+	w, err := io.Copy(out, resp.Body)
+	if err != nil {
+		return false
+	}
+
+	fmt.Printf("Downloaded %d bytes from "+url+"\n", w)
+	return true
+}