Complete delete functionality, update feature roadmap
This commit is contained in:
parent
279db23b42
commit
dfc8ff001e
@ -3,9 +3,11 @@ A lightweight {paste,file}bin alternative written in Golang.
|
||||
|
||||
Overall goal is to create a service that works well as a single-user self-hosted instance, which can trivially upload files/text.
|
||||
|
||||
## Feature List (so far...)
|
||||
## Feature Roadmap (so far...; from most important to least)
|
||||
- [x] Post/read functionality
|
||||
- [ ] Delete (and edit?) functionality
|
||||
- [x] Delete functionality
|
||||
- [x] Syntax highlighting
|
||||
- [ ] Server configuration file (via viper)
|
||||
- [ ] API key authentication / loose user management
|
||||
- [ ] CLI upload / management functionality
|
||||
- [ ] Edit/revise/replace content at ID
|
@ -141,58 +141,42 @@ https://sr.ht/~seraphimrp/bingo`)
|
||||
}
|
||||
})
|
||||
|
||||
app.Delete(("/:id"), func(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
idNotFound := false
|
||||
|
||||
var itemValue []byte
|
||||
app.Delete("/:id", func(c *fiber.Ctx) error {
|
||||
potentialId := c.Params("id")
|
||||
keyNotFound := false
|
||||
|
||||
err := db.View(func(txn *badger.Txn) error {
|
||||
item, err := txn.Get([]byte(c.Params("id")))
|
||||
_, err := txn.Get([]byte(potentialId))
|
||||
if err != nil && err != badger.ErrKeyNotFound {
|
||||
return err
|
||||
} else if err == badger.ErrKeyNotFound {
|
||||
idNotFound = true
|
||||
return nil
|
||||
keyNotFound = true
|
||||
}
|
||||
|
||||
itemValue, err = item.ValueCopy(nil)
|
||||
return err
|
||||
return nil
|
||||
})
|
||||
handleErr(err, fmt.Sprintf("unable to fetch %s from db", id))
|
||||
handleErr(err, "encountered an error when searching for existing keys")
|
||||
|
||||
if idNotFound {
|
||||
if keyNotFound {
|
||||
c.SendStatus(404)
|
||||
return c.SendString("did not receive valid parameter")
|
||||
}
|
||||
|
||||
ua := useragent.Parse(string(c.Context().UserAgent()))
|
||||
if ua.IsUnknown() {
|
||||
return c.SendString(string(itemValue))
|
||||
}
|
||||
err = db.Update(func(txn *badger.Txn) error {
|
||||
cfmt.Infof("🗑️ [info] deleting entry at %s\n", potentialId)
|
||||
err := txn.Delete([]byte(potentialId))
|
||||
return err
|
||||
})
|
||||
handleErr(err, "encountered an error when deleting an entry in the db")
|
||||
|
||||
queries := c.Queries()
|
||||
if len(queries) == 1 {
|
||||
lang := slices.Collect(maps.Keys(queries))[0]
|
||||
cfmt.Infof("🗑️ [info] entry deleted at %s\n", potentialId)
|
||||
|
||||
var outputHtml bytes.Buffer
|
||||
err = quick.Highlight(&outputHtml, string(itemValue), lang, "html", "gruvbox")
|
||||
handleErr(err, fmt.Sprintf("unable to syntax highlight %s\n", id))
|
||||
|
||||
cfmt.Infof("👀 [info] reading %s with '%s' syntax\n", id, lang)
|
||||
return c.Render("render_syntax", fiber.Map{
|
||||
"Code": outputHtml.String(),
|
||||
})
|
||||
} else {
|
||||
cfmt.Infof("👀 [info] reading %s\n", id)
|
||||
return c.Render("render", fiber.Map{
|
||||
"Code": string(itemValue),
|
||||
})
|
||||
}
|
||||
return c.SendString("success")
|
||||
})
|
||||
}
|
||||
|
||||
func generateId() string {
|
||||
// TODO: optimize via https://stackoverflow.com/questions/22892120/how-to-generate-a-random-string-of-a-fixed-length-in-go
|
||||
id := ""
|
||||
characters := "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user