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,24 +51,29 @@ 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)
|
||||||
resp, err := http.Get(w.ImageUrl)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
file, err := os.Create(targetPath)
|
// if the file was already downloaded it doesn't need to be downloaded again
|
||||||
if err != nil {
|
if _, err := os.Stat(targetPath); errors.Is(err, os.ErrNotExist) {
|
||||||
return err
|
resp, err := http.Get(w.ImageUrl)
|
||||||
}
|
if err != nil {
|
||||||
defer file.Close()
|
return err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
_, err = io.Copy(file, resp.Body)
|
file, err := os.Create(targetPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
if err != nil {
|
_, err = io.Copy(file, resp.Body)
|
||||||
return err
|
|
||||||
|
if err != nil {
|
||||||
|
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