智慧食堂
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

svc_file_img_upload.go 2.5 KiB

1 jaar geleden
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package svc
  2. import (
  3. "applet/app/db"
  4. "applet/app/e"
  5. "applet/app/enum"
  6. "applet/app/lib/qiniu"
  7. "applet/app/md"
  8. "applet/app/utils"
  9. "errors"
  10. "fmt"
  11. "strings"
  12. "time"
  13. )
  14. // 请求文件上传
  15. func ImgReqUpload(uid, dirName, fname, callbackUrl string, fsize int64) (interface{}, error) {
  16. ext := utils.FileExt(fname)
  17. if err := initStg(fsize, ext); err != nil {
  18. return nil, err
  19. }
  20. pureFileName := strings.Replace(fname, "."+ext, "", 1)
  21. pureFileName += "-" + utils.RandString(6, utils.AnyToString(time.Now().UnixNano()))
  22. newName := dirName + "/" + pureFileName + "." + ext
  23. f := &md.FileCallback{
  24. Uid: uid,
  25. DirId: dirName,
  26. FileName: newName,
  27. }
  28. return qiniu.ReqImgUpload(f, callbackUrl), nil
  29. }
  30. func initStg(fsize int64, ext string) error {
  31. // 获取上传配置
  32. fileCfg := make(map[string]string)
  33. sysCfgDb := db.SysCfgDb{}
  34. sysCfgDb.Set()
  35. fileCfg[enum.FileBucket] = sysCfgDb.SysCfgGetWithDb(enum.FileBucket)
  36. fileCfg[enum.FileBucketHost] = sysCfgDb.SysCfgGetWithDb(enum.FileBucketHost)
  37. fileCfg[enum.FileAccessKey] = sysCfgDb.SysCfgGetWithDb(enum.FileAccessKey)
  38. fileCfg[enum.FileSecretKey] = sysCfgDb.SysCfgGetWithDb(enum.FileSecretKey)
  39. fileCfg[enum.FileBucketRegion] = sysCfgDb.SysCfgGetWithDb(enum.FileBucketRegion)
  40. fileCfg[enum.FileUserUploadMaxSize] = sysCfgDb.SysCfgGetWithDb(enum.FileUserUploadMaxSize)
  41. fileCfg[enum.FileExt] = sysCfgDb.SysCfgGetWithDb(enum.FileExt)
  42. fileCfg[enum.FileBucketScheme] = sysCfgDb.SysCfgGetWithDb(enum.FileBucketScheme)
  43. utils.FilePutContents("initStg", fmt.Sprintf("[KEY_CFG_FILE_BUCKET]:%s ; [KEY_CFG_FILE_HOST]:%s ; [KEY_CFG_FILE_AK]::%s;"+
  44. " [KEY_CFG_FILE_SK]::%s, ;[KEY_CFG_FILE_REGION]::%s, ;[KEY_CFG_FILE_MAX_SIZE]::%s, ;[KEY_CFG_FILE_EXT]::%s, ;[KEY_CFG_FILE_SCHEME]::%s, "+
  45. ">>>>>>>>>>>>>>>>>>>>", fileCfg[enum.FileBucket], fileCfg[enum.FileBucketHost], fileCfg[enum.FileAccessKey], fileCfg[enum.FileSecretKey],
  46. fileCfg[enum.FileBucketRegion], fileCfg[enum.FileUserUploadMaxSize], fileCfg[enum.FileExt], fileCfg[enum.FileBucketScheme]))
  47. for _, v := range fileCfg {
  48. if v == "" {
  49. return errors.New("上传配置不完整")
  50. }
  51. }
  52. qiniu.Init(fileCfg[enum.FileAccessKey], fileCfg[enum.FileSecretKey], fileCfg[enum.FileBucket], fileCfg[enum.FileBucketRegion], fileCfg[enum.FileBucketScheme])
  53. // 检查文件大小限制
  54. if utils.StrToInt64(fileCfg[enum.FileUserUploadMaxSize]) < fsize {
  55. return e.NewErrCode(e.ERR_FILE_MAX_SIZE)
  56. }
  57. // 检查文件后缀
  58. if !strings.Contains(fileCfg[enum.FileExt], ext) {
  59. return e.NewErrCode(e.ERR_FILE_EXT)
  60. }
  61. return nil
  62. }