diff --git a/atom/common/api/atom_api_native_image.cc b/atom/common/api/atom_api_native_image.cc index a810069e71b..3d000b6d5d9 100644 --- a/atom/common/api/atom_api_native_image.cc +++ b/atom/common/api/atom_api_native_image.cc @@ -13,6 +13,7 @@ #include "atom/common/native_mate_converters/gurl_converter.h" #include "atom/common/node_includes.h" #include "base/base64.h" +#include "base/files/file_util.h" #include "base/strings/string_util.h" #include "base/strings/pattern.h" #include "native_mate/dictionary.h" @@ -119,6 +120,20 @@ bool PopulateImageSkiaRepsFromPath(gfx::ImageSkia* image, return succeed; } +base::FilePath NormalizePath(const base::FilePath& path) { + if (!path.ReferencesParent()) { + return path; + } + + base::FilePath absolute_path = MakeAbsoluteFilePath(path); + // MakeAbsoluteFilePath returns an empty path on failures so use original path + if (absolute_path.empty()) { + return path; + } else { + return absolute_path; + } +} + #if defined(OS_MACOSX) bool IsTemplateFilename(const base::FilePath& path) { return (base::MatchPattern(path.value(), "*Template.*") || @@ -254,17 +269,19 @@ mate::Handle NativeImage::CreateFromJPEG( mate::Handle NativeImage::CreateFromPath( v8::Isolate* isolate, const base::FilePath& path) { gfx::ImageSkia image_skia; - if (path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) { + base::FilePath image_path = NormalizePath(path); + + if (image_path.MatchesExtension(FILE_PATH_LITERAL(".ico"))) { #if defined(OS_WIN) - ReadImageSkiaFromICO(&image_skia, path); + ReadImageSkiaFromICO(&image_skia, image_path); #endif } else { - PopulateImageSkiaRepsFromPath(&image_skia, path); + PopulateImageSkiaRepsFromPath(&image_skia, image_path); } gfx::Image image(image_skia); mate::Handle handle = Create(isolate, image); #if defined(OS_MACOSX) - if (IsTemplateFilename(path)) + if (IsTemplateFilename(image_path)) handle->SetTemplateImage(true); #endif return handle; diff --git a/spec/api-native-image-spec.js b/spec/api-native-image-spec.js new file mode 100644 index 00000000000..043a914578c --- /dev/null +++ b/spec/api-native-image-spec.js @@ -0,0 +1,38 @@ +'use strict'; + +const assert = require('assert'); +const nativeImage = require('electron').nativeImage; +const path = require('path'); + +describe('nativeImage module', () => { + describe('createFromPath(path)', () => { + it('returns an empty image for invalid paths', () => { + assert(nativeImage.createFromPath('').isEmpty()); + assert(nativeImage.createFromPath('does-not-exist.png').isEmpty()); + }); + + it('loads images from paths relative to the current working directory', () => { + const imagePath = `.${path.sep}${path.join('spec', 'fixtures', 'assets', 'logo.png')}`; + const image = nativeImage.createFromPath(imagePath); + assert(!image.isEmpty()); + assert.equal(image.getSize().height, 190); + assert.equal(image.getSize().width, 538); + }); + + it('loads images from paths with `.` segments', () => { + const imagePath = `${path.join(__dirname, 'fixtures')}${path.sep}.${path.sep}${path.join('assets', 'logo.png')}`; + const image = nativeImage.createFromPath(imagePath); + assert(!image.isEmpty()); + assert.equal(image.getSize().height, 190); + assert.equal(image.getSize().width, 538); + }); + + it('loads images from paths with `..` segments', () => { + const imagePath = `${path.join(__dirname, 'fixtures', 'api')}${path.sep}..${path.sep}${path.join('assets', 'logo.png')}`; + const image = nativeImage.createFromPath(imagePath); + assert(!image.isEmpty()); + assert.equal(image.getSize().height, 190); + assert.equal(image.getSize().width, 538); + }); + }); +});