apply.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  1. package options
  2. import (
  3. "encoding/base64"
  4. "slices"
  5. "strconv"
  6. "time"
  7. "github.com/imgproxy/imgproxy/v3/imagetype"
  8. "github.com/imgproxy/imgproxy/v3/vips"
  9. log "github.com/sirupsen/logrus"
  10. )
  11. func applyWidthOption(po *ProcessingOptions, args []string) error {
  12. return parsePositiveInt(&po.Width, "width", args...)
  13. }
  14. func applyHeightOption(po *ProcessingOptions, args []string) error {
  15. return parsePositiveInt(&po.Height, "height", args...)
  16. }
  17. func applyMinWidthOption(po *ProcessingOptions, args []string) error {
  18. return parsePositiveInt(&po.MinWidth, "min width", args...)
  19. }
  20. func applyMinHeightOption(po *ProcessingOptions, args []string) error {
  21. return parsePositiveInt(&po.MinHeight, "min height", args...)
  22. }
  23. func applyEnlargeOption(po *ProcessingOptions, args []string) error {
  24. return parseBool(&po.Enlarge, "enlarge", args...)
  25. }
  26. func applyExtendOption(po *ProcessingOptions, args []string) error {
  27. return parseExtend(&po.Extend, "extend", args)
  28. }
  29. func applyExtendAspectRatioOption(po *ProcessingOptions, args []string) error {
  30. return parseExtend(&po.ExtendAspectRatio, "extend_aspect_ratio", args)
  31. }
  32. func applySizeOption(po *ProcessingOptions, args []string) (err error) {
  33. if err = ensureMaxArgs("size", args, 7); err != nil {
  34. return
  35. }
  36. if len(args) >= 1 && len(args[0]) > 0 {
  37. if err = applyWidthOption(po, args[0:1]); err != nil {
  38. return
  39. }
  40. }
  41. if len(args) >= 2 && len(args[1]) > 0 {
  42. if err = applyHeightOption(po, args[1:2]); err != nil {
  43. return
  44. }
  45. }
  46. if len(args) >= 3 && len(args[2]) > 0 {
  47. if err = applyEnlargeOption(po, args[2:3]); err != nil {
  48. return
  49. }
  50. }
  51. if len(args) >= 4 && len(args[3]) > 0 {
  52. if err = applyExtendOption(po, args[3:]); err != nil {
  53. return
  54. }
  55. }
  56. return nil
  57. }
  58. func applyResizingTypeOption(po *ProcessingOptions, args []string) error {
  59. if err := ensureMaxArgs("resizing type", args, 1); err != nil {
  60. return err
  61. }
  62. if r, ok := resizeTypes[args[0]]; ok {
  63. po.ResizingType = r
  64. } else {
  65. return newOptionArgumentError("Invalid resize type: %s", args[0])
  66. }
  67. return nil
  68. }
  69. func applyResizeOption(po *ProcessingOptions, args []string) error {
  70. if err := ensureMaxArgs("resize", args, 8); err != nil {
  71. return err
  72. }
  73. if len(args[0]) > 0 {
  74. if err := applyResizingTypeOption(po, args[0:1]); err != nil {
  75. return err
  76. }
  77. }
  78. if len(args) > 1 {
  79. if err := applySizeOption(po, args[1:]); err != nil {
  80. return err
  81. }
  82. }
  83. return nil
  84. }
  85. func applyZoomOption(po *ProcessingOptions, args []string) error {
  86. nArgs := len(args)
  87. if err := ensureMaxArgs("zoom", args, 2); err != nil {
  88. return err
  89. }
  90. var z float64
  91. if err := parsePositiveNonZeroFloat64(&z, "zoom", args[0]); err != nil {
  92. return err
  93. }
  94. po.ZoomWidth = z
  95. po.ZoomHeight = z
  96. if nArgs > 1 {
  97. if err := parsePositiveNonZeroFloat64(&po.ZoomHeight, "zoom height", args[1]); err != nil {
  98. return err
  99. }
  100. }
  101. return nil
  102. }
  103. func applyDprOption(po *ProcessingOptions, args []string) error {
  104. return parsePositiveNonZeroFloat64(&po.Dpr, "dpr", args...)
  105. }
  106. func applyGravityOption(po *ProcessingOptions, args []string) error {
  107. return parseGravity(&po.Gravity, "gravity", args, cropGravityTypes)
  108. }
  109. func applyCropOption(po *ProcessingOptions, args []string) error {
  110. if err := parsePositiveFloat64(&po.Crop.Width, "crop width", args[0]); err != nil {
  111. return err
  112. }
  113. if len(args) > 1 {
  114. if err := parsePositiveFloat64(&po.Crop.Height, "crop height", args[1]); err != nil {
  115. return err
  116. }
  117. }
  118. if len(args) > 2 {
  119. return parseGravity(&po.Crop.Gravity, "crop gravity", args[2:], cropGravityTypes)
  120. }
  121. return nil
  122. }
  123. func applyPaddingOption(po *ProcessingOptions, args []string) error {
  124. nArgs := len(args)
  125. if nArgs < 1 || nArgs > 4 {
  126. return newOptionArgumentError("Invalid padding arguments: %v", args)
  127. }
  128. po.Padding.Enabled = true
  129. if nArgs > 0 && len(args[0]) > 0 {
  130. if err := parsePositiveInt(&po.Padding.Top, "padding top (+all)", args[0]); err != nil {
  131. return err
  132. }
  133. po.Padding.Right = po.Padding.Top
  134. po.Padding.Bottom = po.Padding.Top
  135. po.Padding.Left = po.Padding.Top
  136. }
  137. if nArgs > 1 && len(args[1]) > 0 {
  138. if err := parsePositiveInt(&po.Padding.Right, "padding right (+left)", args[1]); err != nil {
  139. return err
  140. }
  141. po.Padding.Left = po.Padding.Right
  142. }
  143. if nArgs > 2 && len(args[2]) > 0 {
  144. if err := parsePositiveInt(&po.Padding.Bottom, "padding bottom", args[2]); err != nil {
  145. return err
  146. }
  147. }
  148. if nArgs > 3 && len(args[3]) > 0 {
  149. if err := parsePositiveInt(&po.Padding.Left, "padding left", args[3]); err != nil {
  150. return err
  151. }
  152. }
  153. if po.Padding.Top == 0 && po.Padding.Right == 0 && po.Padding.Bottom == 0 && po.Padding.Left == 0 {
  154. po.Padding.Enabled = false
  155. }
  156. return nil
  157. }
  158. func applyTrimOption(po *ProcessingOptions, args []string) error {
  159. if err := ensureMaxArgs("trim", args, 4); err != nil {
  160. return err
  161. }
  162. nArgs := len(args)
  163. if err := parseFloat64(&po.Trim.Threshold, "trim threshold", args[0]); err != nil {
  164. return err
  165. }
  166. po.Trim.Enabled = true
  167. if nArgs > 1 && len(args[1]) > 0 {
  168. if c, err := vips.ColorFromHex(args[1]); err == nil {
  169. po.Trim.Color = c
  170. po.Trim.Smart = false
  171. } else {
  172. return newOptionArgumentError("Invalid trim color: %s", args[1])
  173. }
  174. }
  175. if nArgs > 2 && len(args[2]) > 0 {
  176. if err := parseBool(&po.Trim.EqualHor, "trim equal horizontal", args[2]); err != nil {
  177. return err
  178. }
  179. }
  180. if nArgs > 3 && len(args[3]) > 0 {
  181. if err := parseBool(&po.Trim.EqualVer, "trim equal vertical", args[3]); err != nil {
  182. return err
  183. }
  184. }
  185. return nil
  186. }
  187. func applyRotateOption(po *ProcessingOptions, args []string) error {
  188. if err := parseInt(&po.Rotate, "rotate", args...); err != nil {
  189. return err
  190. }
  191. if po.Rotate%90 != 0 {
  192. return newOptionArgumentError("Rotation angle must be a multiple of 90")
  193. }
  194. return nil
  195. }
  196. func applyQualityOption(po *ProcessingOptions, args []string) error {
  197. return parseQualityInt(&po.Quality, "quality", args...)
  198. }
  199. func applyFormatQualityOption(po *ProcessingOptions, args []string) error {
  200. argsLen := len(args)
  201. if len(args)%2 != 0 {
  202. return newOptionArgumentError("Missing quality for: %s", args[argsLen-1])
  203. }
  204. for i := 0; i < argsLen; i += 2 {
  205. f, ok := imagetype.GetTypeByName(args[i])
  206. if !ok {
  207. return newOptionArgumentError("Invalid image format: %s", args[i])
  208. }
  209. var q int
  210. if err := parseQualityInt(&q, args[i]+" quality", args[i+1]); err != nil {
  211. return err
  212. }
  213. po.FormatQuality[f] = q
  214. }
  215. return nil
  216. }
  217. func applyMaxBytesOption(po *ProcessingOptions, args []string) error {
  218. return parsePositiveInt(&po.MaxBytes, "max_bytes", args...)
  219. }
  220. func applyBackgroundOption(po *ProcessingOptions, args []string) error {
  221. switch len(args) {
  222. case 1:
  223. if len(args[0]) == 0 {
  224. po.Flatten = false
  225. } else if c, err := vips.ColorFromHex(args[0]); err == nil {
  226. po.Flatten = true
  227. po.Background = c
  228. } else {
  229. return newOptionArgumentError("Invalid background argument: %s", err)
  230. }
  231. case 3:
  232. po.Flatten = true
  233. if r, err := strconv.ParseUint(args[0], 10, 8); err == nil && r <= 255 {
  234. po.Background.R = uint8(r)
  235. } else {
  236. return newOptionArgumentError("Invalid background red channel: %s", args[0])
  237. }
  238. if g, err := strconv.ParseUint(args[1], 10, 8); err == nil && g <= 255 {
  239. po.Background.G = uint8(g)
  240. } else {
  241. return newOptionArgumentError("Invalid background green channel: %s", args[1])
  242. }
  243. if b, err := strconv.ParseUint(args[2], 10, 8); err == nil && b <= 255 {
  244. po.Background.B = uint8(b)
  245. } else {
  246. return newOptionArgumentError("Invalid background blue channel: %s", args[2])
  247. }
  248. default:
  249. return newOptionArgumentError("Invalid background arguments: %v", args)
  250. }
  251. return nil
  252. }
  253. func applyBlurOption(po *ProcessingOptions, args []string) error {
  254. return parsePositiveNonZeroFloat32(&po.Blur, "blur", args...)
  255. }
  256. func applySharpenOption(po *ProcessingOptions, args []string) error {
  257. return parsePositiveNonZeroFloat32(&po.Sharpen, "sharpen", args...)
  258. }
  259. func applyPixelateOption(po *ProcessingOptions, args []string) error {
  260. return parsePositiveInt(&po.Pixelate, "pixelate", args...)
  261. }
  262. func applyWatermarkOption(po *ProcessingOptions, args []string) error {
  263. if err := ensureMaxArgs("watermark", args, 7); err != nil {
  264. return err
  265. }
  266. if o, err := strconv.ParseFloat(args[0], 64); err == nil && o >= 0 && o <= 1 {
  267. po.Watermark.Enabled = o > 0
  268. po.Watermark.Opacity = o
  269. } else {
  270. return newOptionArgumentError("Invalid watermark opacity: %s", args[0])
  271. }
  272. if len(args) > 1 && len(args[1]) > 0 {
  273. if g, ok := gravityTypes[args[1]]; ok && slices.Contains(watermarkGravityTypes, g) {
  274. po.Watermark.Position.Type = g
  275. } else {
  276. return newOptionArgumentError("Invalid watermark position: %s", args[1])
  277. }
  278. }
  279. if len(args) > 2 && len(args[2]) > 0 {
  280. if err := parseFloat64(&po.Watermark.Position.X, "watermark X offset", args[2]); err != nil {
  281. return err
  282. }
  283. }
  284. if len(args) > 3 && len(args[3]) > 0 {
  285. if err := parseFloat64(&po.Watermark.Position.Y, "watermark Y offset", args[3]); err != nil {
  286. return err
  287. }
  288. }
  289. if len(args) > 4 && len(args[4]) > 0 {
  290. if err := parsePositiveNonZeroFloat64(&po.Watermark.Scale, "watermark scale", args[4]); err == nil {
  291. return err
  292. }
  293. }
  294. return nil
  295. }
  296. func applyFormatOption(po *ProcessingOptions, args []string) error {
  297. if err := ensureMaxArgs("format", args, 1); err != nil {
  298. return err
  299. }
  300. if f, ok := imagetype.GetTypeByName(args[0]); ok {
  301. po.Format = f
  302. } else {
  303. return newOptionArgumentError("Invalid image format: %s", args[0])
  304. }
  305. return nil
  306. }
  307. func applyCacheBusterOption(po *ProcessingOptions, args []string) error {
  308. if err := ensureMaxArgs("cache buster", args, 1); err != nil {
  309. return err
  310. }
  311. po.CacheBuster = args[0]
  312. return nil
  313. }
  314. func applySkipProcessingFormatsOption(po *ProcessingOptions, args []string) error {
  315. for _, format := range args {
  316. if f, ok := imagetype.GetTypeByName(format); ok {
  317. po.SkipProcessingFormats = append(po.SkipProcessingFormats, f)
  318. } else {
  319. return newOptionArgumentError("Invalid image format in skip processing: %s", format)
  320. }
  321. }
  322. return nil
  323. }
  324. func applyRawOption(po *ProcessingOptions, args []string) error {
  325. return parseBool(&po.Raw, "raw", args...)
  326. }
  327. func applyFilenameOption(po *ProcessingOptions, args []string) error {
  328. if err := ensureMaxArgs("filename", args, 2); err != nil {
  329. return err
  330. }
  331. po.Filename = args[0]
  332. if len(args) == 1 {
  333. return nil
  334. }
  335. var b bool
  336. if err := parseBool(&b, "filename is base64", args[1]); err != nil || !b {
  337. return err
  338. }
  339. decoded, err := base64.RawURLEncoding.DecodeString(po.Filename)
  340. if err != nil {
  341. return newOptionArgumentError("Invalid filename encoding: %s", err)
  342. }
  343. po.Filename = string(decoded)
  344. return nil
  345. }
  346. func applyExpiresOption(po *ProcessingOptions, args []string) error {
  347. if err := ensureMaxArgs("expires", args, 1); err != nil {
  348. return err
  349. }
  350. timestamp, err := strconv.ParseInt(args[0], 10, 64)
  351. if err != nil {
  352. return newOptionArgumentError("Invalid expires argument: %v", args[0])
  353. }
  354. if timestamp > 0 && timestamp < time.Now().Unix() {
  355. return newOptionArgumentError("Expired URL")
  356. }
  357. expires := time.Unix(timestamp, 0)
  358. po.Expires = &expires
  359. return nil
  360. }
  361. func applyStripMetadataOption(po *ProcessingOptions, args []string) error {
  362. return parseBool(&po.StripMetadata, "strip metadata", args...)
  363. }
  364. func applyKeepCopyrightOption(po *ProcessingOptions, args []string) error {
  365. return parseBool(&po.KeepCopyright, "keep copyright", args...)
  366. }
  367. func applyStripColorProfileOption(po *ProcessingOptions, args []string) error {
  368. return parseBool(&po.StripColorProfile, "strip color profile", args...)
  369. }
  370. func applyAutoRotateOption(po *ProcessingOptions, args []string) error {
  371. return parseBool(&po.AutoRotate, "auto rotate", args...)
  372. }
  373. func applyEnforceThumbnailOption(po *ProcessingOptions, args []string) error {
  374. return parseBool(&po.EnforceThumbnail, "enforce thumbnail", args...)
  375. }
  376. func applyReturnAttachmentOption(po *ProcessingOptions, args []string) error {
  377. return parseBool(&po.ReturnAttachment, "return_attachment", args...)
  378. }
  379. func applyMaxSrcResolutionOption(po *ProcessingOptions, args []string) error {
  380. if err := po.isSecurityOptionsAllowed(); err != nil {
  381. return err
  382. }
  383. var v float64
  384. if err := parsePositiveNonZeroFloat64(&v, "max_src_resolution", args...); err != nil {
  385. return err
  386. }
  387. po.SecurityOptions.MaxSrcResolution = int(v * 1000000)
  388. return nil
  389. }
  390. func applyMaxSrcFileSizeOption(po *ProcessingOptions, args []string) error {
  391. if err := po.isSecurityOptionsAllowed(); err != nil {
  392. return err
  393. }
  394. return parseInt(&po.SecurityOptions.MaxSrcFileSize, "max_src_file_size", args...)
  395. }
  396. func applyMaxAnimationFramesOption(po *ProcessingOptions, args []string) error {
  397. if err := po.isSecurityOptionsAllowed(); err != nil {
  398. return err
  399. }
  400. return parsePositiveNonZeroInt(&po.SecurityOptions.MaxAnimationFrames, "max_animation_frames", args...)
  401. }
  402. func applyMaxAnimationFrameResolutionOption(po *ProcessingOptions, args []string) error {
  403. if err := po.isSecurityOptionsAllowed(); err != nil {
  404. return err
  405. }
  406. var v float64
  407. if err := parseFloat64(&v, "max_animation_frame_resolution", args...); err != nil {
  408. return err
  409. }
  410. po.SecurityOptions.MaxAnimationFrameResolution = int(v * 1000000)
  411. return nil
  412. }
  413. func applyMaxResultDimensionOption(po *ProcessingOptions, args []string) error {
  414. if err := po.isSecurityOptionsAllowed(); err != nil {
  415. return err
  416. }
  417. return parseInt(&po.SecurityOptions.MaxResultDimension, "max_result_dimension", args...)
  418. }
  419. func applyPresetOption(f *Factory, po *ProcessingOptions, args []string, usedPresets ...string) error {
  420. for _, preset := range args {
  421. if p, ok := f.presets[preset]; ok {
  422. if slices.Contains(usedPresets, preset) {
  423. log.Warningf("Recursive preset usage is detected: %s", preset)
  424. continue
  425. }
  426. po.UsedPresets = append(po.UsedPresets, preset)
  427. if err := f.applyURLOptions(po, p, true, append(usedPresets, preset)...); err != nil {
  428. return err
  429. }
  430. } else {
  431. return newOptionArgumentError("Unknown preset: %s", preset)
  432. }
  433. }
  434. return nil
  435. }