手动判断文件名是否为空
This commit is contained in:
@@ -18,5 +18,6 @@
|
|||||||
"warehouses_api_err":15,
|
"warehouses_api_err":15,
|
||||||
"warehouses_name_err":16,
|
"warehouses_name_err":16,
|
||||||
"warehouses_id_err":17,
|
"warehouses_id_err":17,
|
||||||
"file_id_error":18
|
"file_id_error":18,
|
||||||
|
"file_name_err":19
|
||||||
}
|
}
|
||||||
@@ -23,19 +23,24 @@ func Router_file(r *gin.RouterGroup) {
|
|||||||
id := ctx.Param("id")
|
id := ctx.Param("id")
|
||||||
id_int, err := strconv.ParseInt(id, 10, 0)
|
id_int, err := strconv.ParseInt(id, 10, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
//fmt.Println(id_int)
|
if id_int != 0 {
|
||||||
file_info := models.File_info{
|
//fmt.Println(id_int)
|
||||||
ID: uint(id_int),
|
file_info := models.File_info{
|
||||||
}
|
ID: uint(id_int),
|
||||||
if models.DB.Where(file_info.ID).First(&file_info).Error == nil {
|
}
|
||||||
fmt.Println(file_info)
|
if models.DB.Where(&file_info).First(&file_info).Error == nil {
|
||||||
|
fmt.Println(file_info)
|
||||||
|
} else {
|
||||||
|
fmt.Println("not fund")
|
||||||
|
}
|
||||||
|
|
||||||
|
Return_json(ctx, "api_ok", map[string]interface{}{
|
||||||
|
"data": file_info,
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
fmt.Println("not fund")
|
Return_json(ctx, "file_id_error", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
Return_json(ctx, "api_ok", map[string]interface{}{
|
|
||||||
"data": file_info,
|
|
||||||
})
|
|
||||||
} else {
|
} else {
|
||||||
Return_json(ctx, "file_id_error", nil)
|
Return_json(ctx, "file_id_error", nil)
|
||||||
}
|
}
|
||||||
@@ -66,104 +71,111 @@ func Router_file(r *gin.RouterGroup) {
|
|||||||
file, err := ctx.FormFile("file")
|
file, err := ctx.FormFile("file")
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
||||||
//限制文件大小
|
if file.Filename != "" {
|
||||||
if file.Size > 512 {
|
|
||||||
if file.Size < int64(models.Configs_file.Max_size) {
|
|
||||||
// 2. 安全获取文件名并处理路径问题
|
|
||||||
filename := filepath.Base(file.Filename) // 防御性处理路径分隔符
|
|
||||||
//fmt.Println(filename)
|
|
||||||
// 3. 获取标准后缀名(含点)
|
|
||||||
//extWithDot := filepath.Ext(filename)
|
|
||||||
|
|
||||||
//判断文件mime是否合法
|
//限制文件大小
|
||||||
// 打开文件流
|
if file.Size > 512 {
|
||||||
src_mime, _ := file.Open()
|
if file.Size < int64(models.Configs_file.Max_size) {
|
||||||
defer src_mime.Close()
|
// 2. 安全获取文件名并处理路径问题
|
||||||
// 读取前512字节用于MIME检测
|
filename := filepath.Base(file.Filename) // 防御性处理路径分隔符
|
||||||
buffer := make([]byte, 512)
|
//fmt.Println(filename)
|
||||||
io.ReadFull(src_mime, buffer)
|
// 3. 获取标准后缀名(含点)
|
||||||
// 检测MIME类型
|
//extWithDot := filepath.Ext(filename)
|
||||||
mimeType := http.DetectContentType(buffer)
|
|
||||||
file_extname := models.Configs_file.Allow_image_mime[mimeType]
|
//判断文件mime是否合法
|
||||||
if file_extname != "" {
|
|
||||||
// 打开文件流
|
// 打开文件流
|
||||||
src, _ := file.Open()
|
src_mime, _ := file.Open()
|
||||||
defer src.Close()
|
defer src_mime.Close()
|
||||||
// 创建SHA256哈希器
|
// 读取前512字节用于MIME检测
|
||||||
hasher := sha256.New()
|
buffer := make([]byte, 512)
|
||||||
|
io.ReadFull(src_mime, buffer)
|
||||||
|
// 检测MIME类型
|
||||||
|
mimeType := http.DetectContentType(buffer)
|
||||||
|
file_extname := models.Configs_file.Allow_image_mime[mimeType]
|
||||||
|
if file_extname != "" {
|
||||||
|
// 打开文件流
|
||||||
|
src, _ := file.Open()
|
||||||
|
defer src.Close()
|
||||||
|
// 创建SHA256哈希器
|
||||||
|
hasher := sha256.New()
|
||||||
|
|
||||||
// 计算哈希值
|
// 计算哈希值
|
||||||
io.Copy(hasher, src)
|
io.Copy(hasher, src)
|
||||||
// 获取哈希结果
|
// 获取哈希结果
|
||||||
hashBytes := hasher.Sum(nil)
|
hashBytes := hasher.Sum(nil)
|
||||||
hashString := hex.EncodeToString(hashBytes)
|
hashString := hex.EncodeToString(hashBytes)
|
||||||
|
|
||||||
new_filename := fmt.Sprintf("%s%s", hashString, file_extname)
|
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)
|
||||||
|
|
||||||
//判断文件是否存在避免重复保存
|
//判断文件是否存在避免重复保存
|
||||||
if models.File_exists(dst) {
|
if models.File_exists(dst) {
|
||||||
//fmt.Println("文件存在")
|
//fmt.Println("文件存在")
|
||||||
|
|
||||||
} else {
|
|
||||||
//fmt.Println("文件no存在")
|
|
||||||
ferr := ctx.SaveUploadedFile(file, dst)
|
|
||||||
if ferr == nil {
|
|
||||||
//文件保存成功
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
//fmt.Println("文件no存在")
|
||||||
|
ferr := ctx.SaveUploadedFile(file, dst)
|
||||||
|
if ferr == nil {
|
||||||
|
//文件保存成功
|
||||||
|
|
||||||
Return_json(ctx, "file_save_err", nil)
|
} else {
|
||||||
ctx.Abort() //end
|
|
||||||
|
Return_json(ctx, "file_save_err", nil)
|
||||||
|
ctx.Abort() //end
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
//记录到数据库
|
||||||
//记录到数据库
|
|
||||||
|
|
||||||
//先检查数据库有没有数据
|
//先检查数据库有没有数据
|
||||||
fund_file_info := models.File_info{
|
fund_file_info := models.File_info{
|
||||||
Name: filename,
|
Name: filename,
|
||||||
Sha256: hashString,
|
Sha256: hashString,
|
||||||
Mime: mimeType,
|
Mime: mimeType,
|
||||||
Type: "image",
|
Type: "image",
|
||||||
UserID: user_info.UserID,
|
UserID: user_info.UserID,
|
||||||
}
|
}
|
||||||
fund_file_info2 := models.File_info{}
|
fund_file_info2 := models.File_info{}
|
||||||
|
|
||||||
models.DB.Where(&fund_file_info).Find(&fund_file_info2)
|
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)
|
||||||
|
|
||||||
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 {
|
} else {
|
||||||
models.DB.Create(&fund_file_info) // 传入指针
|
|
||||||
fund_file_info2 = fund_file_info
|
|
||||||
}
|
|
||||||
|
|
||||||
red := map[string]interface{}{
|
Return_json(ctx, "file_mime_err", nil)
|
||||||
"data": fund_file_info2,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Return_json(ctx, "api_ok", red)
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
Return_json(ctx, "file_mime_err", nil)
|
Return_json(ctx, "file_size_err", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
Return_json(ctx, "file_size_err", nil)
|
Return_json(ctx, "file_size_err", nil)
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
|
|
||||||
Return_json(ctx, "file_size_err", nil)
|
} else {
|
||||||
|
Return_json(ctx, "file_name_err", nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Return_json(ctx, "file_get_err", nil)
|
Return_json(ctx, "file_get_err", nil)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user