From cf271100f5ca947edef07ef0844a2ff95e370637 Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 11 Apr 2017 00:34:21 -0400 Subject: [PATCH] fix(image-stream): throw user error if image is a directory (#1279) If the user tries to drag and drop a directory to the application, then he'll get a scary `EISDIR` error message. This commit catches this error, and display a nice user friendly message instead. Change-Type: patch Changelog-Entry: Prevent uncaught `EISDIR` when dropping a directory to the application. Signed-off-by: Juan Cruz Viotti --- lib/image-stream/index.js | 12 ++++-- tests/image-stream/directory.spec.js | 62 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tests/image-stream/directory.spec.js diff --git a/lib/image-stream/index.js b/lib/image-stream/index.js index a838af34..2003da3e 100644 --- a/lib/image-stream/index.js +++ b/lib/image-stream/index.js @@ -67,12 +67,16 @@ const errors = require('../shared/errors'); * }); */ exports.getFromFilePath = (file) => { - return utils.getArchiveMimeType(file).then((type) => { - if (!_.has(handlers, type)) { - throw errors.createUserError('Invalid image', `The ${type} format is not supported`); + return fs.statAsync(file).then((fileStats) => { + if (!fileStats.isFile()) { + throw errors.createUserError('Invalid image', 'The image must be a file'); } - return fs.statAsync(file).then((fileStats) => { + return utils.getArchiveMimeType(file).then((type) => { + if (!_.has(handlers, type)) { + throw errors.createUserError('Invalid image', `The ${type} format is not supported`); + } + return _.invoke(handlers, type, file, { size: fileStats.size }); diff --git a/tests/image-stream/directory.spec.js b/tests/image-stream/directory.spec.js new file mode 100644 index 00000000..d5983bc3 --- /dev/null +++ b/tests/image-stream/directory.spec.js @@ -0,0 +1,62 @@ +/* + * Copyright 2016 resin.io + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +'use strict'; + +const m = require('mochainon'); +const path = require('path'); +const DATA_PATH = path.join(__dirname, 'data'); +const IMAGES_PATH = path.join(DATA_PATH, 'images'); +const errors = require('../../lib/shared/errors'); +const imageStream = require('../../lib/image-stream/index'); + +describe('ImageStream: Directory', function() { + + this.timeout(20000); + + describe('.getFromFilePath()', function() { + + describe('given a directory', function() { + + it('should be rejected with an error', function(done) { + imageStream.getFromFilePath(IMAGES_PATH).catch((error) => { + m.chai.expect(error).to.be.an.instanceof(Error); + m.chai.expect(errors.getTitle(error)).to.equal('Invalid image'); + m.chai.expect(errors.getDescription(error)).to.equal('The image must be a file'); + m.chai.expect(error.report).to.be.false; + done(); + }); + }); + + }); + + }); + + describe('.getImageMetadata()', function() { + + it('should be rejected with an error', function(done) { + imageStream.getImageMetadata(IMAGES_PATH).catch((error) => { + m.chai.expect(error).to.be.an.instanceof(Error); + m.chai.expect(errors.getTitle(error)).to.equal('Invalid image'); + m.chai.expect(errors.getDescription(error)).to.equal('The image must be a file'); + m.chai.expect(error.report).to.be.false; + done(); + }); + }); + + }); + +});