noxify / gridsome-plugin-remote-image

simple plugin to download remote images in gridsome
MIT License
14 stars 16 forks source link

Keep source filename #2

Closed hacknug closed 4 years ago

hacknug commented 4 years ago

Right now the plugin renames each file using a hash. I think it might be a better idea to allow users to configure this using options.targetFileName. This could accept a function that gets imageSource to allow users to keep the current functionality.

By default (when !options.targetFileName) it should keep the original file name.

noxify commented 4 years ago

Hi @hacknug,

thanks for the request.

Yes it should be possible to disable the automatic filename generation.

After thinking a bit about your request, I have two examples:

Example 1

If I want to embed an image, which is hosted on imgix, I can define some additional url parameters to transform/optimize/manipulate the image.

This means, the URL is

https://imgix.com/image.jpg?option1=1&option2=2

and not

https://imgix.com/image.jpg

Example 2

The image url doesn't contain a dedicated filename in the url. Example for this is: http://lorempixel.com/400/200/sports/1


For example 1, it's possible to extract the filename via a simple regexp, but for example 2, I have no idea how to solve this.

The reason why I have hashed the filename ( based on the Image URL ) is to keep it simple and make sure, that the image name is unique and doesn't overwrite an existing filename, because a filename only isn't unique for me and if I have different sources with the same filename, I have a conflict.

What do you think?

Thanks!

Regards, Marcus

dsturm commented 4 years ago

@noxify, good thoughts. For example 2, I would suggest:

  1. either to generate a filename based on it´s path and Content-Type (i.e. 400-200-sports-1.jpg) or
  2. stick to hash-based file names if there is no file name present

Cheers, Daniel

hacknug commented 4 years ago

Example 1

If I want to embed an image, which is hosted on imgix, I can define some additional url parameters to transform/optimize/manipulate the image.

This means, the URL is

https://imgix.com/image.jpg?option1=1&option2=2

and not

https://imgix.com/image.jpg

This one I'd probably solve with using URL and getting its pathname property. Should be trivial as you mentioned.

Example 2

The image url doesn't contain a dedicated filename in the url. Example for this is: http://lorempixel.com/400/200/sports/1

In this case I was going to say I would do what @dsturm suggested but realized images could be inside an /assets directory so I'd say:


In my case I have a bunch of JSON files with about 1000 image links each, most of which are not unique. What I would like is to be able to download all the files I'm missing and skip whatever's already on my repo. In this project I think I would mind to be able to replicate the sources dir structure.

It would also be cool to be able to specify if I want to force a re-download of everything (false by default I'd say).

noxify commented 4 years ago

Added with https://github.com/noxify/gridsome-plugin-remote-image/releases/tag/v1.1.0

Configuration: https://webstone.info/documentation/gridsome-plugin-remote-image/configuration