Start a new topic
Answered

Stream Asset

We create a new Job that needs to get a Stream Asset. What is the best way to implement the call of a method that accesses this file?


We are working with PDF files. So, for each Item of VirtoCommerce we input a PDF file together.

It's like e-books. These files are being inserted through Assets.

This part is ready.

But now, we need integrate the VirtoCommerce with other project using Web Services. For each e-book registered in VirtoCommerce we need call a method from webservice to register this e-book in this other project.

We decide use Jobs for it. So, how the best way of call from Asset (Blob) this e-book with Byte[] format? We have the ItemAssetId...

Sorry for the misspellings of English.


Best Answer

you could simply write something like:

  

private readonly IBlobStorageProvider _assetProvider;

 public StatusSafeCopyWork()
        {
		_assetProvider = FileSystemBlobAssetRepository("c:\blobs", new AssetEntityFactory());
        }

  Hope this helps.


I still don't quite understand all the requirements. What do you mean by registering the e-book in other system? Do you want to pass the whole PDF to other system? If so look at the IBlobStorageProvider interface. It accepts the key which is the AssetId that you already have. You can also use more inderect implementation called AssetService. The code will look something like this:


   var stream = _assetProvider.OpenReadOnly(assetId);

   if (stream.CanSeek)

    stream.Seek(0, SeekOrigin.Begin);

   return new StreamReader(stream);


using the StreamReader object you can then read the whole PDF.


Hope this helps.

Exactly Sasha, I need pass the whole PDF to other system.

How can I initialize the _assetProvider variable?

Thank you in advance.

You should create instances of IBlobStorageProvider (and IAssetService). VirtoCommerce is using Unity for that. container is IUnityContainer in the following code:

   

// using azure assets
            if (isAzure)
            {
                container.RegisterType<IAssetRepository, AzureBlobAssetRepository>();
                container.RegisterType<IBlobStorageProvider, AzureBlobAssetRepository>();
                container.RegisterType<IAssetUrl, AzureBlobAssetRepository>();
            }
            else
            {
                // using local storage assets
                container.RegisterType<IAssetRepository, FileSystemBlobAssetRepository>();
                container.RegisterType<IBlobStorageProvider, FileSystemBlobAssetRepository>();
                container.RegisterType<IAssetUrl, FileSystemAssetUrl>();
            }

            container.RegisterType<IAssetService, AssetService>();

  

Different classes are used depending on service hosting environment (cloud or on premise).

Hope this helps.

 

If i create this constructor in my new Jobclass:

private readonly IAssetService _assetProvider;

        public StatusSafeCopyWork(IAssetService assetProvider)

        {

            _assetProvider = assetProvider;

        }


Is it work?

I need change anything in the Commerce.JobWorkerRole ?

I already inserted rows in the SystemJobs table.

Answer

you could simply write something like:

  

private readonly IBlobStorageProvider _assetProvider;

 public StatusSafeCopyWork()
        {
		_assetProvider = FileSystemBlobAssetRepository("c:\blobs", new AssetEntityFactory());
        }

  Hope this helps.

Login or Signup to post a comment