Skip to content

Commit

Permalink
Add new API endpoint /icons/{iconID}
Browse files Browse the repository at this point in the history
  • Loading branch information
fguillot committed Oct 6, 2023
1 parent 5774323 commit 2002d60
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 deletions.
18 changes: 17 additions & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ func (c *Client) SaveEntry(entryID int64) error {
return err
}

// FetchCounters
// FetchCounters fetches feed counters.
func (c *Client) FetchCounters() (*FeedCounters, error) {
body, err := c.request.Get("/v1/feeds/counters")
if err != nil {
Expand All @@ -518,6 +518,22 @@ func (c *Client) FlushHistory() error {
return err
}

// Icon fetches a feed icon.
func (c *Client) Icon(iconID int64) (*FeedIcon, error) {
body, err := c.request.Get(fmt.Sprintf("/v1/icons/%d", iconID))
if err != nil {
return nil, err
}
defer body.Close()

var feedIcon *FeedIcon
if err := json.NewDecoder(body).Decode(&feedIcon); err != nil {
return nil, fmt.Errorf("miniflux: response error (%v)", err)
}

return feedIcon, nil
}

func buildFilterQueryString(path string, filter *Filter) string {
if filter != nil {
values := url.Values{}
Expand Down
3 changes: 2 additions & 1 deletion internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) {
sr.HandleFunc("/feeds/{feedID}", handler.getFeed).Methods(http.MethodGet)
sr.HandleFunc("/feeds/{feedID}", handler.updateFeed).Methods(http.MethodPut)
sr.HandleFunc("/feeds/{feedID}", handler.removeFeed).Methods(http.MethodDelete)
sr.HandleFunc("/feeds/{feedID}/icon", handler.feedIcon).Methods(http.MethodGet)
sr.HandleFunc("/feeds/{feedID}/icon", handler.getIconByFeedID).Methods(http.MethodGet)
sr.HandleFunc("/feeds/{feedID}/mark-all-as-read", handler.markFeedAsRead).Methods(http.MethodPut)
sr.HandleFunc("/export", handler.exportFeeds).Methods(http.MethodGet)
sr.HandleFunc("/import", handler.importFeeds).Methods(http.MethodPost)
Expand All @@ -67,4 +67,5 @@ func Serve(router *mux.Router, store *storage.Storage, pool *worker.Pool) {
sr.HandleFunc("/entries/{entryID}/save", handler.saveEntry).Methods(http.MethodPost)
sr.HandleFunc("/entries/{entryID}/fetch-content", handler.fetchContent).Methods(http.MethodGet)
sr.HandleFunc("/flush-history", handler.flushHistory).Methods(http.MethodPut, http.MethodDelete)
sr.HandleFunc("/icons/{iconID}", handler.getIconByIconID).Methods(http.MethodGet)
}
23 changes: 22 additions & 1 deletion internal/api/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"miniflux.app/v2/internal/http/response/json"
)

func (h *handler) feedIcon(w http.ResponseWriter, r *http.Request) {
func (h *handler) getIconByFeedID(w http.ResponseWriter, r *http.Request) {
feedID := request.RouteInt64Param(r, "feedID")

if !h.store.HasIcon(feedID) {
Expand All @@ -35,3 +35,24 @@ func (h *handler) feedIcon(w http.ResponseWriter, r *http.Request) {
Data: icon.DataURL(),
})
}

func (h *handler) getIconByIconID(w http.ResponseWriter, r *http.Request) {
iconID := request.RouteInt64Param(r, "iconID")

icon, err := h.store.IconByID(iconID)
if err != nil {
json.ServerError(w, r, err)
return
}

if icon == nil {
json.NotFound(w, r)
return
}

json.OK(w, r, &feedIconResponse{
ID: icon.ID,
MimeType: icon.MimeType,
Data: icon.DataURL(),
})
}
2 changes: 1 addition & 1 deletion internal/model/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type Icon struct {
ID int64 `json:"id"`
Hash string `json:"hash"`
MimeType string `json:"mime_type"`
Content []byte `json:"content"`
Content []byte `json:"-"`
}

// DataURL returns the data URL of the icon.
Expand Down
4 changes: 2 additions & 2 deletions internal/storage/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (s *Storage) IconByID(iconID int64) (*model.Icon, error) {
if err == sql.ErrNoRows {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("store: unable to fetch icon by hash: %v", err)
return nil, fmt.Errorf("store: unable to fetch icon #%d: %w", iconID, err)
}

return &icon, nil
Expand Down Expand Up @@ -63,7 +63,7 @@ func (s *Storage) IconByHash(icon *model.Icon) error {
if err == sql.ErrNoRows {
return nil
} else if err != nil {
return fmt.Errorf(`store: unable to fetch icon by hash: %v`, err)
return fmt.Errorf(`store: unable to fetch icon by hash %q: %v`, icon.Hash, err)
}

return nil
Expand Down
22 changes: 18 additions & 4 deletions internal/tests/feed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -762,14 +762,28 @@ func TestGetFeedIcon(t *testing.T) {
}

if feedIcon.ID == 0 {
t.Fatalf(`Invalid feed icon ID, got "%v"`, feedIcon.ID)
t.Fatalf(`Invalid feed icon ID, got "%d"`, feedIcon.ID)
}

if feedIcon.MimeType != "image/x-icon" {
t.Fatalf(`Invalid feed icon mime type, got "%v" instead of "%v"`, feedIcon.MimeType, "image/x-icon")
expectedMimeType := "image/x-icon"
if feedIcon.MimeType != expectedMimeType {
t.Fatalf(`Invalid feed icon mime type, got %q instead of %q`, feedIcon.MimeType, expectedMimeType)
}

if !strings.Contains(feedIcon.Data, "image/x-icon") {
if !strings.HasPrefix(feedIcon.Data, expectedMimeType) {
t.Fatalf(`Invalid feed icon data, got "%v"`, feedIcon.Data)
}

feedIcon, err = client.Icon(feedIcon.ID)
if err != nil {
t.Fatal(err)
}

if feedIcon.MimeType != expectedMimeType {
t.Fatalf(`Invalid feed icon mime type, got %q instead of %q`, feedIcon.MimeType, expectedMimeType)
}

if !strings.HasPrefix(feedIcon.Data, expectedMimeType) {
t.Fatalf(`Invalid feed icon data, got "%v"`, feedIcon.Data)
}
}
Expand Down

0 comments on commit 2002d60

Please sign in to comment.