Skip to content

Commit

Permalink
fix: downloadMediaMessage return type matched parameter (#757)
Browse files Browse the repository at this point in the history
* fix: downloadMediaMessage return type matched parameter

* chore: keyword-spacing eslint rule warn instead of error

* chore: linting

aaaaaaaaaaaaaaaaaaaa
  • Loading branch information
arthursimas1 committed May 15, 2024
1 parent 17ce9df commit 2ae664c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"selector": "TSEnumDeclaration",
"message": "Don't declare enums, use literals instead"
}
],
"keyword-spacing": [
"warn"
]
}
}
37 changes: 19 additions & 18 deletions src/Utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios from 'axios'
import { randomBytes } from 'crypto'
import { promises as fs } from 'fs'
import { Logger } from 'pino'
import { type Transform } from 'stream'
import { proto } from '../../WAProto'
import { MEDIA_KEYS, URL_REGEX, WA_DEFAULT_EPHEMERAL } from '../Defaults'
import {
Expand Down Expand Up @@ -868,31 +869,31 @@ const REUPLOAD_REQUIRED_STATUS = [410, 404]
/**
* Downloads the given message. Throws an error if it's not a media message
*/
export const downloadMediaMessage = async(
export const downloadMediaMessage = async<Type extends 'buffer' | 'stream'>(
message: WAMessage,
type: 'buffer' | 'stream',
type: Type,
options: MediaDownloadOptions,
ctx?: DownloadMediaMessageContext
) => {
try {
const result = await downloadMsg()
return result
} catch(error) {
if(ctx) {
if(axios.isAxiosError(error)) {
// check if the message requires a reupload
if(REUPLOAD_REQUIRED_STATUS.includes(error.response?.status!)) {
ctx.logger.info({ key: message.key }, 'sending reupload media request...')
// request reupload
message = await ctx.reuploadRequest(message)
const result = await downloadMsg()
return result
const result = await downloadMsg()
.catch(async(error) => {
if(ctx) {
if(axios.isAxiosError(error)) {
// check if the message requires a reupload
if(REUPLOAD_REQUIRED_STATUS.includes(error.response?.status!)) {
ctx.logger.info({ key: message.key }, 'sending reupload media request...')
// request reupload
message = await ctx.reuploadRequest(message)
const result = await downloadMsg()
return result
}
}
}
}

throw error
}
throw error
})

return result as Type extends 'buffer' ? Buffer : Transform

async function downloadMsg() {
const mContent = extractMessageContent(message.message)
Expand Down

0 comments on commit 2ae664c

Please sign in to comment.