Skip to content

Commit

Permalink
Adds ability to use absolute file paths on Windows. Fixes #137
Browse files Browse the repository at this point in the history
  • Loading branch information
mustafaoral committed Jan 12, 2022
1 parent ca62fa5 commit a3f1742
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
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);
});

0 comments on commit a3f1742

Please sign in to comment.