dao.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package drawimg
  2. import (
  3. "context"
  4. "go-common/app/interface/main/creative/conf"
  5. "go-common/library/log"
  6. "os"
  7. "strconv"
  8. "time"
  9. )
  10. // Dao define
  11. type Dao struct {
  12. // conf
  13. c *conf.Config
  14. // watermark
  15. dw *DrawImg
  16. }
  17. // New init dao
  18. func New(c *conf.Config) (d *Dao) {
  19. if !isExist(c.WaterMark.FontFile) {
  20. log.Error("font file not exist")
  21. return
  22. }
  23. if !isExist(c.WaterMark.UnameMark) {
  24. log.Error("uname image file not exist")
  25. return
  26. }
  27. if !isExist(c.WaterMark.UIDMark) {
  28. log.Error("uid image file not exist")
  29. return
  30. }
  31. d = &Dao{
  32. c: c,
  33. dw: NewDrawImg(c.WaterMark.FontFile, c.WaterMark.FontSize),
  34. }
  35. return
  36. }
  37. // Make create watermark.
  38. func (d *Dao) Make(c context.Context, mid int64, text string, isUname bool) (dw *DrawImg, err error) {
  39. var src string
  40. if isUname {
  41. src = d.c.WaterMark.UnameMark
  42. } else {
  43. src = d.c.WaterMark.UIDMark
  44. }
  45. img, err := d.dw.ReadSrcImg(src)
  46. if err != nil {
  47. return
  48. }
  49. if img == nil {
  50. return
  51. }
  52. d.dw.srcImg = img
  53. midStr := strconv.FormatInt(mid, 10)
  54. timestamp := strconv.FormatInt(time.Now().Unix(), 10)
  55. file := d.c.WaterMark.SaveImg + midStr + "-" + timestamp + ".png"
  56. if err = d.dw.Draw(text, file, isUname); err != nil {
  57. log.Error("d.dw.Draw error(%v)", err)
  58. return
  59. }
  60. dw = &DrawImg{
  61. CanvasWidth: d.dw.CanvasWidth,
  62. CanvasHeight: d.dw.CanvasHeight,
  63. File: file,
  64. }
  65. return
  66. }
  67. func isExist(path string) bool {
  68. _, err := os.Stat(path)
  69. return err == nil || os.IsExist(err)
  70. }