Skip to content

Commit

Permalink
fixed not filtering by name for saved files
Browse files Browse the repository at this point in the history
  • Loading branch information
CommanderStorm committed Oct 15, 2023
1 parent 685596a commit 1b7a087
Showing 1 changed file with 38 additions and 37 deletions.
75 changes: 38 additions & 37 deletions server/backend/cron/news.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,43 +87,45 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error {
}
}

if !skipNews(existingNewsLinksForSource, item.Link) {
// pick the first enclosure that is an image (if any)
var pickedEnclosure *gofeed.Enclosure
for _, enclosure := range item.Enclosures {
if strings.HasSuffix(enclosure.URL, "jpg") ||
strings.HasSuffix(enclosure.URL, "jpeg") ||
strings.HasSuffix(enclosure.URL, "png") ||
ImageContentTypeRegex.MatchString(enclosure.Type) {
pickedEnclosure = enclosure
break
}
if skipNews(existingNewsLinksForSource, item.Link) {
continue
}

// pick the first enclosure that is an image (if any)
var pickedEnclosure *gofeed.Enclosure
for _, enclosure := range item.Enclosures {
if strings.HasSuffix(enclosure.URL, "jpg") ||
strings.HasSuffix(enclosure.URL, "jpeg") ||
strings.HasSuffix(enclosure.URL, "png") ||
ImageContentTypeRegex.MatchString(enclosure.Type) {
pickedEnclosure = enclosure
break
}
var enclosureUrl = null.StringFrom("")
var file *model.File
if pickedEnclosure != nil {
file, err = c.saveImage(pickedEnclosure.URL)
if err != nil {
log.WithError(err).Error("can't save news image")
}
enclosureUrl = null.StringFrom(pickedEnclosure.URL)
}
var enclosureUrl = null.StringFrom("")
var file *model.File
if pickedEnclosure != nil {
file, err = c.saveImage(pickedEnclosure.URL)
if err != nil {
log.WithError(err).Error("can't save news image")
}
bm := bluemonday.StrictPolicy()
sanitizedDesc := bm.Sanitize(item.Description)
enclosureUrl = null.StringFrom(pickedEnclosure.URL)
}
bm := bluemonday.StrictPolicy()
sanitizedDesc := bm.Sanitize(item.Description)

newsItem := model.News{
Date: *item.PublishedParsed,
Created: time.Now(),
Title: item.Title,
Description: sanitizedDesc,
Src: source.Source,
Link: item.Link,
Image: enclosureUrl,
FileID: null.IntFrom(file.File),
File: file,
}
newNews = append(newNews, newsItem)
newsItem := model.News{
Date: *item.PublishedParsed,
Created: time.Now(),
Title: item.Title,
Description: sanitizedDesc,
Src: source.Source,
Link: item.Link,
Image: enclosureUrl,
FileID: null.IntFrom(file.File),
File: file,
}
newNews = append(newNews, newsItem)
}
if ammountOfNewNews := len(newNews); ammountOfNewNews != 0 {
err = c.db.Save(&newNews).Error
Expand All @@ -140,10 +142,9 @@ func (c *CronService) parseNewsFeed(source model.NewsSource) error {
// saveImage saves an image to the database, so it can be downloaded by another cronjob and returns its id
func (c *CronService) saveImage(url string) (*model.File, error) {
targetFileName := fmt.Sprintf("%x.jpg", md5.Sum([]byte(url)))
file := model.File{
Name: targetFileName, // path intentionally omitted
}
if err := c.db.First(&file).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
var file model.File
// path intentionally omitted in query to allow for deduplication
if err := c.db.First(&file, "name = ?", targetFileName).Error; err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
log.WithError(err).WithField("targetFileName", targetFileName).Error("Couldn't query database for file")
return nil, err
} else if err == nil {
Expand Down

0 comments on commit 1b7a087

Please sign in to comment.