feat(publish): add shared media-image asset normalizer

Introduce a reusable helper that produces upload-ready blobs for
adapters that share the same image-asset semantics (dongchedi,
sohuhao, wangyihao, weixin-gzh, zol). Centralizes URL candidate
expansion and passthrough mime detection so each adapter focuses on
its own publish protocol.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-27 00:57:13 +08:00
parent 7b05974ca4
commit 34e54b9f78
2 changed files with 182 additions and 0 deletions
@@ -0,0 +1,44 @@
import { Buffer } from "node:buffer";
import { describe, expect, it, vi } from "vitest";
vi.mock("electron", () => ({
nativeImage: {
createFromBuffer: () => ({
isEmpty: () => false,
getSize: () => ({ width: 1, height: 1 }),
crop: () => ({
isEmpty: () => false,
getSize: () => ({ width: 1, height: 1 }),
toPNG: () => Buffer.from([]),
}),
toPNG: () => Buffer.from([]),
}),
},
}));
vi.mock("../transport/api-client", () => ({
resolveDesktopApiURL: (path: string) => `http://127.0.0.1:8080${path}`,
}));
import {
buildAssetURLCandidates,
getCenteredCropRect,
} from "./media-image";
describe("media image helpers", () => {
it("resolves API asset URLs through the desktop API base and requests png output", () => {
expect(buildAssetURLCandidates("/api/public/assets/12").at(0)).toBe(
"http://127.0.0.1:8080/api/public/assets/12?format=png",
);
});
it("centers crop rectangles at the requested ratio", () => {
const rect = getCenteredCropRect(1600, 1000, 4 / 3);
expect(rect.x).toBeCloseTo(133.333, 3);
expect(rect.y).toBeCloseTo(0);
expect(rect.width).toBeCloseTo(1333.333, 3);
expect(rect.height).toBeCloseTo(1000);
});
});