diff --git a/gin_ops/def_config/config_temp.yaml b/gin_ops/def_config/config_temp.yaml index 2d1e27a..74b2afc 100644 --- a/gin_ops/def_config/config_temp.yaml +++ b/gin_ops/def_config/config_temp.yaml @@ -32,26 +32,26 @@ file: other: "./data/upload/other/" allow_image_mime: - image/jpeg: true - image/png: true - image/gif: true - image/bmp: true + image/jpeg: ".jpeg" + image/png: ".png" + image/gif: ".gif" + image/bmp: ".bmp" allow_video_mime: - video/mp4: true - video/x-msvideo: true - video/quicktime: true - video/x-flv: true - video/mpeg: true + video/mp4: ".mp4" + video/x-msvideo: ".avi" + video/quicktime: ".mov" + video/x-flv: ".flv" + video/mpeg: ".mpeg" allow_music_mime: - audio/mpeg: true - audio/aac: true - audio/wav: true - audio/flac: true + audio/mpeg: ".mpeg" + audio/aac: ".aac" + audio/wav: ".wav" + audio/flac: ".flac" allow_pdf_mime: - application/pdf: true + application/pdf: ".pdf" diff --git a/gin_ops/models/config_head.go b/gin_ops/models/config_head.go index 92ebf00..06acb70 100644 --- a/gin_ops/models/config_head.go +++ b/gin_ops/models/config_head.go @@ -1,26 +1,6 @@ package models //mime信息转换位拓展名 -var Mime_to_extname = map[string]string{ - - "image/jpeg": ".jpeg", - "image/png": ".png", - "image/gif": ".gif", - "image/bmp": ".bmp", - - "video/mp4": ".mp4", - "video/x-msvideo": ".", - "video/quicktime": ".", - "video/x-flv": ".flv", - "video/mpeg": ".mpeg", - - "audio/mpeg": ".mp3", - "audio/aac": ".acc", - "audio/wav": ".wav", - "audio/flac": ".flac", - - "application/pdf": ".pdf", -} type Configs_web_t struct { Host string `mapstructure:"host"` @@ -41,8 +21,8 @@ type Configs_user_t struct { type Configs_file_t struct { Max_size uint64 `mapstructure:"max_size"` Pahts map[string]string `mapstructure:"pahts"` - Allow_image_mime map[string]bool `mapstructure:"allow_image_mime"` - Allow_video_mime map[string]bool `mapstructure:"allow_video_mime"` - Allow_music_mime map[string]bool `mapstructure:"allow_music_mime"` - Allow_pdf_mime map[string]bool `mapstructure:"allow_pdf_mime"` + Allow_image_mime map[string]string `mapstructure:"allow_image_mime"` + Allow_video_mime map[string]string `mapstructure:"allow_video_mime"` + Allow_music_mime map[string]string `mapstructure:"allow_music_mime"` + Allow_pdf_mime map[string]string `mapstructure:"allow_pdf_mime"` } diff --git a/gin_ops/models/database.go b/gin_ops/models/database.go index efec5e9..99de474 100644 --- a/gin_ops/models/database.go +++ b/gin_ops/models/database.go @@ -12,6 +12,19 @@ import ( var DB *gorm.DB var err error +type File_info struct { + ID uint `gorm:"primaryKey;autoIncrement"` + Name string `gorm:"not null;size:256;index"` // 前端报告的文件名 + Path string `gorm:"not null;size:300"` // + Sha256 string `gorm:"not null;size:256;index"` // + Mime string `gorm:"size:64;index"` + Type string `gorm:"size:64;index"` + Const uint `gorm:"default:1;index"` + Per uint `gorm:"default:1"` + UserID uint `gorm:"not null;index"` + Date time.Time `gorm:"type:datetime;default:CURRENT_TIMESTAMP"` // 默认当前时间 +} + type User struct { ID uint `gorm:"primaryKey;autoIncrement"` // 自增主键 Name string `gorm:"size:100;uniqueIndex"` // 唯一约束索引 @@ -188,4 +201,6 @@ func Init_database() { DB.AutoMigrate(&Ticket{}) + DB.AutoMigrate(&File_info{}) + } diff --git a/gin_ops/routers/routers_flie.go b/gin_ops/routers/routers_flie.go index 568f083..fa84495 100644 --- a/gin_ops/routers/routers_flie.go +++ b/gin_ops/routers/routers_flie.go @@ -9,6 +9,7 @@ import ( "io" "net/http" "path" + "path/filepath" "saas/models" "github.com/gin-gonic/gin" @@ -49,7 +50,8 @@ func Router_file(r *gin.RouterGroup) { if file.Size > 512 { if file.Size < int64(models.Configs_file.Max_size) { // 2. 安全获取文件名并处理路径问题 - //filename := filepath.Base(file.Filename) // 防御性处理路径分隔符 + filename := filepath.Base(file.Filename) // 防御性处理路径分隔符 + //fmt.Println(filename) // 3. 获取标准后缀名(含点) //extWithDot := filepath.Ext(filename) @@ -62,7 +64,8 @@ func Router_file(r *gin.RouterGroup) { io.ReadFull(src_mime, buffer) // 检测MIME类型 mimeType := http.DetectContentType(buffer) - if models.Configs_file.Allow_image_mime[mimeType] { + file_extname := models.Configs_file.Allow_image_mime[mimeType] + if file_extname != "" { // 打开文件流 src, _ := file.Open() defer src.Close() @@ -75,10 +78,10 @@ func Router_file(r *gin.RouterGroup) { hashBytes := hasher.Sum(nil) hashString := hex.EncodeToString(hashBytes) - new_filename := fmt.Sprintf("%s", hashString) + new_filename := fmt.Sprintf("%s%s", hashString, file_extname) file.Filename = new_filename - fmt.Println(user_info) + //fmt.Println(user_info) //这是上传的真实路径 dst := path.Join(models.Configs_file.Pahts["image"], file.Filename) @@ -87,19 +90,47 @@ func Router_file(r *gin.RouterGroup) { if models.File_exists(dst) { //fmt.Println("文件存在") - Return_json(ctx, "api_ok", nil) } else { //fmt.Println("文件no存在") ferr := ctx.SaveUploadedFile(file, dst) if ferr == nil { //文件保存成功 - Return_json(ctx, "api_ok", nil) } else { Return_json(ctx, "file_save_err", nil) + ctx.Abort() //end } } + //记录到数据库 + + //先检查数据库有没有数据 + fund_file_info := models.File_info{ + Name: filename, + Sha256: hashString, + Mime: mimeType, + Type: "image", + UserID: user_info.UserID, + } + fund_file_info2 := models.File_info{} + + models.DB.Where(&fund_file_info).Find(&fund_file_info2) + + if fund_file_info2.ID != 0 { + fmt.Println(fund_file_info2) + fund_file_info2.Const += 1 + models.DB.Where(&fund_file_info).Updates(&fund_file_info2) + } else { + models.DB.Create(&fund_file_info) // 传入指针 + fund_file_info2 = fund_file_info + } + + red := map[string]interface{}{ + "data": fund_file_info2, + } + + Return_json(ctx, "api_ok", red) + } else { Return_json(ctx, "file_mime_err", nil) diff --git a/gin_ops/templates/setting-my.html b/gin_ops/templates/setting-my.html index a3ad1e9..f24ec0b 100644 --- a/gin_ops/templates/setting-my.html +++ b/gin_ops/templates/setting-my.html @@ -559,7 +559,7 @@ canvas.toBlob(resolve, 'image/png', 0.9) ); - post_file("/image", blob, `avatar_${Date.now()}.jpg`, (c) => { + post_file("/image", blob, `avatar.png`, (c) => { if (c.statusCode == 200) { if (c.data.err_code == 0) {