Transcode link preview images
This commit is contained in:
parent
fd610a6300
commit
40f16b98e2
2 changed files with 33 additions and 18 deletions
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
import type { RequestInit, Response } from 'node-fetch';
|
import type { RequestInit, Response } from 'node-fetch';
|
||||||
import type { AbortSignal as AbortSignalForNodeFetch } from 'abort-controller';
|
import type { AbortSignal as AbortSignalForNodeFetch } from 'abort-controller';
|
||||||
|
import { blobToArrayBuffer } from 'blob-util';
|
||||||
|
|
||||||
import type { MIMEType } from '../types/MIME';
|
import type { MIMEType } from '../types/MIME';
|
||||||
import {
|
import {
|
||||||
|
@ -14,6 +15,7 @@ import {
|
||||||
stringToMIMEType,
|
stringToMIMEType,
|
||||||
} from '../types/MIME';
|
} from '../types/MIME';
|
||||||
import type { LoggerType } from '../types/Logging';
|
import type { LoggerType } from '../types/Logging';
|
||||||
|
import { scaleImageToLevel } from '../util/scaleImageToLevel';
|
||||||
import * as log from '../logging/log';
|
import * as log from '../logging/log';
|
||||||
|
|
||||||
const USER_AGENT = 'WhatsApp/2';
|
const USER_AGENT = 'WhatsApp/2';
|
||||||
|
@ -603,5 +605,20 @@ export async function fetchLinkPreviewImage(
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Scale link preview image
|
||||||
|
if (contentType !== IMAGE_GIF) {
|
||||||
|
const dataBlob = new Blob([data], {
|
||||||
|
type: contentType,
|
||||||
|
});
|
||||||
|
const { blob: xcodedDataBlob } = await scaleImageToLevel(
|
||||||
|
dataBlob,
|
||||||
|
contentType,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
const xcodedDataArrayBuffer = await blobToArrayBuffer(xcodedDataBlob);
|
||||||
|
|
||||||
|
data = new Uint8Array(xcodedDataArrayBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
return { data, contentType };
|
return { data, contentType };
|
||||||
}
|
}
|
||||||
|
|
|
@ -1151,15 +1151,14 @@ describe('link preview fetching', () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
await fetchLinkPreviewImage(
|
(
|
||||||
fakeFetch,
|
await fetchLinkPreviewImage(
|
||||||
'https://example.com/img',
|
fakeFetch,
|
||||||
new AbortController().signal
|
'https://example.com/img',
|
||||||
),
|
new AbortController().signal
|
||||||
{
|
)
|
||||||
data: fixture,
|
)?.contentType,
|
||||||
contentType: stringToMIMEType(contentType),
|
stringToMIMEType(contentType)
|
||||||
}
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@ -1238,15 +1237,14 @@ describe('link preview fetching', () => {
|
||||||
);
|
);
|
||||||
|
|
||||||
assert.deepEqual(
|
assert.deepEqual(
|
||||||
await fetchLinkPreviewImage(
|
(
|
||||||
fakeFetch,
|
await fetchLinkPreviewImage(
|
||||||
'https://example.com/img',
|
fakeFetch,
|
||||||
new AbortController().signal
|
'https://example.com/img',
|
||||||
),
|
new AbortController().signal
|
||||||
{
|
)
|
||||||
data: fixture,
|
)?.contentType,
|
||||||
contentType: IMAGE_JPEG,
|
IMAGE_JPEG
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
sinon.assert.calledTwice(fakeFetch);
|
sinon.assert.calledTwice(fakeFetch);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue