Open
fix:Optimizing the stability of rotating HTTP downloads and reusing the same URL in network diagrams.add jest and update example.version:2.2.1-beta.2 #30
fix:Optimizing the stability of rotating HTTP downloads and reusing the same URL in network diagrams.add jest and update example.version:2.2.1-beta.2 #30
Open
yanPengAdd on 20 hours ago
yanPeng
yanPeng
20 hours ago

变更类型

请在对应类型(请勿多选)前打 √,仅可选择一项:

变更内容

修复问题点

1.网络图旋转不稳定
远程图片每次旋转都重新 http.request,未固定 GET / ARRAY_BUFFER / HTTP1.1;含特殊字符的 URL 可能请求失败。

2.同 URL 重复下载
对同一网络地址多次 rotateImage 会反复拉取整图,耗时长、易失败。

3.PixelMap 未释放
createPixelMap 后主流程未 release(),连续旋转易抬高内存。

4.边界与资源处理不严谨
本地文件用 READ_WRITE 打开,只读 URI 可能失败
用 includes('http') 判断远程,路径含 http 的本地文件会被误判
本地解码失败只打日志返回 undefined,错误信息降级
每次 rotateImage 都清理临时目录,可能误删刚生成文件
入口缺少回调 / 角度有限性校验;模块销毁时无缓存与临时文件清理
5.工程配套不足
缺少 Jest;example 仅有鸿蒙 testerino,未覆盖上游 demo。

解决方案

1.增加HTTP 稳定性,normalizeRemoteRequestUrl;请求指定 GET + ARRAY_BUFFER + HTTP1.1
2.同 URL 复用,静态 LRU(上限 4)缓存远程 ArrayBuffer
3.内存,旋转/打包后 finally 中 pixelMap.release()
4.边界处理:READ_ONLY;^https?😕/ 判定远程;解码失败抛 Cannot decode bitmap;入口校验回调与 Number.isFinite(angle)
5.生命周期:去掉每次调用时的 cleanDirectory;增加 onDestroy 清缓存并清理临时文件;构造清理加 try-catch
6.工程增加 jest/;example 双入口(上游 demo + 鸿蒙 demo);

改动前后代码块

(1)网络下载:规范化 URL + LRU 缓存

// 改动前:每次直连请求,无缓存、无请求选项
const data: http.HttpResponse = await httpRequest.request(src);

// 改动后:规范化 URL + 指定请求参数 + LRU 复用
let imageData = RNImageRotateTurboModule.remoteImageBodyCache.get(src);
if (imageData === undefined) {
  const requestUrl = RNImageRotateTurboModule.normalizeRemoteRequestUrl(src);
  const data = await httpRequest.request(requestUrl, {
    method: http.RequestMethod.GET,
    expectDataType: http.HttpDataType.ARRAY_BUFFER,
    usingProtocol: http.HttpProtocol.HTTP1_1,
  });
  // ... 成功后 cacheRemoteImageBody(src, imageData)
}

(2)PixelMap 统一释放

// 改动前:rotate → pack,无 release
imagePixelMap.rotate(...).then(() => {
  rotateTask.doInBackgroundGuarded(imagePixelMap);
});

// 改动后:try/finally 全路径释放
try {
  await imagePixelMap.rotate(rotateTask.mAngle);
  await rotateTask.doInBackgroundGuarded(imagePixelMap);
} finally {
  await imagePixelMap.release();
}

(3)远程判定 / 本地打开 / 错误上抛

// 改动前
if (muri.includes('http')) { ... }
file = fs.openSync(src, fs.OpenMode.READ_WRITE);
// catch 仅 console,返回 undefined

// 改动后
if (RNImageRotateTurboModule.isRemoteUri(muri)) { ... } // /^https?:\/\//i
file = fs.openSync(src, fs.OpenMode.READ_ONLY);
throw new Error('Cannot decode bitmap: ' + src);

影响评估

  • 是否影响公开接口功能
    • 不影响。 仍为 ImageRotate.rotateImage(uri, angle, success, failure);成功仍回 file://…/ReactNative_rotated_image_*;失败仍走 failure / 空 URI 仍抛 Please specify a URI。
  • 是否产生兼容问题
    • 对正常业务调用无破坏性兼容问题。 行为对齐/增强:只读本地 URI 更稳、远程判定更准、同 URL 更快、内存更稳。可能感知差异:① 非法回调 / 非有限角度会显式报错(原先可能二次抛错);② 本地解码失败信息更接近 Cannot decode bitmap;③ 临时文件改为构造/销毁时清理,而非每次调用清理(更合理,一般不影响业务)。
  • 建议验证test
    • 网络图连续旋转同 URL;本地只读路径;非法 URI / 空 URI;±90° / 45°;观察内存是否平稳。

测试步骤

cd example & npm i & npm run dev
运行工程

selected-area.png

测试情况

合入前自检

likedislike
Merge blocked
cpf-managercpf-managerCollaborators
20 hours ago Added label: AI检视中
openharmony_ci
openharmony_ciCollaborators
20 hours ago Comments:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
openharmony_ciopenharmony_ciCollaborators
20 hours ago Added label: dco检查成功
cpf-manager
cpf-managerCollaborators20 hours agoStart code review2
example/src/harmonyTestDemo.tsx
@@ -0,0 +18,4 @@
18+ 
19+import ImageRotate from "react-native-image-rotate";
20+ 
21+const SOURCE_IMAGE = 'https://filesamples.com/samples/image/jpg/sample_640×426.jpg';
cpf-manager
cpf-manager20 hours agoComments:

