feat(edit): 编辑页上报ozon 开发
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { createHash } from 'node:crypto';
|
||||
import { test } from 'node:test';
|
||||
import sharp from 'sharp';
|
||||
import {
|
||||
qiniuPrivateDownloadUrl,
|
||||
prepareOzonImages,
|
||||
} from '../src/ozon-publish-media.ts';
|
||||
|
||||
const config = {
|
||||
provider: 'qiniu_kodo',
|
||||
bucket: 'test-bucket',
|
||||
downloadOrigin: 'https://images.example.test',
|
||||
accessKey: 'MY_ACCESS_KEY',
|
||||
secretKey: 'MY_SECRET_KEY',
|
||||
ttlSeconds: 86400,
|
||||
};
|
||||
test('七牛签名包含秒级有效期,HMAC-SHA1 与独立计算向量一致', () => {
|
||||
assert.equal(
|
||||
qiniuPrivateDownloadUrl(
|
||||
config.downloadOrigin,
|
||||
'resource/flower.jpg',
|
||||
1451491200,
|
||||
config.accessKey,
|
||||
config.secretKey,
|
||||
),
|
||||
'https://images.example.test/resource/flower.jpg?e=1451491200&token=MY_ACCESS_KEY:qUjF3pEnCC9cpm3MaHVikOTEevE=',
|
||||
);
|
||||
assert.throws(
|
||||
() =>
|
||||
qiniuPrivateDownloadUrl(
|
||||
'http://localhost:9000',
|
||||
'a',
|
||||
1451491200,
|
||||
'ak',
|
||||
'sk',
|
||||
),
|
||||
/HTTPS/,
|
||||
);
|
||||
});
|
||||
test('发布 WebP 按 checksum 固定源,转 JPEG 并交付独立发布路径', async () => {
|
||||
const bytes = await sharp({
|
||||
create: {
|
||||
width: 20,
|
||||
height: 20,
|
||||
channels: 4,
|
||||
background: { r: 100, g: 80, b: 60, alpha: 1 },
|
||||
},
|
||||
})
|
||||
.webp()
|
||||
.toBuffer();
|
||||
const checksum = createHash('sha256').update(bytes).digest('hex');
|
||||
const writes = [];
|
||||
const storage = {
|
||||
send: async (command) => {
|
||||
if (command.constructor.name === 'GetObjectCommand')
|
||||
return {
|
||||
Body: (async function* () {
|
||||
yield bytes;
|
||||
})(),
|
||||
};
|
||||
writes.push(command.input);
|
||||
return {};
|
||||
},
|
||||
};
|
||||
const snapshot = {
|
||||
media: [
|
||||
{
|
||||
asset_id: 'asset-a',
|
||||
checksum_sha256: checksum,
|
||||
bucket: 'test-bucket',
|
||||
object_key: 'private/original',
|
||||
storage_provider: 'qiniu_kodo',
|
||||
content_type: 'image/webp',
|
||||
},
|
||||
],
|
||||
};
|
||||
const urls = await prepareOzonImages(
|
||||
storage,
|
||||
snapshot,
|
||||
'workspace-a',
|
||||
'job-a',
|
||||
config,
|
||||
);
|
||||
assert.equal(writes.length, 1);
|
||||
assert.equal(writes[0].ContentType, 'image/jpeg');
|
||||
assert.equal((await sharp(writes[0].Body).metadata()).format, 'jpeg');
|
||||
assert.ok(writes[0].Key.startsWith('publishing/workspace-a/job-a/'));
|
||||
assert.ok(!urls[0].includes('private/original'));
|
||||
assert.ok(new URL(urls[0]).searchParams.get('token'));
|
||||
snapshot.media[0].checksum_sha256 = 'changed';
|
||||
await assert.rejects(
|
||||
prepareOzonImages(storage, snapshot, 'workspace-a', 'job-a', config),
|
||||
/图片内容已变更/,
|
||||
);
|
||||
assert.equal(writes.length, 1);
|
||||
});
|
||||
Reference in New Issue
Block a user