media-helper
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -0,6 +1,9 @@ | ||
'use strict' | ||
const fs = require('fs') | ||
const mmm = require('mmmagic') | ||
const request = require('request').defaults({encoding: null}) | ||
const request = require('request').defaults({ encoding: null }) | ||
const Magic = mmm.Magic | ||
const typechecker = new Magic(mmm.MAGIC_MIME_TYPE) | ||
@@ -13,3 +16,3 @@ /** | ||
function isBase64 (str) { | ||
var base64Regex = /^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/ | ||
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) | ||
@@ -24,3 +27,3 @@ } | ||
function isURL (str) { | ||
var urlRegex = /^https?:\/\//i | ||
const urlRegex = /^https?:\/\//i | ||
return urlRegex.test(str) | ||
@@ -39,13 +42,34 @@ } | ||
/** | ||
* Determines if a file exists | ||
* @param {string} path - Path to a file | ||
* @returns {boolean} | ||
*/ | ||
function isBuffer (buffer) { | ||
return buffer instanceof Buffer | ||
} | ||
/** | ||
* Determines the mime-type of a file | ||
* @param {string} path - Path to a file | ||
* @param {string} media - either path, URL or base64 datas. | ||
* @returns {Promise} | ||
*/ | ||
function getMimeType (path) { | ||
function getMimeType (media) { | ||
return new Promise((resolve, reject) => { | ||
new Magic(mmm.MAGIC_MIME_TYPE) | ||
.detectFile(path, function (err, result) { | ||
if (err) { reject(err) } | ||
if (isFile(media)) { | ||
typechecker.detectFile(media, (err, result) => { | ||
err && reject(err) | ||
resolve(result) | ||
}) | ||
} else if (isBuffer(media)) { | ||
typechecker.detect(media, (err, result) => { | ||
err && reject(err) | ||
resolve(result) | ||
}) | ||
} else if (isBase64(media)) { | ||
return getMimeType(toBuffer(media)) | ||
} else if (isURL(media)) { | ||
return getMimeType(toBuffer(media)) | ||
} else { | ||
reject('media is not a file') | ||
} | ||
}) | ||
@@ -62,4 +86,4 @@ } | ||
getMimeType(path) | ||
.then(type => { | ||
if (type.indexOf('image') > -1) { resolve(true) } else { reject(false) } | ||
.then((type) => { | ||
;(type.indexOf('image') > -1) ? resolve(true) : resolve(false) | ||
}) | ||
@@ -78,4 +102,4 @@ .catch(err => reject(err)) | ||
getMimeType(path) | ||
.then(type => { | ||
if (type.indexOf('video') > -1) { resolve(true) } else { reject(false) } | ||
.then((type) => { | ||
;(type.indexOf('video') > -1) ? resolve(true) : resolve(false) | ||
}) | ||
@@ -94,3 +118,4 @@ .catch(err => reject(err)) | ||
request.get(url, (err, response, body) => { | ||
if (err) { reject(err) } else { resolve(body.toString('base64')) } | ||
err && reject(err) | ||
resolve(body.toString('base64')) | ||
}) | ||
@@ -107,6 +132,6 @@ }) | ||
return new Promise((resolve, reject) => { | ||
fs.readFile(path, {encoding: 'base64'}, | ||
(err, data) => { | ||
if (err) { reject(err) } else { resolve(data) } | ||
}) | ||
fs.readFile(path, { encoding: 'base64' }, (err, data) => { | ||
err && reject(err) | ||
resolve(data) | ||
}) | ||
}) | ||
@@ -130,16 +155,46 @@ } | ||
.catch(error => reject(error)) | ||
} else { reject('Error: toBase64(): argument must be url or file') } | ||
} else { | ||
reject('Error: toBase64(): argument must be url or file') | ||
} | ||
}) | ||
} | ||
/** | ||
* Reads an image from file or url and convert it to base64 | ||
* @param {media} media - file, url or path | ||
* @returns {Promise} | ||
*/ | ||
function toBuffer (media) { | ||
return new Promise((resolve, reject) => { | ||
if (isURL(media)) { | ||
return toBuffer(toBase64(media)) | ||
} else if (isBase64(media)) { | ||
try { | ||
resolve(Buffer.from(media)) | ||
} catch (ex) { | ||
reject(ex) | ||
} | ||
} else if (isFile(media)) { | ||
fs.readFile(media, (err, data) => { | ||
err && reject(err) | ||
resolve(data) | ||
}) | ||
} else { | ||
reject('Error: toBuffer(): argument must be file, url or file') | ||
} | ||
}) | ||
} | ||
module.exports = { | ||
isBase64: isBase64, | ||
isURL: isURL, | ||
isFile: isFile, | ||
fileToBase64: fileToBase64, | ||
urlToBase64: urlToBase64, | ||
toBase64: toBase64, | ||
isVideo: isVideo, | ||
isImage: isImage, | ||
getMimeType: getMimeType | ||
isBase64, | ||
isURL, | ||
isFile, | ||
isBuffer, | ||
fileToBase64, | ||
urlToBase64, | ||
toBase64, | ||
toBuffer, | ||
isVideo, | ||
isImage, | ||
getMimeType | ||
} |
{ | ||
"name": "media-helper", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A small module to help detecting and converting files through URL/file sytem", | ||
"main": "media-helper.js", | ||
"scripts": {}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/4quet/media-helper.git" | ||
"url": "git+https://github.com/soixantecircuits/media-helper.git" | ||
}, | ||
"scripts": { | ||
"test": "./node_modules/mocha/bin/mocha --reporter dot tests/*.js" | ||
}, | ||
"keywords": [ | ||
@@ -20,13 +22,17 @@ "file", | ||
], | ||
"author": "4quet", | ||
"license": "ISC", | ||
"author": "Soixante circuits", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/4quet/media-helper/issues" | ||
"url": "https://github.com/soixantecircuits/media-helper/issues" | ||
}, | ||
"homepage": "https://github.com/4quet/media-helper#readme", | ||
"homepage": "https://github.com/soixantecircuits/media-helper#readme", | ||
"dependencies": { | ||
"fs": "0.0.1-security", | ||
"mmmagic": "^0.4.5", | ||
"request": "^2.79.0" | ||
}, | ||
"devDependencies": { | ||
"chai": "^3.5.0", | ||
"chai-as-promised": "^6.0.0", | ||
"mocha": "^3.2.0" | ||
} | ||
} |
# media-helper | ||
## Installation | ||
`npm install media-helper` | ||
`npm install media-helper --save` ⚡️ `yarn add media-helper` | ||
## Usage | ||
``` | ||
var med = require('media-helper') | ||
var path = "/path/to/an/image/" | ||
var url = "http://somesite.com/img.jpg" | ||
```js | ||
const mh = require('media-helper') | ||
if (med.isFile(path) === true ) { | ||
console.log(path, " is an existing file !") | ||
const path = "/path/to/image.jpg" | ||
const url = "http://somesite.com/img.jpg" | ||
if (mh.isFile(path)) { | ||
console.log(path, " is an existing file !") | ||
mh.toBase64(path) | ||
.then((data) => { | ||
// data is the base64 datas for the file | ||
}) | ||
.catch((err) => { | ||
console.log(err) | ||
}) | ||
} | ||
if (med.isURL(url) === true) { | ||
console.log(url, " is an URL !") | ||
if (mh.isURL(url)) { | ||
console.log(url, " is an URL !") | ||
mh.toBase64(url) | ||
.then(data => { | ||
// data is the base64 datas for the image | ||
}) | ||
.catch((err) => { | ||
console.log(err) | ||
}) | ||
} | ||
var base64_path, base64_url | ||
// This function returns a Promise | ||
med.toBase64(path).then(data => { | ||
base64_path = data | ||
}) | ||
med.toBase64(url).then(data => { | ||
base64_url = data | ||
}) | ||
``` | ||
## API | ||
`isBase64 (str)`: Determines if a string is base64 encoded. Returns boolean. | ||
`isFile (path)`: Determines if a string describes a path to an existing file on your system. Returns boolean. | ||
`isURL (url)`: Determines if a string describes an URL. Returns boolean. | ||
`getMimeType (path)`: Determines the mime-type of a file on your system. Uses Promise. | ||
`isImage (path)`: Determines if a file is an image. Uses Promise. | ||
`isVideo (path)`: Determines if a file is a video. Uses Promise. | ||
`toBase64 (str)`: Converts an image to base64, the `str` parameter can describe either a path or an url. Uses Promise. | ||
`urlToBase64 (url)`: Converts an image to base64 through an URL. Uses Promise. | ||
`fileToBase64 (path)`: Converts an image on your system to base64. Uses Promise | ||
|method|parameters|returns|description| | ||
|:---|:---:|:---:|:---| | ||
|`isBase64`| `string` | `boolean` |Determines if a string is base64 encoded.| | ||
|`isFile`| `path` | `boolean` |Determines if a string describes a path to an existing file on your system.| | ||
|`isURL`| `url` | `boolean` |Determines if a string describes an HTTP URL.| | ||
|`isBuffer`| `Buffer` | `boolean` |Determines if an object is a `Buffer`.| | ||
|`getMimeType`| `path` | `Promise` |Determines the mime-type of a file on your system.| | ||
|`isImage`| `path` | `Promise` |Determines if a file is an image.| | ||
|`isVideo`| `path` | `Promise` |Determines if a file is a video.| | ||
|`toBuffer`| `string` | `Promise` |Converts an image to Buffer. `string` can describe either a path, base64 datas or an url.| | ||
|`toBase64`| `string` | `Promise` |Converts an image to base64. `string` can describe either a path or an url.| | ||
|`urlToBase64`| `url` | `Promise` |Converts an image to base64 through an URL.| | ||
|`fileToBase64`| `path` | `Promise` |Converts an image on your system to base64.| |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
38272
2
10
223
56
3
2
- Removedfs@0.0.1-security
- Removedfs@0.0.1-security(transitive)