mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
Don't save original if not required for media + update readme
This commit is contained in:
@@ -2,7 +2,6 @@ import { MessageType, Mimetype, delay, promiseTimeout, WA_MESSAGE_STATUS_TYPE, g
|
||||
import { promises as fs } from 'fs'
|
||||
import * as assert from 'assert'
|
||||
import { WAConnectionTest, testJid, sendAndRetreiveMessage } from './Common'
|
||||
import { resolve } from 'path'
|
||||
|
||||
WAConnectionTest('Messages', conn => {
|
||||
|
||||
@@ -83,7 +82,7 @@ WAConnectionTest('Messages', conn => {
|
||||
})
|
||||
it('should send a jpeg image', async () => {
|
||||
const message = await sendAndRetreiveMessage(conn, { url: './Media/meme.jpeg' }, MessageType.image)
|
||||
assert.ok (message.message?.imageMessage?.jpegThumbnail)
|
||||
assert.ok(message.message.imageMessage.jpegThumbnail.length > 0)
|
||||
const msg = await conn.downloadMediaMessage(message)
|
||||
assert.deepStrictEqual(msg, await fs.readFile('./Media/meme.jpeg'))
|
||||
})
|
||||
|
||||
@@ -132,6 +132,9 @@ export class WAConnection extends Base {
|
||||
isGIF = true
|
||||
options.mimetype = MimetypeMap[MessageType.video]
|
||||
}
|
||||
const requiresDurationComputation = mediaType === MessageType.audio && !options.duration
|
||||
const requiresThumbnailComputation = (mediaType === MessageType.image || mediaType === MessageType.video) && !('thumbnail' in options)
|
||||
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation
|
||||
const {
|
||||
mediaKey,
|
||||
encBodyPath,
|
||||
@@ -139,7 +142,7 @@ export class WAConnection extends Base {
|
||||
fileEncSha256,
|
||||
fileSha256,
|
||||
fileLength
|
||||
} = await encryptedStream(media, mediaType)
|
||||
} = await encryptedStream(media, mediaType, requiresOriginalForSomeProcessing)
|
||||
// url safe Base64 encode the SHA256 hash of the body
|
||||
const fileEncSha256B64 = encodeURIComponent(
|
||||
fileEncSha256.toString('base64')
|
||||
@@ -147,8 +150,10 @@ export class WAConnection extends Base {
|
||||
.replace(/\//g, '_')
|
||||
.replace(/\=+$/, '')
|
||||
)
|
||||
await generateThumbnail(bodyPath, mediaType, options)
|
||||
if (mediaType === MessageType.audio && !options.duration) {
|
||||
if(requiresThumbnailComputation) {
|
||||
await generateThumbnail(bodyPath, mediaType, options)
|
||||
}
|
||||
if (requiresDurationComputation) {
|
||||
try {
|
||||
options.duration = await getAudioDuration(bodyPath)
|
||||
} catch (error) {
|
||||
@@ -176,7 +181,7 @@ export class WAConnection extends Base {
|
||||
|
||||
if (mediaUrl) break
|
||||
else {
|
||||
json = await this.refreshMediaConn (true)
|
||||
json = await this.refreshMediaConn(true)
|
||||
throw new Error (`upload failed, reason: ${JSON.stringify(result)}`)
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -187,7 +192,7 @@ export class WAConnection extends Base {
|
||||
if (!mediaUrl) throw new Error('Media upload failed on all hosts')
|
||||
|
||||
const message = {
|
||||
[mediaType]: MessageTypeProto[mediaType].fromObject (
|
||||
[mediaType]: MessageTypeProto[mediaType].fromObject(
|
||||
{
|
||||
url: mediaUrl,
|
||||
mediaKey: mediaKey,
|
||||
@@ -202,7 +207,7 @@ export class WAConnection extends Base {
|
||||
ptt: options.ptt
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
return WAMessageProto.Message.fromObject(message)// as WAMessageContent
|
||||
}
|
||||
/** prepares a WAMessage for sending from the given content & options */
|
||||
|
||||
@@ -310,7 +310,7 @@ export const getGotStream = async(url: string | URL, options: Options & { isStre
|
||||
})
|
||||
return fetched
|
||||
}
|
||||
export const encryptedStream = async(media: WAMediaUpload, mediaType: MessageType) => {
|
||||
export const encryptedStream = async(media: WAMediaUpload, mediaType: MessageType, saveOriginalFileIfRequired = true) => {
|
||||
const { stream, type } = await getStream(media)
|
||||
|
||||
const mediaKey = randomBytes(32)
|
||||
@@ -322,7 +322,7 @@ export const encryptedStream = async(media: WAMediaUpload, mediaType: MessageTyp
|
||||
let writeStream: WriteStream
|
||||
if(type === 'file') {
|
||||
bodyPath = (media as any).url
|
||||
} else {
|
||||
} else if(saveOriginalFileIfRequired) {
|
||||
bodyPath = join(tmpdir(), mediaType + generateMessageID())
|
||||
writeStream = createWriteStream(bodyPath)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user