Merge pull request #4674 from atom/normalize-native-image-paths
Normalize native image paths
This commit is contained in:
commit
68c413c391
2 changed files with 59 additions and 4 deletions
|
@ -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> NativeImage::CreateFromJPEG(
|
|||
mate::Handle<NativeImage> 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<NativeImage> handle = Create(isolate, image);
|
||||
#if defined(OS_MACOSX)
|
||||
if (IsTemplateFilename(path))
|
||||
if (IsTemplateFilename(image_path))
|
||||
handle->SetTemplateImage(true);
|
||||
#endif
|
||||
return handle;
|
||||
|
|
38
spec/api-native-image-spec.js
Normal file
38
spec/api-native-image-spec.js
Normal file
|
@ -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);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in a new issue