【AI-Review】【一般】【基础代码问题】【字符串问题】
● 问题: SOURCE_IMAGE 常量的 URL 为 https://filesamples.com/samples/image/jpg/sample_640×426.jpg,其中 640 与 426 之间的分隔符是 Unicode 乘号 ×(U+00D7,UTF-8 编码 C3 97),不是 ASCII 字母 x。该 URL 会被传入原生模块的 getPixelMapFromURL,经 normalizeRemoteRequestUrl 处理后,encodeURI(decodeURI(src)) 会将 × 编码为 %C3%97,最终 HTTP 请求目标变为 sample_640%C3%97426.jpg。如果服务器上实际文件名使用的是 ASCII x(即 sample_640x426.jpg),请求会返回 404。

● 影响: 鸿蒙 Demo 中三个主要旋转测试用例(rotateImage_cw、rotateImage_ccw、rotateImage_angle_45)都使用 SOURCE_IMAGE 作为输入。如果 URL 因字符问题导致 404,这三个测试用例的 assert 会因 uri 未设置或包含 __failure__ 而失败,Demo 无法正常验证旋转功能。

● 建议: 将 ×(U+00D7)改为 ASCII x(U+0078),即 sample_640x426.jpg。修改后在浏览器或 curl 中验证该 URL 可正常返回图片,再提交。

likedislike
yanPeng
yanPeng
6 hours ago Comments:

非问题,const SOURCE_IMAGE = 'https://filesamples.com/samples/image/jpg/sample_640×426.jpg'; 这是正常访问的https网址,测试用图不替换

likedislike
cpf-manager
cpf-managerCollaborators20 hours agoStart code review3
example/src/harmonyTestDemo.tsx
Outdated
@@ -0,0 +80,4 @@
80+ <TestSuite name = "imageRotateTest">
81+ <Text>{""}</Text>
82+ 
83+ <TextInput>
cpf-manager
cpf-manager20 hours agoComments:

【AI-Review】【一般】【基础代码问题】【代码逻辑错误】
● 问题: harmonyTestDemo.tsx 第 83-85 行的 <TextInput>{}</TextInput> 没有设置 value、onChangeText 等任何属性,且 JSX 表达式容器中的 {} 在标准 TSX 解析中等价于空对象字面量。React 无法将普通对象渲染为子节点,会抛出 "Objects are not valid as a React child" 错误。

● 影响: Demo 页面渲染到该 TextInput 时可能触发运行时错误,导致整个鸿蒙 Demo 页面白屏或崩溃,无法进行后续测试操作。

● 建议: 删除第 81-85 行的空 <Text>{""}</Text> 和 <TextInput>{}</TextInput> 元素。如果此处原本计划放置输入框用于自定义 URI 输入,请补充完整的 value/onChangeText 逻辑;否则直接移除。

likedislike
System
System MessageSystem
6 hours ago Comments:

changed this line on 9676ccb9 view diff detail

likedislike
yanPeng
yanPeng
6 hours ago Comments:

已修改

likedislike
cpf-manager
cpf-managerCollaborators20 hours agoStart code review3
example/src/harmonyTestDemo.tsx
Outdated
@@ -0,0 +50,4 @@
50+ this.rotate = this.rotate.bind(this);
51+ }
52+ 
53+ rotate(angle) {
cpf-manager
cpf-manager20 hours agoComments:

【AI-Review】【建议】【软件设计】【冗余重复代码】
● 问题: harmonyTestDemo.tsx 第 50 行在构造函数中执行了 this.rotate = this.rotate.bind(this),第 53-68 行定义了 rotate(angle) 方法。但 rotate 方法在 render() 和所有 TestCase 的 arrange 回调中均未被调用——每个 TestCase 都直接调用 ImageRotate.rotateImage(...),没有通过 this.rotate。

● 影响: 该方法及其 bind 绑定属于死代码,增加了 Demo 的阅读成本。维护者可能误以为 rotate 是主要入口逻辑,实际运行路径绕过了它。

● 建议: 删除第 50 行的 this.rotate = this.rotate.bind(this) 和第 53-68 行的 rotate 方法。如果后续需要复用,再按需添加。

likedislike
System
System MessageSystem
6 hours ago Comments:

changed this line on 9676ccb9 view diff detail

likedislike
yanPeng
yanPeng
6 hours ago Comments:

已修改

likedislike
cpf-managercpf-managerCollaborators
20 hours ago Removed label: AI检视中
cpf-managercpf-managerCollaborators
20 hours ago Added label: AI已检视
yanPengyanPeng
6 hours ago Force-pushed 1 commits:9676ccb9-fix:Optimizing the stability of rotating HTTP downloads and reusing the same URL in network diagrams.add jest and update example.version:2.2.1-beta.2
cpf-managercpf-managerCollaborators
6 hours ago Removed label: AI已检视
cpf-managercpf-managerCollaborators
6 hours ago Added label: AI检视中
openharmony_ci
openharmony_ciCollaborators
6 hours ago Comments:

感谢提交 Pull Requests!
Thanks for submitting a pull request.

likedislike
cpf-managercpf-managerCollaborators
6 hours ago Removed label: AI检视中
cpf-managercpf-managerCollaborators
6 hours ago Added label: AI已检视
yanPengyanPeng
6 hours ago Modify description of pull request