forked from codigoencasa/builderbot
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(provider): ⚡ bailey add send file video audio
- Loading branch information
1 parent
e19c3a2
commit 14d1a61
Showing
4 changed files
with
71 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters