cover.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package bfs
  2. import (
  3. "bytes"
  4. "context"
  5. "io/ioutil"
  6. "net/http"
  7. "fmt"
  8. "go-common/library/log"
  9. )
  10. // UpVideoCovers upload video covers.
  11. func (d *Dao) UpVideoCovers(c context.Context, covers []string) (cvs []string, err error) {
  12. var (
  13. nfsURL string
  14. )
  15. for _, cv := range covers {
  16. // get nfs file
  17. bs, err := d.bvcCover(cv)
  18. if err != nil || len(bs) == 0 {
  19. log.Error("d.UpVideoCovers(%s) error(%v) or bs==0", nfsURL, err)
  20. continue
  21. }
  22. // up to bfs
  23. bfsPath, err := d.UploadArc(c, http.DetectContentType(bs), bytes.NewReader(bs))
  24. if err != nil {
  25. log.Error("d.UpVideoCovers raw url(%s) error(%v)", cv, err)
  26. continue
  27. }
  28. // parse bfs return path
  29. if err != nil {
  30. log.Error("url.Parse(%v) error(%v)", bfsPath, err)
  31. continue
  32. }
  33. cvs = append(cvs, bfsPath)
  34. log.Info("UpVideoCovers cover(%s) bfs (%s)", cv, bfsPath)
  35. }
  36. return
  37. }
  38. // bvcCover http get bvc cover bytes.
  39. func (d *Dao) bvcCover(url string) (bs []byte, err error) {
  40. resp, err := d.client.Get(url)
  41. if err != nil {
  42. log.Error("s.client.Get(%v) error(%v)", url, err)
  43. return
  44. }
  45. defer resp.Body.Close()
  46. if resp.StatusCode != http.StatusOK {
  47. err = fmt.Errorf("get NFS file faild, url(%s) http status: %d", url, resp.StatusCode)
  48. return
  49. }
  50. // read bytes
  51. if bs, err = ioutil.ReadAll(resp.Body); err != nil {
  52. log.Error("ioutil.ReadAll error(%v)", err)
  53. }
  54. return
  55. }