-
Notifications
You must be signed in to change notification settings - Fork 0
/
RestAPIImageRetriever.cs
117 lines (102 loc) · 4.92 KB
/
RestAPIImageRetriever.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using ImageResizer.Storage;
using Newtonsoft.Json;
namespace RestAPIImageRetriever
{
public class RestAPIImageRetriever : BlobProviderBase
{
static readonly HttpClient client = new HttpClient();
static readonly string API_BASE = "rest/V2.0/list/MediaAssetFile/byIdentifiers?identifiers=";
static readonly string LAST_MODIFIED = "&fields=MediaAssetFileAttribute.LastModified";
static readonly string FILE_URL = "&fields=MediaAssetFileAttribute.HTTPPath";
public RestAPIImageRetriever()
{
try
{
client.BaseAddress = new Uri(ConfigurationManager.AppSettings["RestAPIBaseURI"]);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var authString = $"{ConfigurationManager.AppSettings["RestAPIUser"]}:{ConfigurationManager.AppSettings["RestAPIPassword"]}";
var byteArray = Encoding.ASCII.GetBytes(authString);
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
LogFileAppender("Plugin has started!");
}
catch (Exception e)
{
LogFileAppender($"Error thrown during startup, {e}");
}
}
public override Task<IBlobMetadata> FetchMetadataAsync(string virtualPath, NameValueCollection queryString)
{
LogFileAppender($"DEBUG :: Trying to get meta data for virtualPath {virtualPath}");
try
{
LogFileAppender($"DEBUG :: Trying to get meta data for virtualPath {virtualPath}");
var data = GetDataFromAPI(virtualPath, true);
var result = new BlobMetadata { Exists = data.RowCount == "1" };
if (result.Exists.Value)
result.LastModifiedDateUtc = DateTime.Parse(data.Rows.Row.Values.Value.Text);
return Task.FromResult<IBlobMetadata>(result);
}
catch (Exception e)
{
LogFileAppender($"ERROR :: Something went wrong when trying to fetch metadata, {e}");
return Task.FromResult<IBlobMetadata>(new BlobMetadata { Exists = false });
}
}
public override Task<Stream> OpenAsync(string virtualPath, NameValueCollection queryString)
{
LogFileAppender($"DEBUG :: Trying to get stream for virtualPath {virtualPath}");
try
{
LogFileAppender($"DEBUG :: Trying to get stream for virtualPath {virtualPath}");
var data = GetDataFromAPI(virtualPath, false);
if (data == null)
throw new NullReferenceException($"ERROR :: No data found for virtual path {virtualPath}");
if (data.RowCount != "1")
throw new ArgumentException($"ERROR :: The path {virtualPath} did not return 1 row!");
var stream = GetStreamFromUrl(data.Rows.Row.Values.Value.Text);
stream.Seek(0, SeekOrigin.Begin);
return Task.FromResult<Stream>(stream);
}
catch (Exception e)
{
LogFileAppender($"ERROR :: Something went wrong when trying to open stream for virtual path {virtualPath}, {e}");
return null;
}
}
private EntityItemTable GetDataFromAPI(string virtualPath, bool metaDataCall)
{
var response = client.GetAsync(GetPath(virtualPath, metaDataCall)).Result;
LogFileAppender($"DEBUG :: Location: {response.Headers.Location}");
return JsonConvert.DeserializeObject<EntityItemTable>(response.Content.ToString());
}
private static string GetPath(string virtualPath, bool metaDataCall)
{
var id = virtualPath.Split('/').ToList().Last();
var field = metaDataCall ? LAST_MODIFIED : FILE_URL;
return API_BASE + id + field;
}
private static Stream GetStreamFromUrl(string url)
{
byte[] imageData = null;
using (var wc = new System.Net.WebClient())
imageData = wc.DownloadData(url);
return new MemoryStream(imageData);
}
private void LogFileAppender(string log)
{
var logFileName = "C:\\temp\\RestAPIImageRetriever.txt";
File.AppendAllLines(logFileName, new List<string> { $"{DateTime.UtcNow} :: {log}" });
}
}
}