cache icon file on local

This commit is contained in:
Help-14 2022-04-25 21:32:17 +07:00
parent 00edf04de4
commit 1b57a5b2fe
2 changed files with 39 additions and 0 deletions

View file

@ -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

View file

@ -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
}