Release v2.14.1
This commit is contained in:
1 parent
635e1a9942
commit
c3299abaac
174 files changed
+4483
-1687
No files matched your search
@@ -3,8 +3,13 @@ const assert = require('node:assert/strict');
|
||||
const fs = require('fs');
|
||||
const os = require('os');
|
||||
const path = require('path');
|
||||
const crypto = require('crypto');
|
||||
const sharp = require('sharp');
|
||||
const {
|
||||
normalizeMediaPath,
|
||||
createImageVariants,
|
||||
removeImageVariants,
|
||||
optimizeUploadedImage,
|
||||
registerMediaAssets,
|
||||
syncMediaAssetsFromDirectory,
|
||||
countMediaAssetReferences
|
||||
@@ -16,6 +21,53 @@ test('media library normalizes only media URLs', () => {
|
||||
assert.equal(normalizeMediaPath('/assets/photo.png'), null);
|
||||
});
|
||||
|
||||
test('media library optimizes large raster uploads in place', async () => {
|
||||
const uploadDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'pulse-media-optimize-'));
|
||||
const filePath = path.join(uploadDir, 'large.jpg');
|
||||
const sourceBuffer = await sharp(crypto.randomBytes(5000 * 100 * 3), {
|
||||
raw: { width: 5000, height: 100, channels: 3 }
|
||||
}).jpeg({ quality: 100 }).toBuffer();
|
||||
await fs.promises.writeFile(filePath, sourceBuffer);
|
||||
|
||||
try {
|
||||
const file = { path: filePath, size: sourceBuffer.length };
|
||||
await optimizeUploadedImage(file, { maxDimension: 8192 });
|
||||
const optimizedStats = await fs.promises.stat(filePath);
|
||||
|
||||
assert.equal(file.path, filePath);
|
||||
assert.equal(file.size, optimizedStats.size);
|
||||
assert.ok(optimizedStats.size < sourceBuffer.length);
|
||||
const metadata = await sharp(filePath).metadata();
|
||||
assert.equal(metadata.format, 'jpeg');
|
||||
assert.equal(metadata.width, 5000);
|
||||
} finally {
|
||||
await fs.promises.rm(uploadDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('media library creates only smaller image variants and preserves the original', async () => {
|
||||
const uploadDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), 'pulse-media-variants-'));
|
||||
const filePath = path.join(uploadDir, 'large.jpg');
|
||||
const sourceBuffer = await sharp(crypto.randomBytes(5000 * 100 * 3), {
|
||||
raw: { width: 5000, height: 100, channels: 3 }
|
||||
}).jpeg({ quality: 100 }).toBuffer();
|
||||
await fs.promises.writeFile(filePath, sourceBuffer);
|
||||
|
||||
try {
|
||||
const variants = await createImageVariants({ path: filePath });
|
||||
assert.deepEqual(variants.map(function (variant) { return variant.width; }), [2048, 4096]);
|
||||
assert.equal((await fs.promises.stat(filePath)).size, sourceBuffer.length);
|
||||
for (const variant of variants) {
|
||||
const variantPath = path.join(uploadDir, path.basename(variant.path));
|
||||
assert.ok((await fs.promises.stat(variantPath)).size < sourceBuffer.length);
|
||||
}
|
||||
await removeImageVariants(filePath);
|
||||
assert.equal((await fs.promises.readdir(uploadDir)).filter(function (name) { return name.includes('.variant-'); }).length, 0);
|
||||
} finally {
|
||||
await fs.promises.rm(uploadDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test('media library registers uploaded files with their metadata', async () => {
|
||||
const queries = [];
|
||||
const pool = {
|
||||
|
||||
Reference in new issue
Block a user