Compare commits
2 Commits
87ae7db41c
...
720322bfd6
| Author | SHA1 | Date |
|---|---|---|
|
|
720322bfd6 | |
|
|
fc915a0f7c |
|
|
@ -18,11 +18,11 @@ type File struct {
|
|||
Md5 string `gorm:"column:md5;type:varchar(255);uniqueIndex:idx_file_name"`
|
||||
}
|
||||
|
||||
func CreateFile(fileStoreName, fileName, fileType, filePath string, fileSize, authID int, NeedAuth bool) File {
|
||||
file := File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize, AuthID: authID, NeedAuth: NeedAuth}
|
||||
func CreateFile(fileStoreName, fileName, fileType, filePath, md5Str string, fileSize, authID int, NeedAuth bool) File {
|
||||
file := File{FileStoreName: fileStoreName, FileName: fileName, FileType: fileType, FilePath: filePath, FileSize: fileSize, AuthID: authID, NeedAuth: NeedAuth, Md5: md5Str}
|
||||
result := DB.Create(&file)
|
||||
if result.Error != nil {
|
||||
return 0
|
||||
return File{}
|
||||
}
|
||||
return file
|
||||
}
|
||||
|
|
|
|||
|
|
@ -126,6 +126,7 @@ func UploadFile(c *gin.Context) {
|
|||
//从请求头获取upload_type
|
||||
uploadType := c.PostForm("upload_type")
|
||||
authType := c.PostForm("auth_type")
|
||||
md5_ := c.PostForm("md5")
|
||||
if uploadType == "" {
|
||||
c.JSON(http.StatusOK, gin.H{"error": "upload_type is empty", "code": proto.ParameterError, "message": "failed"})
|
||||
return
|
||||
|
|
@ -143,14 +144,16 @@ func UploadFile(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
//计算文件md5值
|
||||
if md5_ == "" {
|
||||
file_, _ := file.Open()
|
||||
md5Value := service.CalculateFileMd5(file_)
|
||||
if md5Value == "" {
|
||||
md5_ = service.CalculateFileMd5(file_)
|
||||
if md5_ == "" {
|
||||
c.JSON(http.StatusOK, gin.H{"error": "计算文件MD5值失败", "code": proto.UploadFileFailed, "message": "failed"})
|
||||
return
|
||||
}
|
||||
}
|
||||
//查询文件是否已存在
|
||||
fileExist := dao.FindFileByMd5(md5Value)
|
||||
fileExist := dao.FindFileByMd5(md5_)
|
||||
if fileExist.ID != 0 {
|
||||
fileExist.FilePath = ""
|
||||
c.JSON(http.StatusOK, gin.H{"code": proto.SuccessCode, "message": "success", "data": fileExist})
|
||||
|
|
@ -173,7 +176,7 @@ func UploadFile(c *gin.Context) {
|
|||
} else if authType == "private" {
|
||||
auth_type_ = true
|
||||
}
|
||||
file_record := dao.CreateFile(fileStoreName, fileName, fileType, filePath, fileSize, id1, auth_type_)
|
||||
file_record := dao.CreateFile(fileStoreName, fileName, fileType, filePath, md5_, fileSize, id1, auth_type_)
|
||||
if file_record.ID == 0 {
|
||||
c.JSON(http.StatusOK, gin.H{"error": "save file info failed", "code": proto.SaveFileInfoFailed, "message": "failed"})
|
||||
return
|
||||
|
|
|
|||
Loading…
Reference in New Issue