Skip to content

Commit

Permalink
🐞📦 - update package dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielstuff committed Mar 19, 2022
1 parent 65c5ace commit 76101ad
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 153 deletions.
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v8.9.1
v16.13.1
87 changes: 52 additions & 35 deletions media-helper.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict'

const fs = require('fs')
const Buffer = require('buffer').Buffer
const mmm = require('mmmagic')
const mime = require('mime-types')
const request = require('request').defaults({ encoding: null })
const axios = require('axios').default

const Magic = mmm.Magic
const typechecker = new Magic(mmm.MAGIC_MIME_TYPE)
Expand All @@ -13,21 +15,22 @@ const typechecker = new Magic(mmm.MAGIC_MIME_TYPE)
* @param {string} str - The string to be tested
* @returns {boolean}
*/
function isBase64 (str) {
if (str!=null) {
const base64Regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/
function isBase64(str) {
if (str != null) {
const base64Regex =
/^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/
return base64Regex.test(str)
} else {
return false
}
}
}

/**
* Determines if a string is an URL
* @param {string} str - The string to be tested
* @returns {boolean}
*/
function isURL (str) {
function isURL(str) {
var matcher = /^(?:\w+:)?\/\/([^\s\.]+\.\S{2}|localhost[\:?\d]*)\S*$/
return matcher.test(encodeURI(str))
}
Expand All @@ -37,7 +40,7 @@ function isURL (str) {
* @param {string} path - Path to a file
* @returns {boolean}
*/
function isFile (path) {
function isFile(path) {
return fs.existsSync(path)
}

Expand All @@ -46,7 +49,7 @@ function isFile (path) {
* @param {string} path - Path to a file
* @returns {boolean}
*/
function isBuffer (buffer) {
function isBuffer(buffer) {
return buffer instanceof Buffer
}

Expand All @@ -55,7 +58,7 @@ function isBuffer (buffer) {
* @param {string} media - either path, URL or base64 datas.
* @returns {Promise}
*/
function getMimeType (media) {
function getMimeType(media) {
return new Promise((resolve, reject) => {
if (isFile(media)) {
typechecker.detectFile(media, (err, result) => {
Expand All @@ -82,7 +85,7 @@ function getMimeType (media) {
* @param {string} path - Path to a file
* @returns {string} or {false}
*/
function getMimeFromName (mediaPathName) {
function getMimeFromName(mediaPathName) {
return mime.lookup(mediaPathName)
}

Expand All @@ -91,13 +94,13 @@ function getMimeFromName (mediaPathName) {
* @param {string} path - Path to a file
* @returns {Promise}
*/
function isImage (path) {
function isImage(path) {
return new Promise((resolve, reject) => {
getMimeType(path)
.then((type) => {
;(type.indexOf('image') > -1) ? resolve(true) : resolve(false)
type.indexOf('image') > -1 ? resolve(true) : resolve(false)
})
.catch(err => reject(err))
.catch((err) => reject(err))
})
}

Expand All @@ -106,13 +109,13 @@ function isImage (path) {
* @param {string} path - Path to a file
* @returns {Promise}
*/
function isVideo (path) {
function isVideo(path) {
return new Promise((resolve, reject) => {
getMimeType(path)
.then((type) => {
;(type.indexOf('video') > -1) ? resolve(true) : resolve(false)
type.indexOf('video') > -1 ? resolve(true) : resolve(false)
})
.catch(err => reject(err))
.catch((err) => reject(err))
})
}

Expand All @@ -121,12 +124,26 @@ function isVideo (path) {
* @param {string} url
* @returns {Promise}
*/
function urlToBase64 (url) {
return new Promise((resolve, reject) => {
request.get(url, (err, response, body) => {
err && reject(err)
resolve(body.toString('base64'))
})
function urlToBase64(url) {
return new Promise(async (resolve, reject) => {
// let body = await got(url, { encoding: null })
axios
.get(url, { responseType: 'arraybuffer' })
.then(function (response) {
// handle success
//console.log(response)
resolve(Buffer.from(response.data, 'binary').toString('base64'))
})
.catch(function (error) {
// handle error
reject(error)
})
// request.get(url, (err, response, body) => {
// console.log(err)
// err && reject(err)
// resolve(body.toString('base64'))
// })
// resolve(body.toString('base64'))
})
}

Expand All @@ -135,7 +152,7 @@ function urlToBase64 (url) {
* @param {string} path - Path to a file
* @returns {Promise}
*/
function fileToBase64 (path) {
function fileToBase64(path) {
return new Promise((resolve, reject) => {
fs.readFile(path, { encoding: 'base64' }, (err, data) => {
err && reject(err)
Expand All @@ -149,18 +166,18 @@ function fileToBase64 (path) {
* @param {string} media - url or path
* @returns {Promise}
*/
function toBase64 (media) {
function toBase64(media) {
return new Promise((resolve, reject) => {
if (isBase64(media)) {
resolve(media)
} else if (isURL(media)) {
urlToBase64(media)
.then(data => resolve(data))
.catch(error => reject(error))
.then((data) => resolve(data))
.catch((error) => reject(error))
} else if (isFile(media)) {
fileToBase64(media)
.then(data => resolve(data))
.catch(error => reject(error))
.then((data) => resolve(data))
.catch((error) => reject(error))
} else if (isBuffer(media)) {
const base64 = media.toString('base64')
resolve(base64)
Expand All @@ -175,7 +192,7 @@ function toBase64 (media) {
* @param {media} media - dataURL string
* @returns {Promise}
*/
function trimDataURI (dataURL) {
function trimDataURI(dataURL) {
const dataUIRregex = /data:[a-zA-Z]+?\/[a-zA-Z]+?;base64,/gi
return dataURL.replace(dataUIRregex, '')
}
Expand All @@ -185,16 +202,16 @@ function trimDataURI (dataURL) {
* @param {media} media - file, url or path
* @returns {Promise}
*/
function toBuffer (media) {
function toBuffer(media) {
return new Promise((resolve, reject) => {
if (isURL(media)) {
toBase64(media)
.then(data => {
toBuffer(data)
.then(data => resolve(data))
.catch(error => reject(error))
})
.catch(error => reject(error))
.then((data) => {
toBuffer(data)
.then((data) => resolve(data))
.catch((error) => reject(error))
})
.catch((error) => reject(error))
} else if (isBase64(media)) {
try {
resolve(Buffer.from(media))
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "media-helper",
"version": "2.0.1",
"version": "2.0.2",
"description": "A small module to help detecting and converting files through URL/file sytem",
"main": "media-helper.js",
"repository": {
Expand All @@ -15,10 +15,10 @@
"media-helper.js"
],
"dependencies": {
"axios": "^0.26.1",
"file-type": "12.3.0",
"mime-types": "2.1.24",
"mmmagic": "0.5.3",
"request": "2.88.0"
"mime-types": "2.1.35",
"mmmagic": "0.5.3"
},
"keywords": [
"file",
Expand All @@ -39,11 +39,11 @@
},
"homepage": "https://github.com/soixantecircuits/media-helper#readme",
"devDependencies": {
"ava": "^0.17.0"
"ava": "^4.1.0"
},
"ava": {
"files": [
"tests/*.js"
"tests/*.mjs"
],
"source": [
"media-helper.js"
Expand Down
111 changes: 0 additions & 111 deletions tests/index.js

This file was deleted.

0 comments on commit 76101ad

Please sign in to comment.