12 private static readonly TimeSpan expiryTemporary = TimeSpan.FromHours(24);
13 private const string cacheFolderName =
"Attachments";
18 public AttachmentCacheService()
23 public override async Task Load(
bool IsResuming, CancellationToken CancellationToken)
25 if (this.
BeginLoad(IsResuming, CancellationToken))
29 CreateCacheFolderIfNeeded();
32 await EvictOldEntries();
49 public async Task<(
byte[]?, string)> TryGet(
string Url)
53 if (
string.IsNullOrWhiteSpace(Url) || !Uri.IsWellFormedUriString(Url, UriKind.RelativeOrAbsolute))
54 return (
null,
string.Empty);
59 return (
null,
string.Empty);
63 if (DateTime.UtcNow >= Entry.
Expires || !Exists)
71 return (
null,
string.Empty);
79 return (
null,
string.Empty);
91 public async Task Add(
string Url,
string ParentId,
bool Permanent,
byte[] Data,
string ContentType)
93 if (
string.IsNullOrWhiteSpace(Url) ||
94 !Uri.IsWellFormedUriString(Url, UriKind.RelativeOrAbsolute) ||
96 string.IsNullOrWhiteSpace(ContentType))
101 string CacheFolder = CreateCacheFolderIfNeeded();
104 DateTime Expires = Permanent ? DateTime.MaxValue : (DateTime.UtcNow + expiryTemporary);
112 LocalFileName = Path.Combine(CacheFolder, Guid.NewGuid().ToString() +
".bin"),
114 ContentType = ContentType
121 Entry.Expires = Expires;
122 Entry.ParentId = ParentId;
123 Entry.ContentType = ContentType;
138 public async Task<bool> Remove(
string Url)
140 if (
string.IsNullOrWhiteSpace(Url) || !Uri.IsWellFormedUriString(Url, UriKind.RelativeOrAbsolute))
167 public async Task<bool> RemoveAttachments(
Attachment[]? Attachments)
169 if (Attachments is
null)
172 bool Removed =
false;
186 private static string CreateCacheFolderIfNeeded()
188 string CacheFolder = GetCacheFolder();
190 if (!Directory.Exists(CacheFolder))
191 Directory.CreateDirectory(CacheFolder);
196 private static string GetCacheFolder()
198 return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), cacheFolderName);
201 private static async Task EvictOldEntries()
229 public async Task MakeTemporary(
string ParentId)
233 if (Entry.
Expires == DateTime.MaxValue)
235 Entry.Expires = DateTime.UtcNow + expiryTemporary;
247 public async Task MakePermanent(
string ParentId)
251 if (Entry.
Expires != DateTime.MaxValue)
253 Entry.Expires = DateTime.MaxValue;
Contains information about a file in the local cache.
CaseInsensitiveString ContentType
Content-Type
DateTime Expires
Timestamp of entry
CaseInsensitiveString LocalFileName
Local file name.
bool BeginLoad(bool IsResuming, CancellationToken CancellationToken)
Sets the IsLoading flag if the service isn't already loading.
void EndLoad(bool isLoaded)
Sets the IsLoading and IsLoaded flags and fires an event representing the current load state of the s...
bool IsResuming
If App is resuming service.
Base class that references services in the app.
static ILogService LogService
Log service.
Contains a reference to an attachment assigned to a legal object.
string ContentType
Internet Content Type of binary attachment.
string Url
URL to retrieve attachment, if provided.
Static interface for database persistence. In order to work, a database provider has to be assigned t...
static Task< IEnumerable< object > > FindDelete(string Collection, params string[] SortOrder)
Finds objects in a given collection and deletes them in the same atomic operation.
static IDatabaseProvider Provider
Registered database provider.
static async Task Update(object Object)
Updates an object in the database.
static async Task Delete(object Object)
Deletes an object in the database.
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
This filter selects objects that have a named field equal to a given value.
This filter selects objects that have a named field lesser or equal to a given value.
Represents a service that caches images downloaded via http calls. They are stored for a certain dura...
Task Flush()
Persists any pending changes.