mirror of
https://github.com/wgh136/nysoure.git
synced 2025-09-27 12:17:24 +00:00
File download
This commit is contained in:
@@ -6,6 +6,7 @@ import (
|
||||
"nysoure/server/model"
|
||||
"nysoure/server/service"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func AddFileRoutes(router fiber.Router) {
|
||||
@@ -19,6 +20,7 @@ func AddFileRoutes(router fiber.Router) {
|
||||
fileGroup.Get("/:id", getFile)
|
||||
fileGroup.Put("/:id", updateFile)
|
||||
fileGroup.Delete("/:id", deleteFile)
|
||||
fileGroup.Get("/download/:id", downloadFile)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -200,3 +202,20 @@ func deleteFile(c fiber.Ctx) error {
|
||||
Message: "File deleted successfully",
|
||||
})
|
||||
}
|
||||
|
||||
func downloadFile(c fiber.Ctx) error {
|
||||
idStr, err := strconv.ParseUint(c.Params("id"), 10, 32)
|
||||
if err != nil {
|
||||
return model.NewRequestError("Invalid file ID")
|
||||
}
|
||||
id := uint(idStr)
|
||||
s, filename, err := service.DownloadFile(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.HasPrefix(s, "http") {
|
||||
return c.Redirect().Status(fiber.StatusFound).To(s)
|
||||
}
|
||||
c.Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
|
||||
return c.SendFile(s)
|
||||
}
|
||||
|
Reference in New Issue
Block a user