skipping file download if already downloaded
This commit is contained in:
parent
fbbd78ce72
commit
60f5495f79
@ -3,6 +3,7 @@ package rendering
|
|||||||
import (
|
import (
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
@ -50,6 +51,9 @@ func (w *Website) Cache() error {
|
|||||||
|
|
||||||
filename := hashUrl(w.ImageUrl) + filepath.Ext(w.ImageUrl)
|
filename := hashUrl(w.ImageUrl) + filepath.Ext(w.ImageUrl)
|
||||||
targetPath := filepath.Join(cacheDir, filename)
|
targetPath := filepath.Join(cacheDir, filename)
|
||||||
|
|
||||||
|
// if the file was already downloaded it doesn't need to be downloaded again
|
||||||
|
if _, err := os.Stat(targetPath); errors.Is(err, os.ErrNotExist) {
|
||||||
resp, err := http.Get(w.ImageUrl)
|
resp, err := http.Get(w.ImageUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -67,7 +71,9 @@ func (w *Website) Cache() error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the value in the struct to the current file
|
||||||
w.ImageUrl, _ = url.JoinPath(CacheUrl, filename)
|
w.ImageUrl, _ = url.JoinPath(CacheUrl, filename)
|
||||||
w.IsFetched = true
|
w.IsFetched = true
|
||||||
return nil
|
return nil
|
||||||
|
Loading…
x
Reference in New Issue
Block a user