- Create custom data storage that allows to access files in custom folder - below you can see source codes of simple custom data storage that allows to access files in folder "C:\Images\".
- Specify that Vintasoft Web API controllers should use created custom data storage to access image files.
Here is C# code of custom data storage that allows to access files in folder "C:\Images\":
Code: Select all
using System.IO;
using System;
using Vintasoft.Data;
namespace AspNetCoreImagingDemo.Controllers
{
/// <summary>
/// Storage that allows to store data in database.
/// </summary>
/// <remarks>
/// Current data storage works in read-only mode.
/// </remarks>
public class CustomDataStorage : IDataStorage
{
static object _lockObject = new object();
string _folderPath = @"C:\Images";
public CustomDataStorage()
: base()
{
}
/// <summary>
/// Determines whether the data storage can store data of specified type.
/// </summary>
/// <param name="type">The type of data.</param>
/// <returns>
/// <b>True</b> if data storage can store data of specified type;
/// otherwise, <b>false</b>.
/// </returns>
/// <exception cref="System.ArgumentNullException">If <paramref name="type" /> is <b>null</b>.</exception>
public bool CanStore(Type type)
{
// specify that data storage can work only with images, which are stored in Stream objects
if (typeof(Stream).IsAssignableFrom(type))
return true;
else
return false;
}
/// <summary>
/// Determines whether the data storage contains an item with the specified identifier.
/// </summary>
/// <param name="key">The item key.</param>
/// <returns>
/// <b>True</b> - the data storage contains an item with the specified identifier;
/// <b>false</b> - the data storage does NOT contain an item with the specified identifier.
/// </returns>
/// <exception cref="System.ArgumentNullException">If <paramref name="key" /> is <b>null</b>.</exception>
public bool Contains(string key)
{
lock (_lockObject)
{
// check if image with identifier "key" is stored in folder
string fileName = Path.Combine(_folderPath, key);
return File.Exists(fileName);
}
}
/// <summary>
/// Returns the keys of all items from data storage.
/// </summary>
/// <returns>The keys of all items from data storage.</returns>
/// <remarks>Method can be not implemented if functionality is not necessary.</remarks>
public string[] GetKeys()
{
lock (_lockObject)
{
// get identifiers ("keys") of all images stored in folder
throw new NotImplementedException();
}
}
/// <summary>
/// Returns an item copy from the data storage.
/// </summary>
/// <param name="key">The item key.</param>
/// <returns>The copy (clone) of item data.</returns>
/// <exception cref="System.ArgumentNullException">If <paramref name="key" /> is <b>null</b>.</exception>
/// <exception cref="System.ArgumentException">Thrown if <paramref name="key"/> is not found.</exception>
/// <exception cref="System.InvalidOperationException">If item with specified <paramref name="key" /> is locked.</exception>
public object GetItemCopy(string key)
{
lock (_lockObject)
{
if (Contains(key))
{
// get image data from folder and return Stream object that contains image data
string fileName = Path.Combine(_folderPath, key);
return new FileStream(fileName, FileMode.Open, FileAccess.Read);
}
else
throw new Exception(string.Format("Data storage does not have image with identifier \"{0}\".", key));
}
}
/// <summary>
/// Adds new item to the data storage.
/// </summary>
/// <param name="key">The item key.</param>
/// <param name="item">The item data.</param>
/// <exception cref="System.ArgumentNullException">If <paramref name="key" /> is <b>null</b>.</exception>
/// <exception cref="System.ArgumentException">An element with the same key already exists in the data storage.</exception>
public void AddItem(string key, object item)
{
lock (_lockObject)
{
// add image with identifier "key" in folder
throw new NotImplementedException();
}
}
/// <summary>
/// Sets the existing item in the data storage.
/// </summary>
/// <param name="key">The item key.</param>
/// <param name="item">The item data.</param>
/// <exception cref="System.ArgumentNullException">If <paramref name="key" /> is <b>null</b>.</exception>
/// <exception cref="System.InvalidOperationException">If item with specified <paramref name="key" /> is locked.</exception>
public void SetItem(string key, object item)
{
lock (_lockObject)
{
// set image with identifier "key" in folder
throw new NotImplementedException();
}
}
/// <summary>
/// Deletes the item from the data storage.
/// </summary>
/// <param name="key">The item key.</param>
/// <exception cref="System.ArgumentNullException">If <paramref name="key" /> is <b>null</b>.</exception>
/// <exception cref="System.InvalidOperationException">If item with specified <paramref name="key" /> is locked.</exception>
public void DeleteItem(string key)
{
lock (_lockObject)
{
// delete image with identifier "key" in folder
throw new NotImplementedException();
}
}
/// <summary>
/// Locks an item in data storage and returns the item.
/// </summary>
/// <param name="key">The item key.</param>
/// <returns>The locked item from data storage.</returns>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="key" /> is <b>null</b>.</exception>
/// <exception cref="System.InvalidOperationException">Thrown if item with specified <paramref name="key" /> is already locked.</exception>
/// <seealso cref="UnlockItem(string, object)"/>
/// <remarks>Method must be implemented if image must be locked during changing.</remarks>
public object LockItem(string key)
{
lock (_lockObject)
{
// lock image with identifier "key" in folder
throw new NotImplementedException();
}
}
/// <summary>
/// Unlock the locked item in data storage.
/// </summary>
/// <param name="key">The item key.</param>
/// <param name="item">Locked item.<br />
/// The item will be overrwritten if item differs from the item returned by the <see cref="LockItem"/> method.</param>
/// <exception cref="System.ArgumentNullException">Thrown if <paramref name="key" /> is <b>null</b>.</exception>
/// <exception cref="System.InvalidOperationException">Thrown if item with specified <paramref name="key" /> is not locked.</exception>
/// <seealso cref="LockItem(string)"/>
/// <remarks>Method must be implemented if image must be locked during changing.</remarks>
public void UnlockItem(string key, object item)
{
lock (_lockObject)
{
// unlock image with identifier "key" in folder
throw new NotImplementedException();
}
}
/// <summary>
/// Releases all resources used by the <see cref="DataBaseImageStorage"/>.
/// </summary>
public void Dispose()
{
throw new NotImplementedException();
}
}
}
Here is C# code of VintaSoft Web API controller that allows to work with image collection:
Code: Select all
using Microsoft.AspNetCore.Hosting;
using Vintasoft.Data;
using Vintasoft.Imaging.AspNetCore.ApiControllers;
namespace AspNetCoreImagingDemo.Controllers
{
/// <summary>
/// A Web API controller that handles HTTP requests from clients and
/// allows to manage an image collection.
/// </summary>
public class MyVintasoftImageCollectionApiController : VintasoftImageCollectionApiController
{
/// <summary>
/// Initializes a new instance of the <see cref="MyVintasoftImageCollectionApiController"/> class.
/// </summary>
public MyVintasoftImageCollectionApiController(IWebHostEnvironment hostingEnvironment)
: base(hostingEnvironment)
{
}
protected override IDataStorage CreateSessionDataStorage(string sessionId)
{
// create custom data storage that allows to access files in folder "C:\Images\"
return new CustomDataStorage();
}
}
}
Here is C# code of VintaSoft Web API controller that allows to work with Vintasoft images:
Code: Select all
using Microsoft.AspNetCore.Hosting;
using System;
using Vintasoft.Data;
using Vintasoft.Imaging.AspNetCore.ApiControllers;
using Vintasoft.Imaging.Pdf;
namespace AspNetCoreImagingDemo.Controllers
{
/// <summary>
/// A Web API controller that handles HTTP requests from clients and
/// allows to get information about image or render image thumbnail/tile.
/// </summary>
public class MyVintasoftImageApiController : VintasoftImageApiController
{
/// <summary>
/// Initializes a new instance of the <see cref="MyVintasoftImageApiController"/> class.
/// </summary>
public MyVintasoftImageApiController(IWebHostEnvironment hostingEnvironment)
: base(hostingEnvironment)
{
}
protected override IDataStorage CreateSessionDataStorage(string sessionId)
{
// create custom data storage that allows to access files in folder "C:\Images\"
return new CustomDataStorage();
}
}
}