文件上传数据库
This commit is contained in:
@@ -32,26 +32,26 @@ file:
|
|||||||
other: "./data/upload/other/"
|
other: "./data/upload/other/"
|
||||||
|
|
||||||
allow_image_mime:
|
allow_image_mime:
|
||||||
image/jpeg: true
|
image/jpeg: ".jpeg"
|
||||||
image/png: true
|
image/png: ".png"
|
||||||
image/gif: true
|
image/gif: ".gif"
|
||||||
image/bmp: true
|
image/bmp: ".bmp"
|
||||||
|
|
||||||
allow_video_mime:
|
allow_video_mime:
|
||||||
video/mp4: true
|
video/mp4: ".mp4"
|
||||||
video/x-msvideo: true
|
video/x-msvideo: ".avi"
|
||||||
video/quicktime: true
|
video/quicktime: ".mov"
|
||||||
video/x-flv: true
|
video/x-flv: ".flv"
|
||||||
video/mpeg: true
|
video/mpeg: ".mpeg"
|
||||||
|
|
||||||
allow_music_mime:
|
allow_music_mime:
|
||||||
audio/mpeg: true
|
audio/mpeg: ".mpeg"
|
||||||
audio/aac: true
|
audio/aac: ".aac"
|
||||||
audio/wav: true
|
audio/wav: ".wav"
|
||||||
audio/flac: true
|
audio/flac: ".flac"
|
||||||
|
|
||||||
allow_pdf_mime:
|
allow_pdf_mime:
|
||||||
application/pdf: true
|
application/pdf: ".pdf"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +1,6 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
//mime信息转换位拓展名
|
//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 {
|
type Configs_web_t struct {
|
||||||
Host string `mapstructure:"host"`
|
Host string `mapstructure:"host"`
|
||||||
@@ -41,8 +21,8 @@ type Configs_user_t struct {
|
|||||||
type Configs_file_t struct {
|
type Configs_file_t struct {
|
||||||
Max_size uint64 `mapstructure:"max_size"`
|
Max_size uint64 `mapstructure:"max_size"`
|
||||||
Pahts map[string]string `mapstructure:"pahts"`
|
Pahts map[string]string `mapstructure:"pahts"`
|
||||||
Allow_image_mime map[string]bool `mapstructure:"allow_image_mime"`
|
Allow_image_mime map[string]string `mapstructure:"allow_image_mime"`
|
||||||
Allow_video_mime map[string]bool `mapstructure:"allow_video_mime"`
|
Allow_video_mime map[string]string `mapstructure:"allow_video_mime"`
|
||||||
Allow_music_mime map[string]bool `mapstructure:"allow_music_mime"`
|
Allow_music_mime map[string]string `mapstructure:"allow_music_mime"`
|
||||||
Allow_pdf_mime map[string]bool `mapstructure:"allow_pdf_mime"`
|
Allow_pdf_mime map[string]string `mapstructure:"allow_pdf_mime"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,19 @@ import (
|
|||||||
var DB *gorm.DB
|
var DB *gorm.DB
|
||||||
var err error
|
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 {
|
type User struct {
|
||||||
ID uint `gorm:"primaryKey;autoIncrement"` // 自增主键
|
ID uint `gorm:"primaryKey;autoIncrement"` // 自增主键
|
||||||
Name string `gorm:"size:100;uniqueIndex"` // 唯一约束索引
|
Name string `gorm:"size:100;uniqueIndex"` // 唯一约束索引
|
||||||
@@ -188,4 +201,6 @@ func Init_database() {
|
|||||||
|
|
||||||
DB.AutoMigrate(&Ticket{})
|
DB.AutoMigrate(&Ticket{})
|
||||||
|
|
||||||
|
DB.AutoMigrate(&File_info{})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
"path/filepath"
|
||||||
"saas/models"
|
"saas/models"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -49,7 +50,8 @@ func Router_file(r *gin.RouterGroup) {
|
|||||||
if file.Size > 512 {
|
if file.Size > 512 {
|
||||||
if file.Size < int64(models.Configs_file.Max_size) {
|
if file.Size < int64(models.Configs_file.Max_size) {
|
||||||
// 2. 安全获取文件名并处理路径问题
|
// 2. 安全获取文件名并处理路径问题
|
||||||
//filename := filepath.Base(file.Filename) // 防御性处理路径分隔符
|
filename := filepath.Base(file.Filename) // 防御性处理路径分隔符
|
||||||
|
//fmt.Println(filename)
|
||||||
// 3. 获取标准后缀名(含点)
|
// 3. 获取标准后缀名(含点)
|
||||||
//extWithDot := filepath.Ext(filename)
|
//extWithDot := filepath.Ext(filename)
|
||||||
|
|
||||||
@@ -62,7 +64,8 @@ func Router_file(r *gin.RouterGroup) {
|
|||||||
io.ReadFull(src_mime, buffer)
|
io.ReadFull(src_mime, buffer)
|
||||||
// 检测MIME类型
|
// 检测MIME类型
|
||||||
mimeType := http.DetectContentType(buffer)
|
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()
|
src, _ := file.Open()
|
||||||
defer src.Close()
|
defer src.Close()
|
||||||
@@ -75,10 +78,10 @@ func Router_file(r *gin.RouterGroup) {
|
|||||||
hashBytes := hasher.Sum(nil)
|
hashBytes := hasher.Sum(nil)
|
||||||
hashString := hex.EncodeToString(hashBytes)
|
hashString := hex.EncodeToString(hashBytes)
|
||||||
|
|
||||||
new_filename := fmt.Sprintf("%s", hashString)
|
new_filename := fmt.Sprintf("%s%s", hashString, file_extname)
|
||||||
file.Filename = new_filename
|
file.Filename = new_filename
|
||||||
|
|
||||||
fmt.Println(user_info)
|
//fmt.Println(user_info)
|
||||||
|
|
||||||
//这是上传的真实路径
|
//这是上传的真实路径
|
||||||
dst := path.Join(models.Configs_file.Pahts["image"], file.Filename)
|
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) {
|
if models.File_exists(dst) {
|
||||||
//fmt.Println("文件存在")
|
//fmt.Println("文件存在")
|
||||||
|
|
||||||
Return_json(ctx, "api_ok", nil)
|
|
||||||
} else {
|
} else {
|
||||||
//fmt.Println("文件no存在")
|
//fmt.Println("文件no存在")
|
||||||
ferr := ctx.SaveUploadedFile(file, dst)
|
ferr := ctx.SaveUploadedFile(file, dst)
|
||||||
if ferr == nil {
|
if ferr == nil {
|
||||||
//文件保存成功
|
//文件保存成功
|
||||||
|
|
||||||
Return_json(ctx, "api_ok", nil)
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
Return_json(ctx, "file_save_err", nil)
|
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 {
|
} else {
|
||||||
|
|
||||||
Return_json(ctx, "file_mime_err", nil)
|
Return_json(ctx, "file_mime_err", nil)
|
||||||
|
|||||||
@@ -559,7 +559,7 @@
|
|||||||
canvas.toBlob(resolve, 'image/png', 0.9)
|
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.statusCode == 200) {
|
||||||
if (c.data.err_code == 0) {
|
if (c.data.err_code == 0) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user