本文整理汇总了Golang中github.com/app-kit/go-appkit.File.SetBackendId方法的典型用法代码示例。如果您正苦于以下问题:Golang File.SetBackendId方法的具体用法?Golang File.SetBackendId怎么用?Golang File.SetBackendId使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/app-kit/go-appkit.File
的用法示例。
在下文中一共展示了File.SetBackendId方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BuildFile
//.........这里部分代码省略.........
if stat.IsDir() {
return apperror.New("path_is_directory")
}
// Build the hash.
hash, err2 := utils.BuildFileMD5Hash(filePath)
if err2 != nil {
return err2
}
file.SetHash(hash)
pathParts := strings.Split(filePath, string(os.PathSeparator))
fullName := pathParts[len(pathParts)-1]
nameParts := strings.Split(fullName, ".")
// Determine extension.
extension := ""
if len(nameParts) > 1 {
extension = nameParts[len(nameParts)-1]
}
file.SetFullName(fullName)
file.SetSize(stat.Size())
// Determine mime type.
mimeType := GetMimeType(filePath)
if mimeType == "" {
mimeType = mime.TypeByExtension("." + extension)
}
file.SetMime(mimeType)
if strings.HasPrefix(mimeType, "image") {
file.SetIsImage(true)
file.SetMediaType(MEDIA_TYPE_IMAGE)
// Determine image info.
imageInfo, err := GetImageInfo(filePath)
if imageInfo != nil {
file.SetWidth(int(imageInfo.Width))
file.SetHeight(int(imageInfo.Height))
} else {
h.Registry().Logger().Warningf("Could not determine image info: %v", err)
}
}
if strings.HasPrefix(mimeType, "video") {
file.SetMediaType(MEDIA_TYPE_VIdEO)
}
// Store the file in the backend.
backendId, writer, err2 := file.Writer(true)
if err2 != nil {
return apperror.Wrap(err2, "file_backend_error")
}
defer writer.Close()
// Open file for reading.
f, err := os.Open(filePath)
if err != nil {
return apperror.Wrap(err, "read_error", fmt.Sprintf("Could not read file at %v", filePath))
}
_, err = io.Copy(writer, f)
if err != nil {
f.Close()
return apperror.Wrap(err, "copy_to_backend_failed")
}
f.Close()
// File is stored in backend now!
file.SetBackendId(backendId)
// Persist file to db.
file.SetTmpPath("")
if file.GetStrId() != "" {
err2 = h.resource.Update(file, user)
} else {
err2 = h.resource.Create(file, user)
}
if err2 != nil {
// Delete file from backend again.
backend.DeleteFile(file)
return apperror.Wrap(err2, "db_error", "Could not save file to database")
}
// Delete tmp file.
if deleteFile {
os.Remove(filePath)
}
if deleteDir {
dir := strings.Join(pathParts[:len(pathParts)-1], string(os.PathSeparator))
os.RemoveAll(dir)
}
return nil
}