mirror of
https://github.com/cheveguerra/botLeiferAurik-Mod_2.0.git
synced 2026-04-19 20:29:23 +00:00
Inicial
This commit is contained in:
65
provider/download.js
Normal file
65
provider/download.js
Normal file
@@ -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 }
|
||||
Reference in New Issue
Block a user