fix: bookmark content download (#413)

Fixed a bug where the content of the article would archive but the
reader version would not be saved due to variable passing. Also made the
code easier to follow by following return principles.

Fixes #406
This commit is contained in:
Felipe Martin Garcia 2022-03-27 21:01:49 +02:00 committed by GitHub
parent 0fe24d2598
commit ce8a172682
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,25 +22,27 @@ import (
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
func downloadBookmarkContent(book *model.Bookmark, dataDir string, request *http.Request) { func downloadBookmarkContent(book *model.Bookmark, dataDir string, request *http.Request) (*model.Bookmark, error) {
content, contentType, err := core.DownloadBookmark(book.URL) content, contentType, err := core.DownloadBookmark(book.URL)
if err == nil && content != nil { if err != nil {
request := core.ProcessRequest{ return nil, fmt.Errorf("error downloading bookmark: %s", err)
}
processRequest := core.ProcessRequest{
DataDir: dataDir, DataDir: dataDir,
Bookmark: *book, Bookmark: *book,
Content: content, Content: content,
ContentType: contentType, ContentType: contentType,
} }
result, isFatalErr, err := core.ProcessBookmark(request) result, isFatalErr, err := core.ProcessBookmark(processRequest)
content.Close() content.Close()
if err != nil && isFatalErr { if err != nil && isFatalErr {
panic(fmt.Errorf("failed to process bookmark: %v", err)) panic(fmt.Errorf("failed to process bookmark: %v", err))
} }
book = &result return &result, err
}
} }
// apiLogin is handler for POST /api/login // apiLogin is handler for POST /api/login
@ -279,7 +281,7 @@ func (h *handler) apiInsertBookmark(w http.ResponseWriter, r *http.Request, ps h
err = json.NewDecoder(r.Body).Decode(&payload) err = json.NewDecoder(r.Body).Decode(&payload)
checkError(err) checkError(err)
book := model.Bookmark{ book := &model.Bookmark{
URL: payload.URL, URL: payload.URL,
Title: payload.Title, Title: payload.Title,
Excerpt: payload.Excerpt, Excerpt: payload.Excerpt,
@ -306,20 +308,25 @@ func (h *handler) apiInsertBookmark(w http.ResponseWriter, r *http.Request, ps h
} }
if !payload.Async { if !payload.Async {
downloadBookmarkContent(&book, h.DataDir, r) book, err = downloadBookmarkContent(book, h.DataDir, r)
if err != nil {
log.Printf("error downloading boorkmark: %s", err)
}
} }
// Save bookmark to database // Save bookmark to database
results, err := h.DB.SaveBookmarks(book) results, err := h.DB.SaveBookmarks(*book)
if err != nil || len(results) == 0 { if err != nil || len(results) == 0 {
panic(fmt.Errorf("failed to save bookmark: %v", err)) panic(fmt.Errorf("failed to save bookmark: %v", err))
} }
book = results[0]
if payload.Async { if payload.Async {
go func() { go func() {
downloadBookmarkContent(&book, h.DataDir, r) bookmark, err := downloadBookmarkContent(book, h.DataDir, r)
if _, err := h.DB.SaveBookmarks(book); err != nil { if err != nil {
log.Printf("error downloading boorkmark: %s", err)
}
if _, err := h.DB.SaveBookmarks(*bookmark); err != nil {
log.Printf("failed to save bookmark: %s", err) log.Printf("failed to save bookmark: %s", err)
} }
}() }()
@ -327,7 +334,7 @@ func (h *handler) apiInsertBookmark(w http.ResponseWriter, r *http.Request, ps h
// Return the new bookmark // Return the new bookmark
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&book) err = json.NewEncoder(w).Encode(results[0])
checkError(err) checkError(err)
} }