processing_options.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. package main
  2. /*
  3. #cgo LDFLAGS: -s -w
  4. #include <image_types.h>
  5. */
  6. import "C"
  7. import (
  8. "encoding/base64"
  9. "errors"
  10. "fmt"
  11. "net/http"
  12. "strconv"
  13. "strings"
  14. )
  15. type urlOptions map[string][]string
  16. type imageType int
  17. const (
  18. imageTypeUnknown = C.UNKNOWN
  19. imageTypeJPEG = C.JPEG
  20. imageTypePNG = C.PNG
  21. imageTypeWEBP = C.WEBP
  22. imageTypeGIF = C.GIF
  23. )
  24. var imageTypes = map[string]imageType{
  25. "jpeg": imageTypeJPEG,
  26. "jpg": imageTypeJPEG,
  27. "png": imageTypePNG,
  28. "webp": imageTypeWEBP,
  29. "gif": imageTypeGIF,
  30. }
  31. type gravityType int
  32. const (
  33. gravityCenter gravityType = iota
  34. gravityNorth
  35. gravityEast
  36. gravitySouth
  37. gravityWest
  38. gravitySmart
  39. )
  40. var gravityTypes = map[string]gravityType{
  41. "ce": gravityCenter,
  42. "no": gravityNorth,
  43. "ea": gravityEast,
  44. "so": gravitySouth,
  45. "we": gravityWest,
  46. "sm": gravitySmart,
  47. }
  48. type resizeType int
  49. const (
  50. resizeFit resizeType = iota
  51. resizeFill
  52. resizeCrop
  53. )
  54. var resizeTypes = map[string]resizeType{
  55. "fit": resizeFit,
  56. "fill": resizeFill,
  57. "crop": resizeCrop,
  58. }
  59. type processingOptions struct {
  60. Resize resizeType
  61. Width int
  62. Height int
  63. Gravity gravityType
  64. Enlarge bool
  65. Format imageType
  66. Blur float32
  67. Sharpen float32
  68. }
  69. func defaultProcessingOptions() processingOptions {
  70. return processingOptions{
  71. Resize: resizeFit,
  72. Width: 0,
  73. Height: 0,
  74. Gravity: gravityCenter,
  75. Enlarge: false,
  76. Format: imageTypeJPEG,
  77. Blur: 0,
  78. Sharpen: 0,
  79. }
  80. }
  81. func decodeURL(parts []string) (string, imageType, error) {
  82. var imgType imageType = imageTypeJPEG
  83. urlParts := strings.Split(strings.Join(parts, ""), ".")
  84. if len(urlParts) > 2 {
  85. return "", 0, errors.New("Invalid url encoding")
  86. }
  87. if len(urlParts) == 2 {
  88. if f, ok := imageTypes[urlParts[1]]; ok {
  89. imgType = f
  90. } else {
  91. return "", 0, fmt.Errorf("Invalid image format: %s", urlParts[1])
  92. }
  93. }
  94. url, err := base64.RawURLEncoding.DecodeString(urlParts[0])
  95. if err != nil {
  96. return "", 0, errors.New("Invalid url encoding")
  97. }
  98. return string(url), imgType, nil
  99. }
  100. func applyWidthOption(po *processingOptions, args []string) error {
  101. if len(args) > 1 {
  102. return fmt.Errorf("Invalid width arguments: %v", args)
  103. }
  104. if w, err := strconv.Atoi(args[0]); err == nil || w >= 0 {
  105. po.Width = w
  106. } else {
  107. return fmt.Errorf("Invalid width: %s", args[0])
  108. }
  109. return nil
  110. }
  111. func applyHeightOption(po *processingOptions, args []string) error {
  112. if len(args) > 1 {
  113. return fmt.Errorf("Invalid height arguments: %v", args)
  114. }
  115. if h, err := strconv.Atoi(args[0]); err == nil || po.Height >= 0 {
  116. po.Height = h
  117. } else {
  118. return fmt.Errorf("Invalid height: %s", args[0])
  119. }
  120. return nil
  121. }
  122. func applyEnlargeOption(po *processingOptions, args []string) error {
  123. if len(args) > 1 {
  124. return fmt.Errorf("Invalid enlarge arguments: %v", args)
  125. }
  126. po.Enlarge = args[0] != "0"
  127. return nil
  128. }
  129. func applySizeOption(po *processingOptions, args []string) (err error) {
  130. if len(args) > 3 {
  131. return fmt.Errorf("Invalid size arguments: %v", args)
  132. }
  133. if len(args) >= 1 {
  134. if err = applyWidthOption(po, args[0:1]); err != nil {
  135. return
  136. }
  137. }
  138. if len(args) >= 2 {
  139. if err = applyHeightOption(po, args[1:2]); err != nil {
  140. return
  141. }
  142. }
  143. if len(args) == 3 {
  144. if err = applyEnlargeOption(po, args[2:3]); err != nil {
  145. return
  146. }
  147. }
  148. return nil
  149. }
  150. func applyResizeOption(po *processingOptions, args []string) error {
  151. if len(args) > 4 {
  152. return fmt.Errorf("Invalid resize arguments: %v", args)
  153. }
  154. if r, ok := resizeTypes[args[0]]; ok {
  155. po.Resize = r
  156. } else {
  157. return fmt.Errorf("Invalid resize type: %s", args[0])
  158. }
  159. if len(args) > 1 {
  160. if err := applySizeOption(po, args[1:]); err != nil {
  161. return err
  162. }
  163. }
  164. return nil
  165. }
  166. func applyGravityOption(po *processingOptions, args []string) error {
  167. if len(args) > 1 {
  168. return fmt.Errorf("Invalid resize arguments: %v", args)
  169. }
  170. if g, ok := gravityTypes[args[0]]; ok {
  171. po.Gravity = g
  172. } else {
  173. return fmt.Errorf("Invalid gravity: %s", args[0])
  174. }
  175. return nil
  176. }
  177. func applyBlurOption(po *processingOptions, args []string) error {
  178. if len(args) > 1 {
  179. return fmt.Errorf("Invalid blur arguments: %v", args)
  180. }
  181. if b, err := strconv.ParseFloat(args[0], 32); err == nil || b >= 0 {
  182. po.Blur = float32(b)
  183. } else {
  184. return fmt.Errorf("Invalid blur: %s", args[0])
  185. }
  186. return nil
  187. }
  188. func applySharpenOption(po *processingOptions, args []string) error {
  189. if len(args) > 1 {
  190. return fmt.Errorf("Invalid sharpen arguments: %v", args)
  191. }
  192. if s, err := strconv.ParseFloat(args[0], 32); err == nil || s >= 0 {
  193. po.Sharpen = float32(s)
  194. } else {
  195. return fmt.Errorf("Invalid sharpen: %s", args[0])
  196. }
  197. return nil
  198. }
  199. func applyPresetOption(po *processingOptions, args []string) error {
  200. for _, preset := range args {
  201. if p, ok := conf.Presets[preset]; ok {
  202. for name, pargs := range p {
  203. if err := applyProcessingOption(po, name, pargs); err != nil {
  204. return err
  205. }
  206. }
  207. } else {
  208. return fmt.Errorf("Unknown asset: %s", preset)
  209. }
  210. }
  211. return nil
  212. }
  213. func applyFormatOption(po *processingOptions, imgType imageType) error {
  214. if !vipsTypeSupportSave[imgType] {
  215. return errors.New("Resulting image type not supported")
  216. }
  217. po.Format = imgType
  218. return nil
  219. }
  220. func applyProcessingOption(po *processingOptions, name string, args []string) error {
  221. switch name {
  222. case "resize":
  223. if err := applyResizeOption(po, args); err != nil {
  224. return err
  225. }
  226. case "size":
  227. if err := applySizeOption(po, args); err != nil {
  228. return err
  229. }
  230. case "width":
  231. if err := applyWidthOption(po, args); err != nil {
  232. return err
  233. }
  234. case "height":
  235. if err := applyHeightOption(po, args); err != nil {
  236. return err
  237. }
  238. case "enlarge":
  239. if err := applyEnlargeOption(po, args); err != nil {
  240. return err
  241. }
  242. case "gravity":
  243. if err := applyGravityOption(po, args); err != nil {
  244. return err
  245. }
  246. case "blur":
  247. if err := applyBlurOption(po, args); err != nil {
  248. return err
  249. }
  250. case "sharpen":
  251. if err := applySharpenOption(po, args); err != nil {
  252. return err
  253. }
  254. case "preset":
  255. if err := applyPresetOption(po, args); err != nil {
  256. return err
  257. }
  258. }
  259. return nil
  260. }
  261. func parseURLOptions(opts []string) (urlOptions, []string) {
  262. parsed := make(urlOptions)
  263. urlStart := len(opts) + 1
  264. for i, opt := range opts {
  265. args := strings.Split(opt, ":")
  266. if len(args) == 1 {
  267. urlStart = i
  268. break
  269. }
  270. parsed[args[0]] = args[1:]
  271. }
  272. var rest []string
  273. if urlStart < len(opts) {
  274. rest = opts[urlStart:]
  275. } else {
  276. rest = []string{}
  277. }
  278. return parsed, rest
  279. }
  280. func parsePathAdvanced(parts []string) (string, processingOptions, error) {
  281. po := defaultProcessingOptions()
  282. options, urlParts := parseURLOptions(parts)
  283. for name, args := range options {
  284. if err := applyProcessingOption(&po, name, args); err != nil {
  285. return "", po, err
  286. }
  287. }
  288. url, imgType, err := decodeURL(urlParts)
  289. if err != nil {
  290. return "", po, err
  291. }
  292. if err := applyFormatOption(&po, imgType); err != nil {
  293. return "", po, errors.New("Resulting image type not supported")
  294. }
  295. return string(url), po, nil
  296. }
  297. func parsePathSimple(parts []string) (string, processingOptions, error) {
  298. var po processingOptions
  299. var err error
  300. if len(parts) < 6 {
  301. return "", po, errors.New("Invalid path")
  302. }
  303. po.Resize = resizeTypes[parts[0]]
  304. if err = applyWidthOption(&po, parts[1:2]); err != nil {
  305. return "", po, err
  306. }
  307. if err = applyHeightOption(&po, parts[2:3]); err != nil {
  308. return "", po, err
  309. }
  310. if err = applyGravityOption(&po, parts[3:4]); err != nil {
  311. return "", po, err
  312. }
  313. if err = applyEnlargeOption(&po, parts[4:5]); err != nil {
  314. return "", po, err
  315. }
  316. url, imgType, err := decodeURL(parts[5:])
  317. if err != nil {
  318. return "", po, err
  319. }
  320. if err := applyFormatOption(&po, imgType); err != nil {
  321. return "", po, errors.New("Resulting image type not supported")
  322. }
  323. return string(url), po, nil
  324. }
  325. func parsePath(r *http.Request) (string, processingOptions, error) {
  326. path := r.URL.Path
  327. parts := strings.Split(strings.TrimPrefix(path, "/"), "/")
  328. if len(parts) < 3 {
  329. return "", processingOptions{}, errors.New("Invalid path")
  330. }
  331. if !conf.AllowInsecure {
  332. if err := validatePath(parts[0], strings.TrimPrefix(path, fmt.Sprintf("/%s", parts[0]))); err != nil {
  333. return "", processingOptions{}, err
  334. }
  335. }
  336. if _, ok := resizeTypes[parts[1]]; ok {
  337. return parsePathSimple(parts[1:])
  338. } else {
  339. return parsePathAdvanced(parts[1:])
  340. }
  341. }