- Blog
- Creating custom CloudStorage provider
Creating custom CloudStorage provider
- 8:29:07 PM
- Tuesday, February 23, 2016
To create a new Storage Provider, which will be used in CloudStorage plugin use ItPartner.Nop.Plugin.Misc.CloudStorage.CustomProvider project as source.
The process can be divided into 4 major steps:
-
Set system name of your provider and path to it's dll.
SeeCustomProviderPlugin.cs
:public static readonly string PROVIDER_SYSTEM_NAME = "CustomProvider"; public static readonly string PROVIDER_DIRECTORY_PATH = "~/Plugins/ItPartner.Misc.CloudStorage.CustomProvider/";
-
Set you provider's initializator which is used to register and access you provider in CloudStorage plugin.
SeeServices/CustomProviderServiceInitializer.cs
:public class CustomProviderServiceInitializer : ICloudStorageProviderInitializer
It should return instance of provider service for each storage type: pictures, downloads and content files:
public ICloudStorageProviderService GetPictureProvider() { return _customeProviderPictureService; } public ICloudStorageProviderService GetDownloadProvider() { return _customeProviderDownloadService; } public ICloudStorageProviderService GetContentProvider() { return _customeProviderContentService; }
-
Change configuration model, view and controller's methods .
OnControllers/CustomProviderController.cs
:public ActionResult Configure() { var providerSettings = _settingService.LoadSetting<CustomProviderSettings>(); var configurationModel = new ConfigurationModel() { ProviderSystemName = _customProviderServiceInitializer.GetSystemName(), AccountName = providerSettings.AccountName, AccountKey = providerSettings.AccountKey, ContainerName = providerSettings.ContainerName, CDN = providerSettings.CDN, UseCDN = providerSettings.UseCDN }; return PartialView("~/Plugins/ItPartner.Misc.CloudStorage.CustomProvider/Views/CustomProvider/_ProviderSettings.cshtml", configurationModel); }
Change the
Models/ConfigurationModel.cs
andViews/CustomProvider/_ProviderSettings.cshtml
according to your settings -
Add logic to provider's service.
OnServices/CustomProviderService.cs
:public override string InsertFile(string fileName, string contentType, byte[] binary) { //some of your code for inserting file in storage } public override bool IsFileExsit(string fileName) { //some of your code for checking the existence of file in storage } //...
There is a point to create separate instances of provider service for each item type. For example, you can set different container names for pictures, downloads and files and make every provider service store container name in it. That will minimize the count of loading provider settings.
private readonly string _container;
Please note that some methods like MoveFile, RenameFile, CreateDirectory and etc. are already implemented in abstract class BaseCloudStorageProviderService (you can see them in "Services/CustomProviderService.cs" called with base.ImplementedMethodName instead of throwing NotImplementedException). It was made for situations, when there is no correct method to move file for example. In that case we're going to download the whole file, upload it to another location and delete the old one. No doubt, that hurts perfomance and may have other difficulties, so if you have an oppotunity to implement it better without extra actions, override them!