Add client banner-by-id API and point frontend at new client endpoints.
Also save admin uploads locally under /public/images with a 10MB limit. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -143,6 +143,8 @@ func (h *handler) getProfile(c *gin.Context) {
|
||||
// @Router /admin/file [post]
|
||||
func (h *handler) fileUpload(c *gin.Context) {
|
||||
|
||||
const maxUploadBytes = 10 << 20 // 10 MB
|
||||
|
||||
file, header, err := c.Request.FormFile("file")
|
||||
if err != nil {
|
||||
c.String(http.StatusBadRequest, "get form err: %s", err.Error())
|
||||
@@ -156,42 +158,56 @@ func (h *handler) fileUpload(c *gin.Context) {
|
||||
}
|
||||
}(file)
|
||||
|
||||
if header.Size > maxUploadBytes {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "file size must not exceed 10 MB"})
|
||||
return
|
||||
}
|
||||
|
||||
cfg := config.GetConfig()
|
||||
appDirPath := cfg.PublicFilePath + "/images/"
|
||||
|
||||
err = os.MkdirAll(appDirPath, os.ModePerm)
|
||||
if err != nil {
|
||||
fmt.Println("wwwww", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create upload directory"})
|
||||
return
|
||||
}
|
||||
|
||||
appName := fmt.Sprintf("%d%s", time.Now().UnixNano(), filepath.Ext(header.Filename))
|
||||
dbPath := "/images/" + appName
|
||||
dbPath := "/public/images/" + appName
|
||||
localPath := appDirPath + appName
|
||||
|
||||
minioClient, err := minioUpload.ConnectionMinio()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "connection to MinIO failed"})
|
||||
if err := c.SaveUploadedFile(header, localPath); err != nil {
|
||||
fmt.Println("Error saving file to public/images:", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "File upload failed"})
|
||||
return
|
||||
}
|
||||
|
||||
contentType := header.Header.Get("Content-Type")
|
||||
ctx := context.Background()
|
||||
|
||||
if err := minioUpload.EnsureBucket(ctx, minioClient); err != nil {
|
||||
fmt.Println("ensure bucket:", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create MinIO bucket"})
|
||||
return
|
||||
}
|
||||
|
||||
size := header.Size
|
||||
if size <= 0 {
|
||||
size = -1
|
||||
}
|
||||
_, err = minioClient.PutObject(ctx, minioUpload.BucketName, appName, file, size, minio.PutObjectOptions{ContentType: contentType})
|
||||
if err != nil {
|
||||
fmt.Println("Error uploading file to MinIO:", err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "File upload to MinIO failed"})
|
||||
return
|
||||
}
|
||||
// minioClient, err := minioUpload.ConnectionMinio()
|
||||
// if err != nil {
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": "connection to MinIO failed"})
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// contentType := header.Header.Get("Content-Type")
|
||||
// ctx := context.Background()
|
||||
//
|
||||
// if err := minioUpload.EnsureBucket(ctx, minioClient); err != nil {
|
||||
// fmt.Println("ensure bucket:", err)
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create MinIO bucket"})
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// size := header.Size
|
||||
// if size <= 0 {
|
||||
// size = -1
|
||||
// }
|
||||
// _, err = minioClient.PutObject(ctx, minioUpload.BucketName, appName, file, size, minio.PutObjectOptions{ContentType: contentType})
|
||||
// if err != nil {
|
||||
// fmt.Println("Error uploading file to MinIO:", err)
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"error": "File upload to MinIO failed"})
|
||||
// return
|
||||
// }
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = dbPath
|
||||
|
||||
Reference in New Issue
Block a user