upbfs.go 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package service
  2. import (
  3. "bytes"
  4. "context"
  5. "crypto/md5"
  6. "encoding/hex"
  7. "io"
  8. "net/http"
  9. "go-common/app/job/main/appstatic/model"
  10. "go-common/library/log"
  11. )
  12. // ParseFile analyses file info
  13. func (s *Service) ParseFile(content []byte) (file *model.FileInfo, err error) {
  14. fType := http.DetectContentType(content)
  15. // file md5
  16. md5hash := md5.New()
  17. if _, err = io.Copy(md5hash, bytes.NewReader(content)); err != nil {
  18. log.Error("resource uploadFile.Copy error(%v)", err)
  19. return
  20. }
  21. md5 := md5hash.Sum(nil)
  22. fMd5 := hex.EncodeToString(md5[:])
  23. file = &model.FileInfo{
  24. Md5: fMd5,
  25. Type: fType,
  26. Size: int64(len(content)),
  27. }
  28. return
  29. }
  30. // Upload can upload a file object: store the info in Redis, and transfer the file to Bfs
  31. func (s *Service) Upload(c context.Context, fileName string, fileType string, timing int64, body []byte) (location string, err error) {
  32. if location, err = s.dao.Upload(c, fileName, fileType, timing, body, s.c.Bfs); err != nil { // bfs
  33. log.Error("s.upload.UploadBfs() error(%v)", err)
  34. }
  35. return
  36. }