Serve public MinIO files at /file/:file_name and add a native Ubuntu MinIO install.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-18 13:47:57 +05:00
parent 71479f4355
commit 6191b7536c
10 changed files with 149 additions and 23 deletions

View File

@@ -13,7 +13,6 @@ import (
"online-back/internal/appresult"
"online-back/internal/config"
"online-back/internal/handlers"
"online-back/pkg/logging"
minioUpload "online-back/pkg/minio"
@@ -23,9 +22,10 @@ import (
)
const (
baseURL = "/users"
fileUploadURL = "/file"
fileURL = "/file/:id"
baseURL = "/users"
fileUploadURL = "/file"
fileURL = "/file/:id"
publicFileURL = "/file/:file_name"
)
type handler struct {
@@ -33,7 +33,7 @@ type handler struct {
repository Repository
}
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
func NewHandler(logger *logging.Logger, repository Repository) *handler {
return &handler{logger: logger, repository: repository}
}
@@ -47,15 +47,17 @@ func (h *handler) Register(router *gin.RouterGroup) {
group.PUT("/:id", h.update)
group.DELETE("/:id", h.delete)
// Avatar/profile file storage — kept at the top-level /file path (not
// nested under /users) to preserve the existing API contract; this used
// to live in the auth handler purely incidentally.
// Upload/delete stay on the admin API (JWT). GET is public at /file/:file_name
// so nginx can serve avatars as https://host/file/<file_name>.
files := router.Group("", appresult.JwtTokenCheck())
files.POST(fileUploadURL, h.fileUpload)
files.GET(fileURL, h.getFile)
files.DELETE(fileURL, h.deleteFile)
}
func (h *handler) RegisterPublic(engine *gin.Engine) {
engine.GET(publicFileURL, h.getFile)
}
// list godoc
// @Summary List users
// @Description Returns paginated users with role user, with optional filters
@@ -375,16 +377,21 @@ func (h *handler) fileUpload(c *gin.Context) {
}
// getFile godoc
// @Description download file
// @Description download file by object name (public; not under /api/admin)
// @Tags file-upload
// @Produce octet-stream
// @Param id path string true "object name"
// @Security BearerAuth
// @Param file_name path string true "object name"
// @Success 200 {file} binary
// @Failure 400 {object} appresult.AppError
// @Failure 404 {object} appresult.AppError
// @Failure 500 {object} appresult.AppError
// @Router /file/{id} [get]
// @Router /file/{file_name} [get]
func (h *handler) getFile(c *gin.Context) {
id := c.Param("id")
fileName := c.Param("file_name")
if fileName == "" || strings.Contains(fileName, "..") || strings.ContainsAny(fileName, "/\\") {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
minioCfg := config.GetConfig().Minio
minioClient, ok := h.connectMinio(c, minioCfg)
@@ -392,7 +399,7 @@ func (h *handler) getFile(c *gin.Context) {
return
}
object, err := minioClient.GetObject(c.Request.Context(), minioCfg.Bucket, id, minio.GetObjectOptions{})
object, err := minioClient.GetObject(c.Request.Context(), minioCfg.Bucket, fileName, minio.GetObjectOptions{})
if err != nil {
h.logger.Errorf("get object from minio failed: %v", err)
c.JSON(http.StatusInternalServerError, appresult.ErrNotAcceptable)