image_type.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package main
  2. /*
  3. #cgo LDFLAGS: -s -w
  4. #include "vips.h"
  5. */
  6. import "C"
  7. import (
  8. "fmt"
  9. "net/url"
  10. "path/filepath"
  11. "strings"
  12. )
  13. type imageType int
  14. const (
  15. imageTypeUnknown = imageType(C.UNKNOWN)
  16. imageTypeJPEG = imageType(C.JPEG)
  17. imageTypePNG = imageType(C.PNG)
  18. imageTypeWEBP = imageType(C.WEBP)
  19. imageTypeGIF = imageType(C.GIF)
  20. imageTypeICO = imageType(C.ICO)
  21. imageTypeSVG = imageType(C.SVG)
  22. imageTypeHEIC = imageType(C.HEIC)
  23. imageTypeBMP = imageType(C.BMP)
  24. imageTypeTIFF = imageType(C.TIFF)
  25. contentDispositionFilenameFallback = "image"
  26. )
  27. var (
  28. imageTypes = map[string]imageType{
  29. "jpeg": imageTypeJPEG,
  30. "jpg": imageTypeJPEG,
  31. "png": imageTypePNG,
  32. "webp": imageTypeWEBP,
  33. "gif": imageTypeGIF,
  34. "ico": imageTypeICO,
  35. "svg": imageTypeSVG,
  36. "heic": imageTypeHEIC,
  37. "bmp": imageTypeBMP,
  38. "tiff": imageTypeTIFF,
  39. }
  40. mimes = map[imageType]string{
  41. imageTypeJPEG: "image/jpeg",
  42. imageTypePNG: "image/png",
  43. imageTypeWEBP: "image/webp",
  44. imageTypeGIF: "image/gif",
  45. imageTypeICO: "image/x-icon",
  46. imageTypeSVG: "image/svg+xml",
  47. imageTypeHEIC: "image/heif",
  48. imageTypeBMP: "image/bmp",
  49. imageTypeTIFF: "image/tiff",
  50. }
  51. contentDispositionsFmt = map[imageType]string{
  52. imageTypeJPEG: "inline; filename=\"%s.jpg\"",
  53. imageTypePNG: "inline; filename=\"%s.png\"",
  54. imageTypeWEBP: "inline; filename=\"%s.webp\"",
  55. imageTypeGIF: "inline; filename=\"%s.gif\"",
  56. imageTypeICO: "inline; filename=\"%s.ico\"",
  57. imageTypeSVG: "inline; filename=\"%s.svg\"",
  58. imageTypeHEIC: "inline; filename=\"%s.heic\"",
  59. imageTypeBMP: "inline; filename=\"%s.bmp\"",
  60. imageTypeTIFF: "inline; filename=\"%s.tiff\"",
  61. }
  62. )
  63. func (it imageType) String() string {
  64. for k, v := range imageTypes {
  65. if v == it {
  66. return k
  67. }
  68. }
  69. return ""
  70. }
  71. func (it imageType) MarshalJSON() ([]byte, error) {
  72. for k, v := range imageTypes {
  73. if v == it {
  74. return []byte(fmt.Sprintf("%q", k)), nil
  75. }
  76. }
  77. return []byte("null"), nil
  78. }
  79. func (it imageType) Mime() string {
  80. if mime, ok := mimes[it]; ok {
  81. return mime
  82. }
  83. return "application/octet-stream"
  84. }
  85. func (it imageType) ContentDisposition(filename string) string {
  86. format, ok := contentDispositionsFmt[it]
  87. if !ok {
  88. return "inline"
  89. }
  90. return fmt.Sprintf(format, filename)
  91. }
  92. func (it imageType) ContentDispositionFromURL(imageURL string) string {
  93. url, err := url.Parse(imageURL)
  94. if err != nil {
  95. return it.ContentDisposition(contentDispositionFilenameFallback)
  96. }
  97. _, filename := filepath.Split(url.Path)
  98. if len(filename) == 0 {
  99. return it.ContentDisposition(contentDispositionFilenameFallback)
  100. }
  101. return it.ContentDisposition(strings.TrimSuffix(filename, filepath.Ext(filename)))
  102. }