Skip to content

Commit

Permalink
feat(provider): ⚡ bailey add send file video audio
Browse files Browse the repository at this point in the history
  • Loading branch information
leifermendez committed Jan 24, 2023
1 parent e19c3a2 commit 14d1a61
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 72 deletions.
65 changes: 65 additions & 0 deletions packages/provider/common/download.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const mimeDep = require('mime-types')
const { tmpdir } = require('os')
const http = require('http')
const https = require('https')
const { rename, createWriteStream } = require('fs')

/**
* Extrar el mimetype from buffer
* @param {string} response
* @returns
*/
const fileTypeFromFile = async (response) => {
const type = response.headers['content-type'] ?? null
const ext = mimeDep.extension(type)
return {
type,
ext,
}
}

/**
* Descargar archivo binay en tmp
* @param {*} url
* @returns
*/
const generalDownload = async (url) => {
const handleDownload = () => {
const checkProtocol = url.includes('https:')
const handleHttp = checkProtocol ? https : http
const name = `tmp-${Date.now()}-dat`
const fullPath = `${tmpdir()}/${name}`
const file = createWriteStream(fullPath)

return new Promise((res, rej) => {
handleHttp.get(url, function (response) {
response.pipe(file)
file.on('finish', async function () {
file.close()
res({ response, fullPath })
})
file.on('error', function () {
file.close()
rej(null)
})
})
})
}

const handleFile = (pathInput, ext) =>
new Promise((resolve, reject) => {
const fullPath = `${pathInput}.${ext}`
rename(pathInput, fullPath, (err) => {
if (err) reject(null)
resolve(fullPath)
})
})

const httpResponse = await handleDownload()
const { ext } = await fileTypeFromFile(httpResponse.response)
const getPath = await handleFile(httpResponse.fullPath, ext)

return getPath
}

module.exports = { generalDownload }
16 changes: 0 additions & 16 deletions packages/provider/common/fileType.js

This file was deleted.

9 changes: 5 additions & 4 deletions packages/provider/src/baileys/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const pino = require('pino')
const rimraf = require('rimraf')
const mime = require('mime-types')
const { join } = require('path')
const { existsSync, createWriteStream, readFileSync } = require('fs')
const { createWriteStream, readFileSync } = require('fs')
const { Console } = require('console')

const {
Expand All @@ -18,9 +18,10 @@ const {
baileyGenerateImage,
baileyCleanNumber,
baileyIsValidNumber,
baileyDownloadMedia,
} = require('./utils')

const { generalDownload } = require('../../common/download')

const logger = new Console({
stdout: createWriteStream(`${process.cwd()}/baileys.log`),
})
Expand Down Expand Up @@ -170,7 +171,7 @@ class BaileysProvider extends ProviderClass {
*/

sendMedia = async (number, imageUrl, text) => {
const fileDownloaded = await baileyDownloadMedia(imageUrl)
const fileDownloaded = await generalDownload(imageUrl)
const mimeType = mime.lookup(fileDownloaded)

if (mimeType.includes('image'))
Expand All @@ -180,7 +181,7 @@ class BaileysProvider extends ProviderClass {
if (mimeType.includes('audio'))
return this.sendAudio(number, fileDownloaded, text)

return this.sendFile()
return this.sendFile(number, fileDownloaded)
}

/**
Expand Down
53 changes: 1 addition & 52 deletions packages/provider/src/baileys/utils.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
const { createWriteStream, rename } = require('fs')
const { createWriteStream } = require('fs')
const combineImage = require('combine-image')
const qr = require('qr-image')
const { tmpdir } = require('os')
const http = require('http')
const https = require('https')

const { fileTypeFromFile } = require('../../common/fileType')

const baileyCleanNumber = (number, full = false) => {
number = number.replace('@s.whatsapp.net', '')
Expand Down Expand Up @@ -43,54 +38,8 @@ const baileyIsValidNumber = (rawNumber) => {
return !exist
}

/**
* Incompleta
* Descargar archivo multimedia para enviar
* @param {*} url
* @returns
*/
const baileyDownloadMedia = async (url) => {
const handleDownload = () => {
const checkProtocol = url.includes('https:')
const handleHttp = checkProtocol ? https : http
const name = `tmp-${Date.now()}-dat`
const fullPath = `${tmpdir()}/${name}`
const file = createWriteStream(fullPath)

return new Promise((res, rej) => {
handleHttp.get(url, function (response) {
response.pipe(file)
file.on('finish', async function () {
file.close()
res({ response, fullPath })
})
file.on('error', function () {
file.close()
rej(null)
})
})
})
}

const handleFile = (pathInput, ext) =>
new Promise((resolve, reject) => {
const fullPath = `${pathInput}.${ext}`
rename(pathInput, fullPath, (err) => {
if (err) reject(null)
resolve(fullPath)
})
})

const httpResponse = await handleDownload()
const { ext } = await fileTypeFromFile(httpResponse.response)
const getPath = await handleFile(httpResponse.fullPath, ext)

return getPath
}

module.exports = {
baileyCleanNumber,
baileyGenerateImage,
baileyIsValidNumber,
baileyDownloadMedia,
}

0 comments on commit 14d1a61

Please sign in to comment.