file.go 901 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package service
  2. import (
  3. "context"
  4. "net/http"
  5. "time"
  6. "go-common/app/admin/main/upload/model"
  7. "github.com/pkg/errors"
  8. )
  9. // UploadAdminRecord upload file to bfs
  10. func (s *Service) UploadAdminRecord(ctx context.Context, action string, up *model.UploadParam, data []byte) (result *model.UploadResult, err error) {
  11. var (
  12. location, etag string
  13. b *model.Bucket
  14. ok bool
  15. )
  16. if b, ok = s.bucketCache[up.Bucket]; !ok {
  17. err = errors.Errorf("read bucket items failed: (%s)", up.Bucket)
  18. return
  19. }
  20. // auth calc.
  21. up.Auth = s.dao.Bfs.Authorize(b.KeyID, b.KeySecret, http.MethodPut, up.Bucket, up.FileName, time.Now().Unix())
  22. if up.ContentType == "" {
  23. up.ContentType = http.DetectContentType(data)
  24. }
  25. if location, etag, err = s.dao.Bfs.Upload(ctx, up, data); err != nil {
  26. return
  27. }
  28. result = &model.UploadResult{
  29. Location: location,
  30. Etag: etag,
  31. }
  32. return
  33. }