Add UI to export the list of bookmarks
This commit is contained in:
parent
78488d2f41
commit
be10f5238e
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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>
|
||||
|
10
web/web.go
10
web/web.go
@ -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
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user