media.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package media
  2. import (
  3. "database/sql/driver"
  4. "image"
  5. "io"
  6. "strings"
  7. "gorm.io/gorm"
  8. "gorm.io/gorm/schema"
  9. )
  10. // Media is an interface including methods that needs for a media library storage
  11. type Media interface {
  12. Scan(value interface{}) error
  13. Value() (driver.Value, error)
  14. GetURLTemplate(*Option) string
  15. GetURL(option *Option, db *gorm.DB, field *schema.Field, templater URLTemplater) string
  16. GetFileHeader() FileHeader
  17. GetFileName() string
  18. GetSizes() map[string]*Size
  19. NeedCrop() bool
  20. Cropped(values ...bool) bool
  21. GetCropOption(name string) *image.Rectangle
  22. GetFileSizes() map[string]int
  23. Store(url string, option *Option, reader io.Reader) error
  24. Retrieve(url string) (FileInterface, error)
  25. IsImage() bool
  26. URL(style ...string) string
  27. Ext() string
  28. String() string
  29. }
  30. // FileInterface media file interface
  31. type FileInterface interface {
  32. io.ReadSeeker
  33. io.Closer
  34. }
  35. // Size is a struct, used for `GetSizes` method, it will return a slice of Size, media library will crop images automatically based on it
  36. type Size struct {
  37. Width int
  38. Height int
  39. Padding bool
  40. }
  41. // URLTemplater is a interface to return url template
  42. type URLTemplater interface {
  43. GetURLTemplate(*Option) string
  44. }
  45. // Option media library option
  46. type Option map[string]string
  47. // get option with name
  48. func (option Option) Get(key string) string {
  49. return option[strings.ToUpper(key)]
  50. }
  51. // set option
  52. func (option Option) Set(key string, val string) {
  53. option[strings.ToUpper(key)] = val
  54. return
  55. }