Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds ability to use absolute file paths on Windows. Fixes #137 #138

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions img.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,18 @@ class Util {
return obj;
}

static isFullUrl(url) {
static isRemoteUrl(url) {
try {
new URL(url);
return true;
} catch(e) {
const validUrl = new URL(url);

if (validUrl.protocol.startsWith("https:") || validUrl.protocol.startsWith("http:")) {
return true;
}

return false;
} catch(e)

{
// invalid url OR local path
return false;
}
Expand All @@ -111,7 +118,7 @@ class Image {
}

this.src = src;
this.isRemoteUrl = typeof src === "string" && Util.isFullUrl(src);
this.isRemoteUrl = typeof src === "string" && Util.isRemoteUrl(src);
this.options = Object.assign({}, globalOptions, options);

if(this.isRemoteUrl) {
Expand Down Expand Up @@ -506,8 +513,8 @@ class Image {
* any files.
*/
static statsSync(src, opts) {
if(typeof src === "string" && Util.isFullUrl(src)) {
throw new Error("`statsSync` is not supported with full URL sources. Use `statsByDimensionsSync` instead.");
if(typeof src === "string" && Util.isRemoteUrl(src)) {
throw new Error("`statsSync` is not supported with remote sources. Use `statsByDimensionsSync` instead.");
}

let dimensions = getImageSize(src);
Expand Down Expand Up @@ -586,7 +593,7 @@ function queueImage(src, opts) {

let promise = processingQueue.add(async () => {
if(typeof src === "string" && opts && opts.statsOnly) {
if(Util.isFullUrl(src)) {
if(Util.isRemoteUrl(src)) {
if(!opts.remoteImageMetadata || !opts.remoteImageMetadata.width || !opts.remoteImageMetadata.height) {
throw new Error("When using `statsOnly` and remote images, you must supply a `remoteImageMetadata` object with { width, height, format? }");
}
Expand Down
23 changes: 23 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -805,3 +805,26 @@ test("statsOnly using remote image, no urlFormat", async t => {
});
});

test("src is recognized as local when using absolute path on Windows", t => {
let image = new eleventyImage.Image("C:\\image.jpg");

t.is(image.isRemoteUrl, false);
});

test("src is recognized as local when using absolute path on POSIX", t => {
let image = new eleventyImage.Image("/home/user/image.jpg");

t.is(image.isRemoteUrl, false);
});

test("src is recognized as remote when using https scheme", t => {
let image = new eleventyImage.Image("https://example.com/image.jpg");

t.is(image.isRemoteUrl, true);
});

test("src is recognized as remote when using http scheme", t => {
let image = new eleventyImage.Image("http://example.com/image.jpg");

t.is(image.isRemoteUrl, true);
});