imagetype.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. // Code generated by gen_imagetype.go; DO NOT EDIT.
  2. package imagetype
  3. import (
  4. "fmt"
  5. "net/url"
  6. "path/filepath"
  7. "strings"
  8. )
  9. type Type int
  10. const (
  11. Unknown Type = iota
  12. JPEG
  13. JXL
  14. PNG
  15. WEBP
  16. GIF
  17. ICO
  18. SVG
  19. HEIC
  20. AVIF
  21. BMP
  22. TIFF
  23. )
  24. const (
  25. contentDispositionFilenameFallback = "image"
  26. contentDispositionsFmt = "%s; filename=\"%s%s\""
  27. )
  28. var (
  29. Types = map[string]Type{
  30. "jpeg": JPEG,
  31. "jpg": JPEG,
  32. "jxl": JXL,
  33. "png": PNG,
  34. "webp": WEBP,
  35. "gif": GIF,
  36. "ico": ICO,
  37. "svg": SVG,
  38. "heic": HEIC,
  39. "avif": AVIF,
  40. "bmp": BMP,
  41. "tiff": TIFF,
  42. }
  43. mimes = map[Type]string{
  44. JPEG: "image/jpeg",
  45. JXL: "image/jxl",
  46. PNG: "image/png",
  47. WEBP: "image/webp",
  48. GIF: "image/gif",
  49. ICO: "image/x-icon",
  50. SVG: "image/svg+xml",
  51. HEIC: "image/heif",
  52. AVIF: "image/avif",
  53. BMP: "image/bmp",
  54. TIFF: "image/tiff",
  55. }
  56. extensions = map[Type]string{
  57. JPEG: ".jpg",
  58. JXL: ".jxl",
  59. PNG: ".png",
  60. WEBP: ".webp",
  61. GIF: ".gif",
  62. ICO: ".ico",
  63. SVG: ".svg",
  64. HEIC: ".heic",
  65. AVIF: ".avif",
  66. BMP: ".bmp",
  67. TIFF: ".tiff",
  68. }
  69. )
  70. func (it Type) String() string {
  71. // JPEG has two names, we should use only the full one
  72. if it == JPEG {
  73. return "jpeg"
  74. }
  75. for k, v := range Types {
  76. if v == it {
  77. return k
  78. }
  79. }
  80. return ""
  81. }
  82. func (it Type) Ext() string {
  83. if ext, ok := extensions[it]; ok {
  84. return ext
  85. }
  86. return ""
  87. }
  88. func (it Type) MarshalJSON() ([]byte, error) {
  89. for k, v := range Types {
  90. if v == it {
  91. return []byte(fmt.Sprintf("%q", k)), nil
  92. }
  93. }
  94. return []byte("null"), nil
  95. }
  96. func (it Type) Mime() string {
  97. if mime, ok := mimes[it]; ok {
  98. return mime
  99. }
  100. return "application/octet-stream"
  101. }
  102. func (it Type) ContentDisposition(filename string, returnAttachment bool) string {
  103. return ContentDisposition(filename, it.Ext(), returnAttachment)
  104. }
  105. func (it Type) ContentDispositionFromURL(imageURL string, returnAttachment bool) string {
  106. url, err := url.Parse(imageURL)
  107. if err != nil {
  108. return it.ContentDisposition(contentDispositionFilenameFallback, returnAttachment)
  109. }
  110. _, filename := filepath.Split(url.Path)
  111. if len(filename) == 0 {
  112. return it.ContentDisposition(contentDispositionFilenameFallback, returnAttachment)
  113. }
  114. return it.ContentDisposition(strings.TrimSuffix(filename, filepath.Ext(filename)), returnAttachment)
  115. }
  116. func (it Type) IsVector() bool {
  117. return it == SVG
  118. }
  119. func (it Type) SupportsAlpha() bool {
  120. return it != JPEG
  121. }
  122. func (it Type) SupportsAnimationLoad() bool {
  123. return it == GIF || it == WEBP || it == JXL
  124. }
  125. func (it Type) SupportsAnimationSave() bool {
  126. return it == GIF || it == WEBP
  127. }
  128. func (it Type) SupportsColourProfile() bool {
  129. return it == JPEG ||
  130. it == JXL ||
  131. it == PNG ||
  132. it == WEBP ||
  133. it == HEIC ||
  134. it == AVIF
  135. }
  136. func (it Type) SupportsQuality() bool {
  137. return it == JPEG ||
  138. it == WEBP ||
  139. it == HEIC ||
  140. it == AVIF ||
  141. it == JXL ||
  142. it == TIFF
  143. }
  144. func (it Type) SupportsThumbnail() bool {
  145. return it == HEIC || it == AVIF
  146. }
  147. func ContentDisposition(filename, ext string, returnAttachment bool) string {
  148. disposition := "inline"
  149. if returnAttachment {
  150. disposition = "attachment"
  151. }
  152. return fmt.Sprintf(contentDispositionsFmt, disposition, strings.ReplaceAll(filename, `"`, "%22"), ext)
  153. }