@c8/simple-promised-file
Advanced tools
Comparing version 0.0.4 to 0.0.5
'use strict' | ||
const path = require('path') | ||
const image = require('../lib/image') | ||
const Image = require('../lib/image') | ||
image(path.join(__dirname, '1.jpg')) | ||
Image(path.join(__dirname, '1.jpg')) | ||
.then((img) => { | ||
// We still have inherited File getters available | ||
console.log(img.size) | ||
console.log(img.mime) | ||
img.mime | ||
.then((mime) => { | ||
console.log(`Mime: ${mime}`) | ||
}) | ||
.catch((err) => { | ||
console.log(err) | ||
}) | ||
img.isImage | ||
.then((result) => { | ||
console.log(`IsImage: ${result}`) | ||
}) | ||
.catch((err) => { | ||
console.log(err) | ||
}) | ||
// Async getters | ||
@@ -13,0 +27,0 @@ img |
@@ -5,2 +5,3 @@ 'use strict' | ||
const mime = require('mime') | ||
const createError = require('create-error') | ||
@@ -40,4 +41,18 @@ /** | ||
/** | ||
* @returns {ReadError} Cannot read a file in this.path | ||
*/ | ||
static get ReadError () { | ||
return createError('ReadError') | ||
} | ||
/** | ||
* @returns {TypeError} Invalid file type | ||
*/ | ||
static get TypeError () { | ||
return createError('TypeError') | ||
} | ||
} | ||
module.exports = AbstractFile |
@@ -5,2 +5,4 @@ 'use strict' | ||
const AbstractFile = require('./abstract-file') | ||
const fileType = require('file-type') | ||
const readChunk = require('read-chunk') | ||
@@ -27,3 +29,3 @@ /** | ||
.catch((err) => { | ||
reject(err) | ||
reject(new File.ReadError(err.message)) | ||
}) | ||
@@ -45,3 +47,3 @@ }) | ||
} else { | ||
reject('Invalid file path provided') | ||
reject(`Error reading file stats for ${this.path}`) | ||
} | ||
@@ -51,4 +53,60 @@ }) | ||
} | ||
/** | ||
* First try to determine mime by reading magic bytes, it that fails, falls back to extension check. | ||
* | ||
* @returns {Promise} | ||
*/ | ||
get mime () { | ||
return new Promise((resolve, reject) => { | ||
this.magicBytesMime | ||
.then((mime) => { | ||
resolve(mime) | ||
}) | ||
.catch(() => { | ||
resolve(super.mime) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Get mime type by reading magic bytes. | ||
* | ||
* @returns {Promise} | ||
*/ | ||
get magicBytesMime () { | ||
return new Promise((resolve, reject) => { | ||
readChunk(this.path, 0, 262) | ||
.then((buffer) => { | ||
let type = fileType(buffer) | ||
if (type.mime) { | ||
resolve(type.mime) | ||
} else { | ||
reject(null) | ||
} | ||
}) | ||
.catch((err) => { | ||
reject(err) | ||
}) | ||
}) | ||
} | ||
/** | ||
* Checks whether tthe file is an image based on magic number on start of the file (file-type npm package) | ||
* | ||
* @return {Boolean} [description] | ||
*/ | ||
get isImage () { | ||
return new Promise((resolve, reject) => { | ||
this.magicBytesMime | ||
.then((mime) => { | ||
resolve(mime.split('/')[0] === 'image') | ||
}) | ||
.catch(() => { | ||
resolve(false) // for now sufficient | ||
}) | ||
}) | ||
} | ||
} | ||
module.exports = File |
@@ -47,4 +47,12 @@ 'use strict' | ||
return new Promise((resolve, reject) => { | ||
let image | ||
new File(path) | ||
.then((img) => { | ||
image = img | ||
return img.isImage | ||
}) | ||
.then((isImage) => { | ||
if (!isImage) throw new File.TypeError('This file is not an image') | ||
let resource = gm(path) | ||
@@ -62,5 +70,5 @@ | ||
_.extend(img, resource) | ||
_.extend(image, resource) | ||
resolve(img) | ||
resolve(image) | ||
}) | ||
@@ -67,0 +75,0 @@ .catch((err) => { |
{ | ||
"name": "@c8/simple-promised-file", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "A small promise library with basic file functionality and promise-wrapped gm image module.", | ||
@@ -28,6 +28,9 @@ "main": "index.js", | ||
"dependencies": { | ||
"create-error": "^0.3.1", | ||
"file-type": "^3.8.0", | ||
"filesize": "^3.2.1", | ||
"gm": "^1.22.0", | ||
"lodash": "^4.11.1", | ||
"mime": "^1.3.4" | ||
"mime": "^1.3.4", | ||
"read-chunk": "^2.0.0" | ||
}, | ||
@@ -34,0 +37,0 @@ "devDependencies": { |
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
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
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
603932
16
313
7
1
+ Addedcreate-error@^0.3.1
+ Addedfile-type@^3.8.0
+ Addedread-chunk@^2.0.0
+ Addedcreate-error@0.3.1(transitive)
+ Addedfile-type@3.9.0(transitive)
+ Addedpify@3.0.0(transitive)
+ Addedread-chunk@2.1.0(transitive)
+ Addedsafe-buffer@5.2.1(transitive)