mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-05-01 17:49:45 -05:00
33 lines
842 B
Go
33 lines
842 B
Go
package publink
|
|
|
|
import (
|
|
"errors"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/shroff/phylum/server/internal/api/serve"
|
|
"github.com/shroff/phylum/server/internal/core/fs"
|
|
)
|
|
|
|
func Setup(r *gin.RouterGroup) {
|
|
r.Handle("GET", "/:id/*path", func(c *gin.Context) {
|
|
id := c.Param("id")
|
|
path := c.Param("path")
|
|
|
|
_, password, _ := c.Request.BasicAuth()
|
|
r, err := fs.OpenFromPublink(c.Request.Context(), id, password, path)
|
|
if err != nil {
|
|
if errors.Is(err, fs.ErrInsufficientPermissions) {
|
|
c.Header("WWW-Authenticate", "Basic realm=\""+id+"\"")
|
|
c.AbortWithStatus(http.StatusUnauthorized)
|
|
} else if errors.Is(err, fs.ErrResourceNotFound) {
|
|
c.AbortWithStatus(http.StatusNotFound)
|
|
} else {
|
|
c.AbortWithStatus(http.StatusInternalServerError)
|
|
}
|
|
} else {
|
|
serve.Serve(c.Writer, c.Request, r)
|
|
}
|
|
})
|
|
}
|