Move downloadFeed out to a seperate function
This commit is contained in:
parent
f34f94320a
commit
56b4f23ae2
@ -165,7 +165,18 @@ func getDataFeeds(dataFeedHashes map[string]string, localPath string) (map[strin
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
downloadAndSave := func() error {
|
err := downloadFeed(dataFeedName, fileName)
|
||||||
|
if err != nil {
|
||||||
|
return dataFeedReaders, dataFeedHashes, err
|
||||||
|
}
|
||||||
|
dataFeedReaders[dataFeedName] = fileName
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return dataFeedReaders, dataFeedHashes, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func downloadFeed(dataFeedName, fileName string) error {
|
||||||
|
|
||||||
// Download data feed.
|
// Download data feed.
|
||||||
r, err := httputil.GetWithUserAgent(fmt.Sprintf(dataFeedURL, dataFeedName))
|
r, err := httputil.GetWithUserAgent(fmt.Sprintf(dataFeedURL, dataFeedName))
|
||||||
@ -176,40 +187,33 @@ func getDataFeeds(dataFeedHashes map[string]string, localPath string) (map[strin
|
|||||||
defer r.Body.Close()
|
defer r.Body.Close()
|
||||||
|
|
||||||
if !httputil.Status2xx(r) {
|
if !httputil.Status2xx(r) {
|
||||||
log.WithField("StatusCode", r.StatusCode).Error("Failed to download NVD data feed")
|
log.WithFields(log.Fields{"StatusCode": r.StatusCode, "DataFeedName": dataFeedName}).Error("Failed to download NVD data feed")
|
||||||
return commonerr.ErrCouldNotDownload
|
return commonerr.ErrCouldNotDownload
|
||||||
}
|
}
|
||||||
|
|
||||||
// Un-gzip it.
|
// Un-gzip it.
|
||||||
gr, err := gzip.NewReader(r.Body)
|
gr, err := gzip.NewReader(r.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).WithField(logDataFeedName, dataFeedName).Error("could not read NVD data feed")
|
log.WithError(err).WithFields(log.Fields{"StatusCode": r.StatusCode, "DataFeedName": dataFeedName}).Error("could not read NVD data feed")
|
||||||
return commonerr.ErrCouldNotDownload
|
return commonerr.ErrCouldNotDownload
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store it to a file at the same time if possible.
|
// Store it to a file at the same time if possible.
|
||||||
if f, err := os.Create(fileName); err == nil {
|
f, err := os.Create(fileName)
|
||||||
_, err = io.Copy(f, gr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.WithError(err).Warning("could not stream NVD data feed to filesystem")
|
|
||||||
|
log.WithError(err).WithField("Filename", fileName).Warning("could not store NVD data feed to filesystem")
|
||||||
|
return commonerr.ErrFilesystem
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
dataFeedReaders[dataFeedName] = fileName
|
|
||||||
|
|
||||||
} else {
|
_, err = io.Copy(f, gr)
|
||||||
log.WithError(err).Warning("could not store NVD data feed to filesystem")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err := downloadAndSave()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dataFeedReaders, dataFeedHashes, err
|
log.WithError(err).WithField("Filename", fileName).Warning("could not stream NVD data feed to filesystem")
|
||||||
}
|
return commonerr.ErrFilesystem
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return dataFeedReaders, dataFeedHashes, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getHashFromMetaURL(metaURL string) (string, error) {
|
func getHashFromMetaURL(metaURL string) (string, error) {
|
||||||
|
Loading…
Reference in New Issue
Block a user