500K+ Product Images, how to override/replace NopFileProvider / change image path

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
Hi,

for a customers project we need to import a lot of product images (500000+) which will end up in the images folder.
That would not work for us.

We would like to change the behaviour to use subfolders dependent on the product id or some other key to have folders like images/01/33/46/imagename.jpg

I guess this needs to be done in the NopFileProvider, so how can i use a custom one instead of the NopFileProvider?

Thanks in advance & best regards
Martin
4 years ago
I've added a property to product;

public List<string> ExternalImages { get; set; }

This is a json serialized list of external (azure blob) urls.
This means you only need to override some parts (upload, read/...) and not create a complete new NopFileProvider class
4 years ago
How can i override these Parts or the NopFileProvider update-safe?
I need to make sure that the images still show up in admin and frontend too.
4 years ago
Found the right solution for me with creating a plugin to extend the PictureService, add a new dependency with a high order and then overriding the methods which are responsible for creating the directory name. Saving files via the standard picture service works as well as images showing up in frontend an admin:

Dependency reg:
builder.RegisterType<ImageSubfolderPictureService>().As<IPictureService>().InstancePerLifetimeScope();

Class extension:
public partial class ImageSubfolderPictureService : PictureService

protected override void SavePictureInFile(int pictureId, byte[] pictureBinary, string mimeType)
        {
            var lastPart = GetFileExtensionFromMimeType(mimeType);
            var fileName = $"{pictureId:0000000}_0.{lastPart}";

           // custom logic to create new file path

            _fileProvider.WriteAllBytes(GetPictureLocalPath(fileName), pictureBinary);
        }

protected override string GetPictureLocalPath(string fileName)
        {
               return custom logic to create new file path
        }
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.