liufumingren 993c7acd30 跨公司下载:云端中转成片(分片上传 + Range 下载)
A(查询机)和 B(仓库)不同网络、B 无公网 IP。B 出站领任务后把成片传回云端中转,
A 再从云端取回本地 —— 全程不需要任何一方有公网入口。

云端
- POST /api/web/edge/download      建任务(按主体选在线设备 → 下发 download)
- GET  /api/web/edge/download/:id  查进度(queued/uploading/done/failed)
- GET  /api/web/edge/download/:id/file  取成片,支持 Range(浏览器播放要拖动进度条)
- POST /api/scanner/tasks/:id/upload  设备分片上传(原始字节流,不是 base64)
- bodyParser 对该上传路由禁用:否则原始请求体会被解析掉
- 成片落 run/edge-files(run/ 属部署保留目录,不会被发布覆盖)
- 每次建任务顺手清理过期文件;元数据按主体校验,越权一律 404

桌面端
- AgentHost 新增 download 任务执行:worker 拉流合并 → 分片上传 → 传完删本地临时文件
- 失败时把 uploadTaskId 带回云端,立刻落 failed,查询机不用干等到超时
- ApiClient 新增建任务 / 查状态 / 取文件 / 分片上传
- PlaybackDownloadService 改为三级选路:
  LAN 设备管理服务 → 云端中转 → 本机直连

实测(真实 NVR)
  建任务 → 设备拉流 1.9MB → 上传 → 下载:7.5 秒,字节数一致,ftypisom
  Range: bytes=0-99 → HTTP 206 + Content-Range 正确

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-22 00:30:09 +08:00

90 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
const { Controller } = require('egg');
/**
* 设备态:边缘任务通道(**需长期设备 JWT**)
*
* 打包仓库的扫码服务没有公网 IP,只能自己出站来领任务:
* - `POST /api/scanner/tasks/poll?wait=25`:没有任务就挂住,有任务立刻返回;
* - `POST /api/scanner/tasks/:id/result`:回执行结果。
*
* 云端从不主动连设备 —— 这也正是跨公司场景能成立的原因。
*/
class DeviceEdgeController extends Controller {
/**
* 领任务(长轮询)
*
* 返回 204 表示这段时间没有任务,客户端立刻再发起下一次即可。
*/
async poll() {
const { ctx } = this;
const scanner = ctx.state.scanner;
// 上限 25 秒:比心跳间隔短,也能避开大多数反代的读超时(默认 60s)
const wait = Math.min(Math.max(Number(ctx.query.wait) || 25, 1), 25);
const task = await ctx.service.edgeTask.poll(scanner.id, wait * 1000);
if (!task) {
ctx.status = 204;
return;
}
ctx.logger.info('[edge] 设备 %s 领取任务 %s(%s)', scanner.sn, task.id, task.type);
ctx.body = { ok: true, data: task };
}
/**
* 回任务结果
*
* body: `{ ok: true, ... }` 或 `{ ok: false, error: '...' }`
*/
async result() {
const { ctx } = this;
const id = String(ctx.params.id || '').trim();
if (!id) ctx.throw(400, '缺少任务 id');
const body = ctx.request.body || {};
await ctx.service.edgeTask.submitResult(id, body);
// 跨网下载失败时,设备把 uploadTaskId 带回来 —— 立刻落 failed,
// 查询机不用干等到超时。
if (body.ok === false && body.uploadTaskId) {
await ctx.service.edgeTask.markFailed(String(body.uploadTaskId), body.error || '设备下载失败');
}
ctx.logger.info('[edge] 设备 %s 回任务 %s:ok=%s', ctx.state.scanner.sn, id, body.ok);
ctx.body = { ok: true };
}
/**
* 上传成片分片(跨网下载用)
*
* `POST /api/scanner/tasks/:id/upload?last=0|1`,body 是**原始字节**。
* 用原始流而不是 JSON/base64:几 MB 的分片做 base64 会膨胀 33%,没有意义。
* 该路由已在 config.bodyParser.ignore 里排除,所以这里直接读 `ctx.req`。
*
* ⚠️ 分片必须按顺序追加(设备侧是顺序读文件的),不校验 seq 是为了少一次往返。
*/
async upload() {
const { ctx } = this;
const id = String(ctx.params.id || '').trim();
if (!id) ctx.throw(400, '缺少任务 id');
const last = String(ctx.query.last || '0') === '1';
try {
const meta = await ctx.service.edgeTask.appendChunk(id, ctx.req, { last });
ctx.body = { ok: true, data: { size: meta.size, state: meta.state } };
} catch (err) {
ctx.logger.warn('[edge] 上传分片失败 %s:%s', id, err.message);
// 上传失败要落状态,否则查询机要白等两分钟
await ctx.service.edgeTask.markFailed(id, err.message);
ctx.throw(500, `上传失败:${err.message}`);
}
}
}
module.exports = DeviceEdgeController;