apply.go 14 KB

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