Skip to content

Commit

Permalink
fix: bookmark content download (#413)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fmartingr authored Mar 27, 2022
1 parent 0fe24d2 commit ce8a172
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions internal/webserver/handler-api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,27 @@ import (
"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)
if err == nil && content != nil {
request := core.ProcessRequest{
DataDir: dataDir,
Bookmark: *book,
Content: content,
ContentType: contentType,
}
if err != nil {
return nil, fmt.Errorf("error downloading bookmark: %s", err)
}

result, isFatalErr, err := core.ProcessBookmark(request)
content.Close()
processRequest := core.ProcessRequest{
DataDir: dataDir,
Bookmark: *book,
Content: content,
ContentType: contentType,
}

if err != nil && isFatalErr {
panic(fmt.Errorf("failed to process bookmark: %v", err))
}
result, isFatalErr, err := core.ProcessBookmark(processRequest)
content.Close()

book = &result
if err != nil && isFatalErr {
panic(fmt.Errorf("failed to process bookmark: %v", err))
}

return &result, err
}

// apiLogin is handler for POST /api/login
Expand Down Expand Up @@ -279,7 +281,7 @@ func (h *handler) apiInsertBookmark(w http.ResponseWriter, r *http.Request, ps h
err = json.NewDecoder(r.Body).Decode(&payload)
checkError(err)

book := model.Bookmark{
book := &model.Bookmark{
URL: payload.URL,
Title: payload.Title,
Excerpt: payload.Excerpt,
Expand All @@ -306,28 +308,33 @@ func (h *handler) apiInsertBookmark(w http.ResponseWriter, r *http.Request, ps h
}

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
results, err := h.DB.SaveBookmarks(book)
results, err := h.DB.SaveBookmarks(*book)
if err != nil || len(results) == 0 {
panic(fmt.Errorf("failed to save bookmark: %v", err))
}
book = results[0]

if payload.Async {
go func() {
downloadBookmarkContent(&book, h.DataDir, r)
if _, err := h.DB.SaveBookmarks(book); err != nil {
bookmark, err := downloadBookmarkContent(book, h.DataDir, r)
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)
}
}()
}

// Return the new bookmark
w.Header().Set("Content-Type", "application/json")
err = json.NewEncoder(w).Encode(&book)
err = json.NewEncoder(w).Encode(results[0])
checkError(err)
}

Expand Down

0 comments on commit ce8a172

Please sign in to comment.