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