magicpen-media
Advanced tools
Comparing version 1.3.1 to 1.4.0
/*global URL, Blob, btoa*/ | ||
var _ = require('lodash'); | ||
var _ = require('lodash'), | ||
fs = require('fs'), | ||
pathModule = require('path'), | ||
mime = require('mime'), | ||
getTemporaryFilePath = require('gettemporaryfilepath'); | ||
@@ -28,3 +32,11 @@ function sanitizeContentType(contentType) { | ||
var title = options.title; | ||
var fallbackToDisc = options.fallbackToDisc; | ||
if (typeof media === 'string' && !/data:/.test(media) && (typeof contentType === 'undefined' || contentType.indexOf('/') === -1)) { | ||
var extension = pathModule.extname(media); | ||
if (extension) { | ||
contentType = mime.types[extension.replace(/^\./, '')] || contentType; | ||
} | ||
} | ||
this.alt({ | ||
@@ -38,4 +50,6 @@ html: { | ||
src = media; | ||
if (typeof title === 'undefined' && !/^data:/.test(media)) { | ||
title = media; | ||
if (!/^data:/.test(media)) { | ||
if (typeof title === 'undefined') { | ||
title = media; | ||
} | ||
} | ||
@@ -84,15 +98,36 @@ } else if (typeof URL !== 'undefined' && typeof URL.createObjectURL === 'function' && typeof Blob !== 'undefined') { | ||
fallback: function () { | ||
var text; | ||
function writeToDisc(data) { | ||
if (data.constructor.name === 'Uint8Array') { | ||
data = new Buffer(data); | ||
} | ||
var fileName; | ||
if (typeof fallbackToDisc === 'string') { | ||
fileName = fallbackToDisc; | ||
} else { | ||
fileName = getTemporaryFilePath({ suffix: '.' + (mime.extensions[contentType] || majorContentType || 'data') }); | ||
} | ||
fs.writeFileSync(fileName, data); | ||
return fileName; | ||
} | ||
if (typeof media === 'string') { | ||
var matchDataUrl = media.match(/^data:([\w\-\+\.]+\/[\w\-\+\.]+)?(?:;charset=([\w\/\-]+))?(;base64)?,([\u0000-\u007f]*)$/); | ||
if (matchDataUrl) { | ||
text = 'data:' + matchDataUrl[1] || 'text/plain'; | ||
contentType = matchDataUrl[1] || contentType || 'text/plain'; | ||
if (matchDataUrl[3] && options.fallbackToDisc && fs.writeFileSync) { | ||
this.text(writeToDisc(new Buffer(matchDataUrl[4], 'base64')) + ' (' + contentType + ')'); | ||
} else { | ||
this.text('data url (' + contentType + ')'); | ||
} | ||
} else { | ||
// path or non-data: url | ||
text = media; | ||
this.text(media + ' (' + (contentType || 'media') + ')'); | ||
} | ||
this.text((matchDataUrl ? matchDataUrl[1] || 'text/plain' : media) + ' (' + (contentType || 'media') + ')'); | ||
} else { | ||
// Uint8Array or Buffer | ||
this.text(media.constructor.name + '[' + media.length + '] (' + (contentType || 'media') + ')'); | ||
if (options.fallbackToDisc && fs.writeFileSync) { | ||
this.text(writeToDisc(media) + ' (' + contentType + ')'); | ||
} else { | ||
this.text(media.constructor.name + '[' + media.length + '] (' + (contentType || 'media') + ')'); | ||
} | ||
} | ||
@@ -99,0 +134,0 @@ } |
{ | ||
"name": "magicpen-media", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"description": "Add media support to magicpen (images, audio, video)", | ||
@@ -21,2 +21,3 @@ "repository": { | ||
"unexpected": "9.0.0", | ||
"unexpected-fs": "1.1.0", | ||
"unexpected-sinon": "6.4.1" | ||
@@ -36,4 +37,6 @@ }, | ||
"dependencies": { | ||
"lodash": "3.10.0" | ||
"gettemporaryfilepath": "0.0.1", | ||
"lodash": "3.10.0", | ||
"mime": "1.3.4" | ||
} | ||
} |
Sorry, the diff of this file is too big to display
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
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
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
518172
13907
3
11
6
+ Addedgettemporaryfilepath@0.0.1
+ Addedmime@1.3.4
+ Addedgettemporaryfilepath@0.0.1(transitive)
+ Addedmime@1.3.4(transitive)