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.

85 lines
2.6 KiB

3 years ago
3 years ago
  1. package upload
  2. import (
  3. "errors"
  4. "io"
  5. "mime/multipart"
  6. "os"
  7. "path"
  8. "strings"
  9. "time"
  10. "github.com/flipped-aurora/gin-vue-admin/server/global"
  11. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  12. "go.uber.org/zap"
  13. )
  14. type Local struct{}
  15. //@author: [piexlmax](https://github.com/piexlmax)
  16. //@author: [ccfish86](https://github.com/ccfish86)
  17. //@author: [SliverHorn](https://github.com/SliverHorn)
  18. //@object: *Local
  19. //@function: UploadFile
  20. //@description: 上传文件
  21. //@param: file *multipart.FileHeader
  22. //@return: string, string, error
  23. func (*Local) UploadFile(file *multipart.FileHeader) (string, string, error) {
  24. // 读取文件后缀
  25. ext := path.Ext(file.Filename)
  26. // 读取文件名并加密
  27. name := strings.TrimSuffix(file.Filename, ext)
  28. name = utils.MD5V([]byte(name))
  29. // 拼接新文件名
  30. filename := name + "_" + time.Now().Format("20060102150405") + ext
  31. // 尝试创建此路径
  32. mkdirErr := os.MkdirAll(global.GVA_CONFIG.Local.Path, os.ModePerm)
  33. if mkdirErr != nil {
  34. global.GVA_LOG.Error("function os.MkdirAll() Filed", zap.Any("err", mkdirErr.Error()))
  35. return "", "", errors.New("function os.MkdirAll() Filed, err:" + mkdirErr.Error())
  36. }
  37. // 拼接路径和文件名
  38. p := global.GVA_CONFIG.Local.Path + "/" + filename
  39. f, openError := file.Open() // 读取文件
  40. if openError != nil {
  41. global.GVA_LOG.Error("function file.Open() Filed", zap.Any("err", openError.Error()))
  42. return "", "", errors.New("function file.Open() Filed, err:" + openError.Error())
  43. }
  44. defer f.Close() // 创建文件 defer 关闭
  45. out, createErr := os.Create(p)
  46. if createErr != nil {
  47. global.GVA_LOG.Error("function os.Create() Filed", zap.Any("err", createErr.Error()))
  48. return "", "", errors.New("function os.Create() Filed, err:" + createErr.Error())
  49. }
  50. defer out.Close() // 创建文件 defer 关闭
  51. _, copyErr := io.Copy(out, f) // 传输(拷贝)文件
  52. if copyErr != nil {
  53. global.GVA_LOG.Error("function io.Copy() Filed", zap.Any("err", copyErr.Error()))
  54. return "", "", errors.New("function io.Copy() Filed, err:" + copyErr.Error())
  55. }
  56. return p, filename, nil
  57. }
  58. //@author: [piexlmax](https://github.com/piexlmax)
  59. //@author: [ccfish86](https://github.com/ccfish86)
  60. //@author: [SliverHorn](https://github.com/SliverHorn)
  61. //@object: *Local
  62. //@function: DeleteFile
  63. //@description: 删除文件
  64. //@param: key string
  65. //@return: error
  66. func (*Local) DeleteFile(key string) error {
  67. p := global.GVA_CONFIG.Local.Path + "/" + key
  68. if strings.Contains(p, global.GVA_CONFIG.Local.Path) {
  69. if err := os.Remove(p); err != nil {
  70. return errors.New("本地文件删除失败, err:" + err.Error())
  71. }
  72. }
  73. return nil
  74. }