File Upload
Since Nika is built on Gin, you can use Gin's built-in file upload capabilities.
Nika also provides common/storage helpers for validating uploaded files
before you save them locally or send them to S3-compatible storage.
Current Approach
Use Gin to receive files from HTTP requests. If you want to persist those files
in S3-compatible storage after receiving them, configure Nika's
common/storage package once during bootstrap and call
storage.Upload or storage.UploadBytes from your handler.
Single File Upload
func (ctrl *UploadController) Upload(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
response.BadRequest(c, "UPLOAD_ERROR", err.Error())
return
}
// Save the file
dst := "./uploads/" + file.Filename
if err := c.SaveUploadedFile(file, dst); err != nil {
response.JSONError(c, 500, "SAVE_ERROR", err.Error())
return
}
response.OkByMsg(c, "File uploaded successfully")
}
Multiple File Upload
func (ctrl *UploadController) UploadMultiple(c *gin.Context) {
form, err := c.MultipartForm()
if err != nil {
response.BadRequest(c, "UPLOAD_ERROR", err.Error())
return
}
files := form.File["files"]
for _, file := range files {
dst := "./uploads/" + file.Filename
c.SaveUploadedFile(file, dst)
}
response.OkByMsg(c, fmt.Sprintf("%d files uploaded", len(files)))
}
File Size Limit
func (ctrl *UploadController) Upload(c *gin.Context) {
// Limit upload size to 10 MB
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, 10<<20)
file, err := c.FormFile("file")
// ...
}
File Type Validation
Use storage.ValidateFileType to validate the uploaded file from its bytes.
This is safer than trusting the client-provided Content-Type header or the
file extension.
import (
"errors"
"github.com/gin-gonic/gin"
"github.com/nika-framework/nika/common/response"
"github.com/nika-framework/nika/common/storage"
)
func (ctrl *UploadController) UploadAvatar(c *gin.Context) {
file, err := c.FormFile("avatar")
if err != nil {
response.BadRequest(c, "UPLOAD_ERROR", err.Error())
return
}
if err := storage.ValidateFileType(file, "image/jpeg", "image/png", "image/webp"); err != nil {
var typeErr *storage.TypeValidationError
if errors.As(err, &typeErr) {
response.BadRequest(c, "INVALID_FILE_TYPE", "avatar must be a JPEG, PNG, or WebP image")
return
}
response.BadRequest(c, "UPLOAD_ERROR", err.Error())
return
}
if err := c.SaveUploadedFile(file, "./uploads/"+file.Filename); err != nil {
response.JSONError(c, 500, "SAVE_ERROR", err.Error())
return
}
response.OkByMsg(c, "Avatar uploaded successfully")
}
Wildcards are supported, so image/* accepts any detected image type:
if err := storage.ValidateFileType(file, "image/*", "application/pdf"); err != nil {
response.BadRequest(c, "INVALID_FILE_TYPE", err.Error())
return
}
If you need the detected MIME type for storage.Upload, call
storage.DetectContentType after validation:
contentType, err := storage.DetectContentType(file)
if err != nil {
response.BadRequest(c, "UPLOAD_ERROR", err.Error())
return
}
Status
| Feature | Status |
|---|---|
| Single file upload | ✅ Available via Gin |
| Multiple file upload | ✅ Available via Gin |
| File size limits | ✅ Available via Gin |
| File type validation | ✅ Available via common/storage |
| S3/Cloud storage integration | ✅ Available via common/storage |