// For complete examples and data files, please go to https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-.NET
public class AmazonCacheDataHandler : ICacheDataHandler
{
private static string bucketName = ""; //TODO: Put you bucketname here
private readonly ViewerConfig _viewerConfig;
private readonly AmazonS3Client _client;
public AmazonCacheDataHandler(ViewerConfig viewerConfig)
{
_viewerConfig = viewerConfig;
_client = new AmazonS3Client(RegionEndpoint.EUWest1);
}
public bool Exists(CacheFileDescription cacheFileDescription)
{
if (!_viewerConfig.UseCache)
{
return false;
}
if (cacheFileDescription == null)
{
throw new System.Exception("CacheFileDescription is not set");
}
if (string.IsNullOrEmpty(cacheFileDescription.Guid))
{
throw new System.Exception("CacheFileDescription is not set");
}
if (string.IsNullOrEmpty(_viewerConfig.StoragePath))
{
throw new System.Exception("Storage path is not set");
}
var key = GetCachePath(_viewerConfig.CachePath, cacheFileDescription);
S3FileInfo fileInfo = new S3FileInfo(_client, bucketName, key);
if (!fileInfo.Exists)
{
return false;
}
return (fileInfo.LastWriteTimeUtc >= DateTime.UtcNow.AddMinutes(-5));
}
public Stream GetInputStream(CacheFileDescription cacheFileDescription)
{
if (cacheFileDescription == null || String.IsNullOrEmpty(cacheFileDescription.Guid))
{
throw new System.Exception("CacheFileDescription is not set");
}
var key = GetCachePath(_viewerConfig.CachePath, cacheFileDescription);
var fileInfo = new S3FileInfo(_client, bucketName, key);
if (!fileInfo.Exists)
{
throw new System.Exception("File not found");
}
return fileInfo.OpenRead();
}
public Stream GetOutputSaveStream(CacheFileDescription cacheFileDescription)
{
try
{
if (!_viewerConfig.UseCache)
{
return new MemoryStream();
}
if (cacheFileDescription == null || String.IsNullOrEmpty(cacheFileDescription.Guid))
{
throw new System.Exception("CacheFileDescription is not set");
}
string key = GetCachePath(_viewerConfig.CachePath, cacheFileDescription);
S3FileInfo fileInfo = new S3FileInfo(_client, bucketName, key);
return fileInfo.Create();
}
catch (System.Exception e)
{
throw new System.Exception(e.Message);
}
}
public string GetCacheUri(CacheFileDescription cacheFileDescription)
{
return GetCachePath(_viewerConfig.CachePath, cacheFileDescription);
}
private string GetCachePath(string path, CacheFileDescription cacheFileDescription)
{
if (cacheFileDescription == null)
{
throw new System.Exception("CacheFileDescription is not set");
}
string filePath;
string fileName;
if (cacheFileDescription != null)
{
if (!string.IsNullOrEmpty(cacheFileDescription.Name))
{
fileName = string.Format("{0}.{1}", cacheFileDescription.Name,
cacheFileDescription.FileType.ToString().ToLower());
}
else
{
fileName = string.Format("{0}.{1}", cacheFileDescription.BaseName,
cacheFileDescription.FileType.ToString().ToLower());
}
filePath = string.Format(@"{0}\{1}\{2}\", path, cacheFileDescription.Guid, fileName);
}
else
{
fileName = !string.IsNullOrEmpty(cacheFileDescription.Name)
? string.Format("{0}.{1}", cacheFileDescription.Name, cacheFileDescription.FileType.ToString().ToLower())
: string.Format("{0}.{1}", cacheFileDescription.BaseName, cacheFileDescription.FileType.ToString().ToLower());
filePath = string.Format(@"{0}\{1}\{2}", path, cacheFileDescription.Guid, fileName);
}
return filePath;
}
public string GetFilePath(CacheFileDescription cacheFileDescription)
{
//TODO: Return File Path
return "";
}
public void ClearCache()
{
//TODO: Write Clear Cache code
}
public void ClearCache(TimeSpan olderThanDays)
{
//TODO: Write Clear Cache with TimeSpan code
}
public List<CachedPageResourceDescription> GetHtmlPageResources(CachedPageDescription cachePageDescription)
{
//TODO: Return Html Page Resources
return new List<CachedPageResourceDescription>();
}
public string GetHtmlPageResourcesFolder(CachedPageDescription cachePageDescription)
{
//TODO: Return File Html Resources Folder
return "<<Html resources folder>>";
}
public DateTime? GetLastModificationDate(CacheFileDescription cacheFileDescription)
{
//TODO: Return Last Modification Date
return DateTime.Now;
}
}