feat(api): add API documentation and ShareX integration
- Add an API documentation page with curl and ShareX examples. - Implement a dynamic ShareX configuration endpoint (`/api/v1/sharex/warpbox-anonymous.sxcu`) that generates a `.sxcu` file pre-configured with the instance's base URL. - Update anonymous uploads to return a private management link (`manageUrl`) and a deletion link (`deleteUrl`) in JSON responses. - Update README with details on Stage 3 Anonymous Integrations. - Add styling for the new API documentation view and management details.
This commit is contained in:
87
backend/libs/handlers/manage.go
Normal file
87
backend/libs/handlers/manage.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"warpbox.dev/backend/libs/helpers"
|
||||
"warpbox.dev/backend/libs/services"
|
||||
"warpbox.dev/backend/libs/web"
|
||||
)
|
||||
|
||||
type managePageData struct {
|
||||
Box boxView
|
||||
Token string
|
||||
FileCount int
|
||||
TotalSize string
|
||||
ExpiresLabel string
|
||||
DownloadCount int
|
||||
MaxDownloads int
|
||||
Protected bool
|
||||
DeleteActionURL string
|
||||
}
|
||||
|
||||
func (a *App) ManageBox(w http.ResponseWriter, r *http.Request) {
|
||||
box, ok := a.loadManagedBox(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
a.renderer.Render(w, http.StatusOK, "manage.html", web.PageData{
|
||||
Title: "Manage upload",
|
||||
Description: "Delete this anonymous Warpbox upload.",
|
||||
Data: a.managePageData(box, r.PathValue("token")),
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) ManageDeleteBox(w http.ResponseWriter, r *http.Request) {
|
||||
box, ok := a.loadManagedBox(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if err := a.uploadService.DeleteBoxWithToken(box.ID, r.PathValue("token")); err != nil {
|
||||
a.logger.Warn("anonymous delete failed", "source", "anonymous-delete", "severity", "warn", "code", 4102, "box_id", box.ID, "error", err.Error())
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/d/"+box.ID+"/deleted", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (a *App) ManageDeleted(w http.ResponseWriter, r *http.Request) {
|
||||
a.renderer.Render(w, http.StatusOK, "manage_deleted.html", web.PageData{
|
||||
Title: "Upload deleted",
|
||||
Description: "This Warpbox upload has been deleted.",
|
||||
Data: boxView{ID: r.PathValue("boxID")},
|
||||
})
|
||||
}
|
||||
|
||||
func (a *App) loadManagedBox(w http.ResponseWriter, r *http.Request) (services.Box, bool) {
|
||||
box, err := a.uploadService.GetBox(r.PathValue("boxID"))
|
||||
if err != nil {
|
||||
http.NotFound(w, r)
|
||||
return services.Box{}, false
|
||||
}
|
||||
if !a.uploadService.VerifyDeleteToken(box, r.PathValue("token")) {
|
||||
http.NotFound(w, r)
|
||||
return services.Box{}, false
|
||||
}
|
||||
return box, true
|
||||
}
|
||||
|
||||
func (a *App) managePageData(box services.Box, token string) managePageData {
|
||||
var totalSize int64
|
||||
for _, file := range box.Files {
|
||||
totalSize += file.Size
|
||||
}
|
||||
return managePageData{
|
||||
Box: boxView{ID: box.ID},
|
||||
Token: token,
|
||||
FileCount: len(box.Files),
|
||||
TotalSize: helpers.FormatBytes(totalSize),
|
||||
ExpiresLabel: box.ExpiresAt.Format("Jan 2, 2006 15:04 MST"),
|
||||
DownloadCount: box.DownloadCount,
|
||||
MaxDownloads: box.MaxDownloads,
|
||||
Protected: a.uploadService.IsProtected(box),
|
||||
DeleteActionURL: "/d/" + box.ID + "/manage/" + token + "/delete",
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user