imgcli\recipe.mbt

100% · 30 / 30 points · 85 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

  1. 1///|
  2. 2/// # sanitize Recipe
  3. 3///
  4. 4/// 一次调用解决"这张图能发给别人吗":
  5. 5/// 1. 元数据:解码后重编码,EXIF/GPS/XMP/ICC/内嵌缩略图都不会进入新容器;
  6. 6/// 2. 已知区域打码(用户给坐标,不猜测人脸):块均值替换,保留构图但不可读;
  7. 7/// 3. 保守模糊:整图 box blur,用于"宁可不可用也别泄露"的场合。
  8. 8///
  9. 9/// 三档 policy:
  10. 10/// - `metadata`:只做 1;
  11. 11/// - `standard`:1 + 2(给了区域才打码,没给就退化成 metadata);
  12. 12/// - `strict`:1 + 2 + 3。
  13. 13
  14. 14///|
  15. 15pub struct Region {
  16. 16 x : Int32
  17. 17 y : Int32
  18. 18 w : Int32
  19. 19 h : Int32
  20. 20}
  21. 21
  22. 22///|
  23. 23pub fn Region::to_string(self : Region) -> String {
  24. 24● "\{self.x},\{self.y},\{self.w},\{self.h}"
  25. 25}
  26. 26
  27. 27///|
  28. 28/// 解析 `"x,y,w,h"`;非法输入返回 None,由调用方决定报错还是忽略。
  29. 29pub fn parse_region(s : String) -> Region? {
  30. 30● let parts = s.split(",")
  31. 31 let nums : Array[Int32] = []
  32. 32 for p in parts {
  33. 33● let t = p.trim()
  34. 34● if t.length() == 0 {
  35. 35● return None
  36. 36 }
  37. 37● let v = @string.parse_int(t) catch { _ => return None }
  38. 38● nums.push(v)
  39. 39 }
  40. 40● if nums.length() != 4 {
  41. 41● return None
  42. 42 }
  43. 43 Some({ x: nums[0], y: nums[1], w: nums[2], h: nums[3], })
  44. 44}
  45. 45
  46. 46///|
  47. 47/// 解析 `"x,y,w,h;x,y,w,h"` 这种批量写法。
  48. 48pub fn parse_regions(s : String) -> Array[Region] raise @types.ImageError {
  49. 49● let out : Array[Region] = []
  50. 50● for seg in s.split(";") {
  51. 51● let t = seg.trim()
  52. 52● if t.length() == 0 {
  53. 53● continue
  54. 54 }
  55. 55● match parse_region(t.to_owned()) {
  56. 56● Some(r) => out.push(r)
  57. 57 None =>
  58. 58● raise @types.ImageError::Process(
  59. 59 @types.ErrorCode::InvalidArgument,
  60. 60 "区域格式应为 x,y,w,h(收到: \{t})",
  61. 61 )
  62. 62 }
  63. 63 }
  64. 64 out
  65. 65}
  66. 66
  67. 67///|
  68. 68/// 元数据签名:出现在输出容器里就说明没剥干净。
  69. 69fn metadata_signatures() -> Array[String] {
  70. 70● [
  71. 71 "Exif", "GPS", "XMP", "ICC_PROFILE", "Photoshop 3.0", "<x:xmpmeta", "Thumbnail",
  72. 72 ]
  73. 73}
  74. 74
  75. 75///|
  76. 76/// 返回命中的签名(空数组 = 干净)。
  77. 77pub fn metadata_hits(data : Bytes) -> Array[String] {
  78. 78● let hits : Array[String] = []
  79. 79● for sig in metadata_signatures() {
  80. 80● if contains_ascii(data, sig) {
  81. 81● hits.push(sig)
  82. 82 }
  83. 83 }
  84. 84 hits
  85. 85}