c_file.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package controllers
  2. import (
  3. "fmt"
  4. "github.com/gogf/gf/net/ghttp"
  5. "gxt-file-server/app/bll"
  6. context2 "gxt-file-server/app/context"
  7. "gxt-file-server/app/errors"
  8. "gxt-file-server/app/schema"
  9. "gxt-file-server/pkg/gplus"
  10. "net/url"
  11. "strconv"
  12. "strings"
  13. )
  14. // NewFile 创建文件管理控制器
  15. func NewFile(bFile bll.IFile) *File {
  16. return &File{
  17. FileBll: bFile,
  18. }
  19. }
  20. // File 文件管理
  21. // @Name File
  22. // @Description 文件管理
  23. type File struct {
  24. FileBll bll.IFile
  25. }
  26. // Upload 上传文件
  27. func (a *File) Upload(r *ghttp.Request) {
  28. var params schema.UploadParams
  29. if err := gplus.ParseJson(r, &params); err != nil {
  30. gplus.ResError(r, err)
  31. }
  32. ctx := gplus.NewContext(r)
  33. if v := r.Header.Get("FILE-EXPIRE"); v != "" {
  34. m, err := strconv.Atoi(v)
  35. if err != nil {
  36. gplus.ResError(r, errors.ErrHeaderParamsError)
  37. }
  38. ctx = context2.NewFileExpireContext(ctx, m)
  39. }
  40. if v := r.Header.Get("FILE-HASH"); v != "" {
  41. ctx = context2.NewFileHashContext(ctx, v)
  42. }
  43. info, err := a.FileBll.Upload(ctx, r.Request, params.FormKey, params.BaseUrl)
  44. if err != nil {
  45. gplus.ResError(r, err)
  46. return
  47. }
  48. gplus.ResSuccess(r, info)
  49. }
  50. // Download 下载文件
  51. func (a *File) Download(r *ghttp.Request) {
  52. if r.GetString("path") == "" || r.GetString("name") == "" {
  53. gplus.ResError(r, errors.ErrBadRequest)
  54. }
  55. path, err := url.PathUnescape(r.GetString("path"))
  56. if err != nil {
  57. gplus.ResError(r, err)
  58. }
  59. fileData, contentType, err := a.FileBll.Download(gplus.NewContext(r), path)
  60. if err != nil {
  61. gplus.ResError(r, err)
  62. }
  63. if strings.HasPrefix(contentType, "text/html") ||
  64. strings.HasPrefix(contentType, "application/javascript") {
  65. contentType = "text/plain; charset=utf-8"
  66. }
  67. r.Response.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", r.GetString("name")))
  68. r.Response.Header().Set("Content-Type", "application/octet-stream")
  69. // r.Response.Header().Set("Content-Type", contentType)
  70. r.Response.Header().Set("Cache-Control", "max-age=31536000")
  71. r.Response.Header().Set("Content-Length", strconv.FormatInt(int64(len(fileData)), 10))
  72. r.Response.Write(fileData)
  73. }
  74. // Persistent 设置文件为持久化
  75. func (a *File) Persistent(r *ghttp.Request) {
  76. var param schema.PersistentFileRequest
  77. if err := gplus.ParseJson(r, &param); err != nil {
  78. gplus.ResError(r, err)
  79. }
  80. err := a.FileBll.Persistent(gplus.NewContext(r), param.Hash)
  81. if err != nil {
  82. gplus.ResError(r, err)
  83. }
  84. gplus.ResOK(r)
  85. }
  86. // Chunk 分块上传
  87. func (a *File) Chunk(r *ghttp.Request) {
  88. var param schema.FileChunkUploadReq
  89. if err := gplus.ParseJson(r, &param); err != nil {
  90. gplus.ResError(r, err)
  91. }
  92. result, err := a.FileBll.ChunkUpload(gplus.NewContext(r), schema.FileChunkParams{
  93. HttpRequest: r.Request,
  94. FormKey: param.FormKey,
  95. BaseUrl: param.BaseUrl,
  96. Index: param.Index,
  97. Total: param.Total,
  98. Hash: param.Hash,
  99. })
  100. if err != nil {
  101. gplus.ResError(r, err)
  102. }
  103. gplus.ResSuccess(r, result)
  104. }
  105. // Merge 文件分块合并
  106. func (a *File) Merge(r *ghttp.Request) {
  107. var param schema.FileMergeParams
  108. if err := gplus.ParseJson(r, &param); err != nil {
  109. gplus.ResError(r, err)
  110. }
  111. result, err := a.FileBll.FileMerge(gplus.NewContext(r), schema.FileMergeParams{
  112. HttpRequest: r.Request,
  113. BaseUrl: param.BaseUrl,
  114. Total: param.Total,
  115. Hash: param.Hash,
  116. FileName: param.FileName,
  117. })
  118. if err != nil {
  119. gplus.ResError(r, err)
  120. }
  121. gplus.ResSuccess(r, result)
  122. }