processing_options.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 decodeURL(parts []string) (string, imageType, error) {
  70. var imgType imageType = imageTypeJPEG
  71. urlParts := strings.Split(strings.Join(parts, ""), ".")
  72. if len(urlParts) > 2 {
  73. return "", 0, errors.New("Invalid url encoding")
  74. }
  75. if len(urlParts) == 2 {
  76. if f, ok := imageTypes[urlParts[1]]; ok {
  77. imgType = f
  78. } else {
  79. return "", 0, fmt.Errorf("Invalid image format: %s", urlParts[1])
  80. }
  81. }
  82. url, err := base64.RawURLEncoding.DecodeString(urlParts[0])
  83. if err != nil {
  84. return "", 0, errors.New("Invalid url encoding")
  85. }
  86. return string(url), imgType, nil
  87. }
  88. func applyWidthOption(po *processingOptions, args []string) error {
  89. if len(args) > 1 {
  90. return fmt.Errorf("Invalid width arguments: %v", args)
  91. }
  92. if w, err := strconv.Atoi(args[0]); err == nil && w >= 0 {
  93. po.Width = w
  94. } else {
  95. return fmt.Errorf("Invalid width: %s", args[0])
  96. }
  97. return nil
  98. }
  99. func applyHeightOption(po *processingOptions, args []string) error {
  100. if len(args) > 1 {
  101. return fmt.Errorf("Invalid height arguments: %v", args)
  102. }
  103. if h, err := strconv.Atoi(args[0]); err == nil && po.Height >= 0 {
  104. po.Height = h
  105. } else {
  106. return fmt.Errorf("Invalid height: %s", args[0])
  107. }
  108. return nil
  109. }
  110. func applyEnlargeOption(po *processingOptions, args []string) error {
  111. if len(args) > 1 {
  112. return fmt.Errorf("Invalid enlarge arguments: %v", args)
  113. }
  114. po.Enlarge = args[0] != "0"
  115. return nil
  116. }
  117. func applySizeOption(po *processingOptions, args []string) (err error) {
  118. if len(args) > 3 {
  119. return fmt.Errorf("Invalid size arguments: %v", args)
  120. }
  121. if len(args) >= 1 {
  122. if err = applyWidthOption(po, args[0:1]); err != nil {
  123. return
  124. }
  125. }
  126. if len(args) >= 2 {
  127. if err = applyHeightOption(po, args[1:2]); err != nil {
  128. return
  129. }
  130. }
  131. if len(args) == 3 {
  132. if err = applyEnlargeOption(po, args[2:3]); err != nil {
  133. return
  134. }
  135. }
  136. return nil
  137. }
  138. func applyResizeOption(po *processingOptions, args []string) error {
  139. if len(args) > 4 {
  140. return fmt.Errorf("Invalid resize arguments: %v", args)
  141. }
  142. if r, ok := resizeTypes[args[0]]; ok {
  143. po.Resize = r
  144. } else {
  145. return fmt.Errorf("Invalid resize type: %s", args[0])
  146. }
  147. if len(args) > 1 {
  148. if err := applySizeOption(po, args[1:]); err != nil {
  149. return err
  150. }
  151. }
  152. return nil
  153. }
  154. func applyGravityOption(po *processingOptions, args []string) error {
  155. if len(args) > 1 {
  156. return fmt.Errorf("Invalid resize arguments: %v", args)
  157. }
  158. if g, ok := gravityTypes[args[0]]; ok {
  159. po.Gravity = g
  160. } else {
  161. return fmt.Errorf("Invalid gravity: %s", args[0])
  162. }
  163. return nil
  164. }
  165. func applyBlurOption(po *processingOptions, args []string) error {
  166. if len(args) > 1 {
  167. return fmt.Errorf("Invalid blur arguments: %v", args)
  168. }
  169. if b, err := strconv.ParseFloat(args[0], 32); err == nil || b >= 0 {
  170. po.Blur = float32(b)
  171. } else {
  172. return fmt.Errorf("Invalid blur: %s", args[0])
  173. }
  174. return nil
  175. }
  176. func applySharpenOption(po *processingOptions, args []string) error {
  177. if len(args) > 1 {
  178. return fmt.Errorf("Invalid sharpen arguments: %v", args)
  179. }
  180. if s, err := strconv.ParseFloat(args[0], 32); err == nil || s >= 0 {
  181. po.Sharpen = float32(s)
  182. } else {
  183. return fmt.Errorf("Invalid sharpen: %s", args[0])
  184. }
  185. return nil
  186. }
  187. func applyPresetOption(po *processingOptions, args []string) error {
  188. for _, preset := range args {
  189. if p, ok := conf.Presets[preset]; ok {
  190. for name, pargs := range p {
  191. if err := applyProcessingOption(po, name, pargs); err != nil {
  192. return err
  193. }
  194. }
  195. } else {
  196. return fmt.Errorf("Unknown asset: %s", preset)
  197. }
  198. }
  199. return nil
  200. }
  201. func applyFormatOption(po *processingOptions, imgType imageType) error {
  202. if !vipsTypeSupportSave[imgType] {
  203. return errors.New("Resulting image type not supported")
  204. }
  205. po.Format = imgType
  206. return nil
  207. }
  208. func applyProcessingOption(po *processingOptions, name string, args []string) error {
  209. switch name {
  210. case "resize":
  211. if err := applyResizeOption(po, args); err != nil {
  212. return err
  213. }
  214. case "size":
  215. if err := applySizeOption(po, args); err != nil {
  216. return err
  217. }
  218. case "width":
  219. if err := applyWidthOption(po, args); err != nil {
  220. return err
  221. }
  222. case "height":
  223. if err := applyHeightOption(po, args); err != nil {
  224. return err
  225. }
  226. case "enlarge":
  227. if err := applyEnlargeOption(po, args); err != nil {
  228. return err
  229. }
  230. case "gravity":
  231. if err := applyGravityOption(po, args); err != nil {
  232. return err
  233. }
  234. case "blur":
  235. if err := applyBlurOption(po, args); err != nil {
  236. return err
  237. }
  238. case "sharpen":
  239. if err := applySharpenOption(po, args); err != nil {
  240. return err
  241. }
  242. case "preset":
  243. if err := applyPresetOption(po, args); err != nil {
  244. return err
  245. }
  246. }
  247. return nil
  248. }
  249. func parseURLOptions(opts []string) (urlOptions, []string) {
  250. parsed := make(urlOptions)
  251. urlStart := len(opts) + 1
  252. for i, opt := range opts {
  253. args := strings.Split(opt, ":")
  254. if len(args) == 1 {
  255. urlStart = i
  256. break
  257. }
  258. parsed[args[0]] = args[1:]
  259. }
  260. var rest []string
  261. if urlStart < len(opts) {
  262. rest = opts[urlStart:]
  263. } else {
  264. rest = []string{}
  265. }
  266. return parsed, rest
  267. }
  268. func defaultProcessingOptions() (processingOptions, error) {
  269. var err error
  270. po := processingOptions{
  271. Resize: resizeFit,
  272. Width: 0,
  273. Height: 0,
  274. Gravity: gravityCenter,
  275. Enlarge: false,
  276. Format: imageTypeJPEG,
  277. Blur: 0,
  278. Sharpen: 0,
  279. }
  280. if _, ok := conf.Presets["default"]; ok {
  281. err = applyPresetOption(&po, []string{"default"})
  282. }
  283. return po, err
  284. }
  285. func parsePathAdvanced(parts []string) (string, processingOptions, error) {
  286. po, err := defaultProcessingOptions()
  287. if err != nil {
  288. return "", po, err
  289. }
  290. options, urlParts := parseURLOptions(parts)
  291. for name, args := range options {
  292. if err := applyProcessingOption(&po, name, args); err != nil {
  293. return "", po, err
  294. }
  295. }
  296. url, imgType, err := decodeURL(urlParts)
  297. if err != nil {
  298. return "", po, err
  299. }
  300. if err := applyFormatOption(&po, imgType); err != nil {
  301. return "", po, errors.New("Resulting image type not supported")
  302. }
  303. return string(url), po, nil
  304. }
  305. func parsePathSimple(parts []string) (string, processingOptions, error) {
  306. var err error
  307. if len(parts) < 6 {
  308. return "", processingOptions{}, errors.New("Invalid path")
  309. }
  310. po, err := defaultProcessingOptions()
  311. if err != nil {
  312. return "", po, err
  313. }
  314. po.Resize = resizeTypes[parts[0]]
  315. if err = applyWidthOption(&po, parts[1:2]); err != nil {
  316. return "", po, err
  317. }
  318. if err = applyHeightOption(&po, parts[2:3]); err != nil {
  319. return "", po, err
  320. }
  321. if err = applyGravityOption(&po, parts[3:4]); err != nil {
  322. return "", po, err
  323. }
  324. if err = applyEnlargeOption(&po, parts[4:5]); err != nil {
  325. return "", po, err
  326. }
  327. url, imgType, err := decodeURL(parts[5:])
  328. if err != nil {
  329. return "", po, err
  330. }
  331. if err := applyFormatOption(&po, imgType); err != nil {
  332. return "", po, errors.New("Resulting image type not supported")
  333. }
  334. return string(url), po, nil
  335. }
  336. func parsePath(r *http.Request) (string, processingOptions, error) {
  337. path := r.URL.Path
  338. parts := strings.Split(strings.TrimPrefix(path, "/"), "/")
  339. if len(parts) < 3 {
  340. return "", processingOptions{}, errors.New("Invalid path")
  341. }
  342. if !conf.AllowInsecure {
  343. if err := validatePath(parts[0], strings.TrimPrefix(path, fmt.Sprintf("/%s", parts[0]))); err != nil {
  344. return "", processingOptions{}, err
  345. }
  346. }
  347. if _, ok := resizeTypes[parts[1]]; ok {
  348. return parsePathSimple(parts[1:])
  349. } else {
  350. return parsePathAdvanced(parts[1:])
  351. }
  352. }