mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
chore: fix format
This commit is contained in:
@@ -30,7 +30,7 @@
|
||||
"changelog:update": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0",
|
||||
"example": "node --inspect -r ts-node/register Example/example.ts",
|
||||
"gen:protobuf": "sh WAProto/GenerateStatics.sh",
|
||||
"format": "prettier --write \"src/**/*.{ts,js,json,md}\"",
|
||||
"format": "prettier --write \"src/**/*.{ts,js,json,md}\"",
|
||||
"lint": "eslint src --ext .js,.ts",
|
||||
"lint:fix": "yarn format && yarn lint --fix",
|
||||
"prepack": "tsc",
|
||||
@@ -66,6 +66,7 @@
|
||||
"json": "^11.0.0",
|
||||
"link-preview-js": "^3.0.0",
|
||||
"open": "^8.4.2",
|
||||
"prettier": "^3.5.3",
|
||||
"protobufjs-cli": "^1.1.3",
|
||||
"release-it": "^15.10.3",
|
||||
"sharp": "^0.32.6",
|
||||
|
||||
@@ -318,7 +318,7 @@ export const extractGroupMetadata = (result: BinaryNode) => {
|
||||
const memberAddMode = getBinaryNodeChildString(group, 'member_add_mode') === 'all_member_add'
|
||||
const metadata: GroupMetadata = {
|
||||
id: groupId,
|
||||
addressingMode: group.attrs.addressing_mode as "pn" | "lid",
|
||||
addressingMode: group.attrs.addressing_mode as 'pn' | 'lid',
|
||||
subject: group.attrs.subject,
|
||||
subjectOwner: group.attrs.s_o,
|
||||
subjectTime: +group.attrs.s_t,
|
||||
|
||||
@@ -634,10 +634,7 @@ export const makeMessagesRecvSocket = (config: SocketConfig) => {
|
||||
isLid ? authState.creds.me?.lid : authState.creds.me?.id
|
||||
)
|
||||
const remoteJid = !isNodeFromMe || isJidGroup(attrs.from) ? attrs.from : attrs.recipient
|
||||
const fromMe = !attrs.recipient || (
|
||||
(attrs.type === 'retry' || attrs.type === 'sender')
|
||||
&& isNodeFromMe
|
||||
)
|
||||
const fromMe = !attrs.recipient || ((attrs.type === 'retry' || attrs.type === 'sender') && isNodeFromMe)
|
||||
|
||||
const key: proto.IMessageKey = {
|
||||
remoteJid,
|
||||
|
||||
@@ -264,7 +264,10 @@ export type MessageGenerationOptionsFromContent = MiscMessageGenerationOptions &
|
||||
userJid: string
|
||||
}
|
||||
|
||||
export type WAMediaUploadFunction = (encFilePath: string, opts: { fileEncSha256B64: string, mediaType: MediaType, timeoutMs?: number }) => Promise<{ mediaUrl: string, directPath: string }>
|
||||
export type WAMediaUploadFunction = (
|
||||
encFilePath: string,
|
||||
opts: { fileEncSha256B64: string; mediaType: MediaType; timeoutMs?: number }
|
||||
) => Promise<{ mediaUrl: string; directPath: string }>
|
||||
|
||||
export type MediaGenerationOptions = {
|
||||
logger?: ILogger
|
||||
|
||||
@@ -3,7 +3,17 @@ import { createHash } from 'crypto'
|
||||
import { createWriteStream, promises as fs } from 'fs'
|
||||
import { tmpdir } from 'os'
|
||||
import { join } from 'path'
|
||||
import { CatalogCollection, CatalogStatus, OrderDetails, OrderProduct, Product, ProductCreate, ProductUpdate, WAMediaUpload, WAMediaUploadFunction } from '../Types'
|
||||
import {
|
||||
CatalogCollection,
|
||||
CatalogStatus,
|
||||
OrderDetails,
|
||||
OrderProduct,
|
||||
Product,
|
||||
ProductCreate,
|
||||
ProductUpdate,
|
||||
WAMediaUpload,
|
||||
WAMediaUploadFunction
|
||||
} from '../Types'
|
||||
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildren, getBinaryNodeChildString } from '../WABinary'
|
||||
import { generateMessageIDV2 } from './generics'
|
||||
import { getStream, getUrlFromDirectPath } from './messages-media'
|
||||
@@ -232,35 +242,29 @@ export const uploadingNecessaryImages = async (
|
||||
}
|
||||
}
|
||||
|
||||
const { stream } = await getStream(img)
|
||||
const hasher = createHash('sha256')
|
||||
const { stream } = await getStream(img)
|
||||
const hasher = createHash('sha256')
|
||||
|
||||
const filePath = join(tmpdir(), 'img' + generateMessageIDV2())
|
||||
const encFileWriteStream = createWriteStream(filePath)
|
||||
const filePath = join(tmpdir(), 'img' + generateMessageIDV2())
|
||||
const encFileWriteStream = createWriteStream(filePath)
|
||||
|
||||
for await (const block of stream) {
|
||||
hasher.update(block)
|
||||
encFileWriteStream.write(block)
|
||||
}
|
||||
for await (const block of stream) {
|
||||
hasher.update(block)
|
||||
encFileWriteStream.write(block)
|
||||
}
|
||||
|
||||
const sha = hasher.digest('base64')
|
||||
|
||||
const { directPath } = await waUploadToServer(
|
||||
filePath,
|
||||
{
|
||||
mediaType: 'product-catalog-image',
|
||||
fileEncSha256B64: sha,
|
||||
timeoutMs
|
||||
}
|
||||
)
|
||||
const { directPath } = await waUploadToServer(filePath, {
|
||||
mediaType: 'product-catalog-image',
|
||||
fileEncSha256B64: sha,
|
||||
timeoutMs
|
||||
})
|
||||
|
||||
await fs
|
||||
.unlink(filePath)
|
||||
.catch(err => console.log('Error deleting temp file ', err))
|
||||
await fs.unlink(filePath).catch(err => console.log('Error deleting temp file ', err))
|
||||
|
||||
return { url: getUrlFromDirectPath(directPath) }
|
||||
}
|
||||
)
|
||||
return { url: getUrlFromDirectPath(directPath) }
|
||||
})
|
||||
)
|
||||
return results
|
||||
}
|
||||
|
||||
@@ -11,7 +11,19 @@ import { Readable, Transform } from 'stream'
|
||||
import { URL } from 'url'
|
||||
import { proto } from '../../WAProto'
|
||||
import { DEFAULT_ORIGIN, MEDIA_HKDF_KEY_MAPPING, MEDIA_PATH_MAP } from '../Defaults'
|
||||
import { BaileysEventMap, DownloadableMessage, MediaConnInfo, MediaDecryptionKeyInfo, MediaType, MessageType, SocketConfig, WAGenericMediaMessage, WAMediaUpload, WAMediaUploadFunction, WAMessageContent } from '../Types'
|
||||
import {
|
||||
BaileysEventMap,
|
||||
DownloadableMessage,
|
||||
MediaConnInfo,
|
||||
MediaDecryptionKeyInfo,
|
||||
MediaType,
|
||||
MessageType,
|
||||
SocketConfig,
|
||||
WAGenericMediaMessage,
|
||||
WAMediaUpload,
|
||||
WAMediaUploadFunction,
|
||||
WAMessageContent
|
||||
} from '../Types'
|
||||
import { BinaryNode, getBinaryNodeChild, getBinaryNodeChildBuffer, jidNormalizedUser } from '../WABinary'
|
||||
import { aesDecryptGCM, aesEncryptGCM, hkdf } from './crypto'
|
||||
import { generateMessageIDV2 } from './generics'
|
||||
@@ -327,20 +339,14 @@ export const encryptedStream = async (
|
||||
const mediaKey = Crypto.randomBytes(32)
|
||||
const { cipherKey, iv, macKey } = await getMediaKeys(mediaKey, mediaType)
|
||||
|
||||
const encFilePath = join(
|
||||
getTmpFilesDirectory(),
|
||||
mediaType + generateMessageIDV2() + '-enc'
|
||||
)
|
||||
const encFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + '-enc')
|
||||
const encFileWriteStream = createWriteStream(encFilePath)
|
||||
|
||||
let originalFileStream: WriteStream | undefined
|
||||
let originalFilePath: string | undefined
|
||||
|
||||
if(saveOriginalFileIfRequired) {
|
||||
originalFilePath = join(
|
||||
getTmpFilesDirectory(),
|
||||
mediaType + generateMessageIDV2() + '-original'
|
||||
)
|
||||
if (saveOriginalFileIfRequired) {
|
||||
originalFilePath = join(getTmpFilesDirectory(), mediaType + generateMessageIDV2() + '-original')
|
||||
originalFileStream = createWriteStream(originalFilePath)
|
||||
}
|
||||
|
||||
@@ -366,8 +372,8 @@ export const encryptedStream = async (
|
||||
})
|
||||
}
|
||||
|
||||
if(originalFileStream) {
|
||||
if(!originalFileStream.write(data)) {
|
||||
if (originalFileStream) {
|
||||
if (!originalFileStream.write(data)) {
|
||||
await once(originalFileStream, 'drain')
|
||||
}
|
||||
}
|
||||
@@ -411,13 +417,12 @@ export const encryptedStream = async (
|
||||
sha256Enc.destroy()
|
||||
stream.destroy()
|
||||
|
||||
|
||||
try {
|
||||
await fs.unlink(encFilePath)
|
||||
if(originalFilePath) {
|
||||
if (originalFilePath) {
|
||||
await fs.unlink(originalFilePath)
|
||||
}
|
||||
} catch(err) {
|
||||
} catch (err) {
|
||||
logger?.error({ err }, 'failed deleting tmp files')
|
||||
}
|
||||
|
||||
@@ -572,7 +577,7 @@ export const getWAUploadToServer = (
|
||||
{ customUploadHosts, fetchAgent, logger, options }: SocketConfig,
|
||||
refreshMediaConn: (force: boolean) => Promise<MediaConnInfo>
|
||||
): WAMediaUploadFunction => {
|
||||
return async(filePath, { mediaType, fileEncSha256B64, timeoutMs }) => {
|
||||
return async (filePath, { mediaType, fileEncSha256B64, timeoutMs }) => {
|
||||
// send a query JSON to obtain the url & auth token to upload our media
|
||||
let uploadInfo = await refreshMediaConn(false)
|
||||
|
||||
@@ -589,25 +594,20 @@ export const getWAUploadToServer = (
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
let result: any
|
||||
try {
|
||||
|
||||
const body = await axios.post(
|
||||
url,
|
||||
createReadStream(filePath),
|
||||
{
|
||||
...options,
|
||||
maxRedirects: 0,
|
||||
headers: {
|
||||
...options.headers || { },
|
||||
'Content-Type': 'application/octet-stream',
|
||||
'Origin': DEFAULT_ORIGIN
|
||||
},
|
||||
httpsAgent: fetchAgent,
|
||||
timeout: timeoutMs,
|
||||
responseType: 'json',
|
||||
maxBodyLength: Infinity,
|
||||
maxContentLength: Infinity,
|
||||
}
|
||||
)
|
||||
const body = await axios.post(url, createReadStream(filePath), {
|
||||
...options,
|
||||
maxRedirects: 0,
|
||||
headers: {
|
||||
...(options.headers || {}),
|
||||
'Content-Type': 'application/octet-stream',
|
||||
Origin: DEFAULT_ORIGIN
|
||||
},
|
||||
httpsAgent: fetchAgent,
|
||||
timeout: timeoutMs,
|
||||
responseType: 'json',
|
||||
maxBodyLength: Infinity,
|
||||
maxContentLength: Infinity
|
||||
})
|
||||
result = body.data
|
||||
|
||||
if (result?.url || result?.directPath) {
|
||||
|
||||
@@ -165,39 +165,35 @@ export const prepareWAMessageMedia = async (message: AnyMediaMessageContent, opt
|
||||
const requiresWaveformProcessing = mediaType === 'audio' && uploadData.ptt === true
|
||||
const requiresAudioBackground = options.backgroundColor && mediaType === 'audio' && uploadData.ptt === true
|
||||
const requiresOriginalForSomeProcessing = requiresDurationComputation || requiresThumbnailComputation
|
||||
const {
|
||||
mediaKey,
|
||||
encFilePath,
|
||||
originalFilePath,
|
||||
fileEncSha256,
|
||||
fileSha256,
|
||||
fileLength
|
||||
} = await encryptedStream(
|
||||
const { mediaKey, encFilePath, originalFilePath, fileEncSha256, fileSha256, fileLength } = await encryptedStream(
|
||||
uploadData.media,
|
||||
options.mediaTypeOverride || mediaType,
|
||||
{
|
||||
logger,
|
||||
saveOriginalFileIfRequired: requiresOriginalForSomeProcessing,
|
||||
opts: options.options
|
||||
})
|
||||
}
|
||||
)
|
||||
// url safe Base64 encode the SHA256 hash of the body
|
||||
const fileEncSha256B64 = fileEncSha256.toString('base64')
|
||||
const [{ mediaUrl, directPath }] = await Promise.all([
|
||||
(async() => {
|
||||
const result = await options.upload(
|
||||
encFilePath,
|
||||
{ fileEncSha256B64, mediaType, timeoutMs: options.mediaUploadTimeoutMs }
|
||||
)
|
||||
(async () => {
|
||||
const result = await options.upload(encFilePath, {
|
||||
fileEncSha256B64,
|
||||
mediaType,
|
||||
timeoutMs: options.mediaUploadTimeoutMs
|
||||
})
|
||||
logger?.debug({ mediaType, cacheableKey }, 'uploaded media')
|
||||
return result
|
||||
})(),
|
||||
(async () => {
|
||||
try {
|
||||
if(requiresThumbnailComputation) {
|
||||
const {
|
||||
thumbnail,
|
||||
originalImageDimensions
|
||||
} = await generateThumbnail(originalFilePath!, mediaType as 'image' | 'video', options)
|
||||
if (requiresThumbnailComputation) {
|
||||
const { thumbnail, originalImageDimensions } = await generateThumbnail(
|
||||
originalFilePath!,
|
||||
mediaType as 'image' | 'video',
|
||||
options
|
||||
)
|
||||
uploadData.jpegThumbnail = thumbnail
|
||||
if (!uploadData.width && originalImageDimensions) {
|
||||
uploadData.width = originalImageDimensions.width
|
||||
@@ -208,12 +204,12 @@ export const prepareWAMessageMedia = async (message: AnyMediaMessageContent, opt
|
||||
logger?.debug('generated thumbnail')
|
||||
}
|
||||
|
||||
if(requiresDurationComputation) {
|
||||
if (requiresDurationComputation) {
|
||||
uploadData.seconds = await getAudioDuration(originalFilePath!)
|
||||
logger?.debug('computed audio duration')
|
||||
}
|
||||
|
||||
if(requiresWaveformProcessing) {
|
||||
if (requiresWaveformProcessing) {
|
||||
uploadData.waveform = await getAudioWaveform(originalFilePath!, logger)
|
||||
logger?.debug('processed waveform')
|
||||
}
|
||||
@@ -225,22 +221,19 @@ export const prepareWAMessageMedia = async (message: AnyMediaMessageContent, opt
|
||||
} catch (error) {
|
||||
logger?.warn({ trace: error.stack }, 'failed to obtain extra info')
|
||||
}
|
||||
})(),
|
||||
])
|
||||
.finally(
|
||||
async() => {
|
||||
try {
|
||||
await fs.unlink(encFilePath)
|
||||
if(originalFilePath) {
|
||||
await fs.unlink(originalFilePath)
|
||||
}
|
||||
|
||||
logger?.debug('removed tmp files')
|
||||
} catch(error) {
|
||||
logger?.warn('failed to remove tmp file')
|
||||
}
|
||||
})()
|
||||
]).finally(async () => {
|
||||
try {
|
||||
await fs.unlink(encFilePath)
|
||||
if (originalFilePath) {
|
||||
await fs.unlink(originalFilePath)
|
||||
}
|
||||
)
|
||||
|
||||
logger?.debug('removed tmp files')
|
||||
} catch (error) {
|
||||
logger?.warn('failed to remove tmp file')
|
||||
}
|
||||
})
|
||||
|
||||
const obj = WAProto.Message.fromObject({
|
||||
[`${mediaType}Message`]: MessageTypeProto[mediaType].fromObject({
|
||||
|
||||
Reference in New Issue
Block a user