-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProgram.cs
More file actions
100 lines (80 loc) · 2.91 KB
/
Program.cs
File metadata and controls
100 lines (80 loc) · 2.91 KB
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
using System.Collections.Concurrent;
using System.Threading;
using GroupDocs.Viewer.UI.Core;
var builder = WebApplication.CreateBuilder(args);
var viewerType = ViewerType.HtmlWithEmbeddedResources;
builder.Services
.AddGroupDocsViewerUI(config =>
{
config.RenderingMode = viewerType.ToRenderingMode();
});
builder.Services
.AddControllers()
.AddGroupDocsViewerSelfHostApi(config =>
{
config.SetViewerType(viewerType);
//Trial limitations https://docs.groupdocs.com/viewer/net/evaluation-limitations-and-licensing-of-groupdocs-viewer/
//Temporary license can be requested at https://purchase.groupdocs.com/temporary-license
//config.SetLicensePath("GroupDocs.Viewer.lic"); // or set environment variable 'GROUPDOCS_LIC_PATH'
})
.AddLocalStorage("./Files");
//NOTE: registered after AddGroupDocsViewerSelfHostApi()
builder.Services.AddSingleton<IFileCache, ConcurrentDictionaryFileCache>();
var app = builder.Build();
app
.UseRouting()
.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.SendFileAsync("index.html");
});
endpoints.MapGroupDocsViewerUI(options =>
{
options.UIPath = "/viewer";
options.ApiEndpoint = "/viewer-api";
});
endpoints.MapGroupDocsViewerApi(options =>
{
options.ApiPath = "/viewer-api";
});
});
await app.RunAsync();
class ConcurrentDictionaryFileCache : IFileCache
{
private readonly ConcurrentDictionary<string, object> _cache =
new ConcurrentDictionary<string, object>();
public TEntry TryGetValue<TEntry>(string cacheKey, string filePath)
{
string key = $"{filePath}_{cacheKey}";
if (_cache.TryGetValue(key, out object? obj))
return (TEntry)obj;
return default!;
}
public Task<TEntry> TryGetValueAsync<TEntry>(string cacheKey, string filePath, CancellationToken cancellationToken = default)
{
TEntry entry = TryGetValue<TEntry>(cacheKey, filePath);
return Task.FromResult(entry);
}
public void Set<TEntry>(string cacheKey, string filePath, TEntry entry)
{
if(entry is null)
throw new ArgumentNullException(nameof(entry));
string key = $"{filePath}_{cacheKey}";
_cache.TryAdd(key, entry);
}
public Task SetAsync<TEntry>(string cacheKey, string filePath, TEntry entry, CancellationToken cancellationToken = default)
{
Set(cacheKey, filePath, entry);
return Task.CompletedTask;
}
public Task RemoveAsync(string filePath, CancellationToken cancellationToken = default)
{
var keysToRemove = _cache.Keys
.Where(k => k.StartsWith($"{filePath}_"))
.ToList();
foreach (var key in keysToRemove)
_cache.TryRemove(key, out _);
return Task.CompletedTask;
}
}