Add UI to export the list of bookmarks

This commit is contained in:
Justin Hawkins 2022-05-25 17:18:32 +09:30
parent 78488d2f41
commit be10f5238e
4 changed files with 26 additions and 1 deletions

View File

@ -2,6 +2,7 @@ package db
import (
"fmt"
"io"
"log"
"sync"
"time"
@ -48,6 +49,19 @@ func (m *BookmarkManager) ListBookmarks() ([]entity.Bookmark, error) {
return bookmarks, nil
}
// ExportBookmarks exports all bookmarks to an io.Writer
func (m *BookmarkManager) ExportBookmarks(w io.Writer) error {
bms := []entity.Bookmark{}
err := m.db.store.Find(&bms, &badgerhold.Query{})
if err != nil {
return fmt.Errorf("could not export bookmarks: %w", err)
}
for _, bm := range bms {
w.Write([]byte(bm.URL + "\n"))
}
return nil
}
func (m *BookmarkManager) SaveBookmark(bm *entity.Bookmark) error {
err := m.db.store.Update(bm.ID, &bm)
if err != nil {

View File

@ -8,7 +8,7 @@ import (
"golang.org/x/mod/semver"
)
const Tag = "v0.0.6"
const Tag = "v0.0.7"
var versionInfo struct {
Local struct {

View File

@ -22,6 +22,7 @@
<a href="#">Admin</a>
<ul class="menu vertical">
<li><a href="/manage">Manage links</a></li>
<li><a href="/export">Export all URLs</a></li>
</ul>
</li>
</ul>

View File

@ -189,6 +189,16 @@ func Create(bmm *db.BookmarkManager) *Server {
c.String(http.StatusOK, "queued")
})
r.GET("/export", func(c *gin.Context) {
c.Writer.Header().Set("Content-Type", "text/plain")
c.Writer.Header().Set("Content-Disposition", "attachment; filename=\"bookmarks.txt\"")
err := bmm.ExportBookmarks(c.Writer)
// this is a bit late, but we already added headers, so at least log it.
if err != nil {
log.Printf("got error when exporting: %s", err)
}
})
return server
}