Skip to content

Commit

Permalink
golie clone should skip entry download if the file already exists
Browse files Browse the repository at this point in the history
and is the most up-to-date.

Relates to rolieup#24.
  • Loading branch information
isimluk committed Aug 11, 2020
1 parent 2197207 commit 4fdaa37
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions pkg/models/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ type Property struct {

type TimeStr string

func (ts TimeStr) Time() (time.Time, error) {
return time.Parse(time.RFC3339, string(ts))
}

func Time(t time.Time) TimeStr {
return TimeStr(t.Format(time.RFC3339))
}
38 changes: 37 additions & 1 deletion pkg/rolie/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/rolieup/golie/pkg/models"
"github.com/rolieup/golie/pkg/rolie_source"
"github.com/rolieup/golie/pkg/utils"
log "github.com/sirupsen/logrus"
)

func Clone(URI string, dir string) error {
Expand Down Expand Up @@ -74,7 +75,9 @@ func (f *fetcher) cloneFeed(feed *models.Feed) error {

func (f *fetcher) cloneEntry(entry *models.Entry) error {
if len(entry.Link) > 0 {
return f.storeRemoteResource(entry.Link[0].Href)
if f.needsRefresh(entry) {
return f.storeRemoteResource(entry.Link[0].Href)
}
}
return nil
}
Expand Down Expand Up @@ -107,6 +110,39 @@ func (f *fetcher) storeLocally(URI string, content []byte) error {
return ioutil.WriteFile(path, content, 0644)
}

func (f *fetcher) needsRefresh(entry *models.Entry) bool {
link := entry.Link[0]
if link.Length == 0 {
return true
}
path, err := f.filepath(link.Href)
if err != nil {
log.Debugf("cannot compose local filepath for remote resource: %v", err)
return true
}
stat, err := os.Stat(path)
if err != nil {
return true
}
if stat.IsDir() {
return true
}
if link.Length != uint64(stat.Size()) {
return true
}
entryTime, err := entry.Updated.Time()
if err != nil {
return true
}
if stat.ModTime().Before(entryTime) {
return true
}

log.Debugf("Skipping download of %s", link.Href)

return false
}

func (f *fetcher) filepath(URI string) (string, error) {
path, err := f.filepathRelative(URI)
if err != nil {
Expand Down

0 comments on commit 4fdaa37

Please sign in to comment.