public async Task <string> AddSharedFile(IFormFile file, string organizationID) { var expirationDate = DateTimeOffset.Now.AddDays(-AppConfig.DataRetentionInDays); var expiredFiles = RemotelyContext.SharedFiles.Where(x => x.Timestamp < expirationDate); RemotelyContext.RemoveRange(expiredFiles); byte[] fileContents; using (var stream = file.OpenReadStream()) { using var ms = new MemoryStream(); await stream.CopyToAsync(ms); fileContents = ms.ToArray(); } var newEntity = RemotelyContext.Add(new SharedFile() { FileContents = fileContents, FileName = file.FileName, ContentType = file.ContentType, OrganizationID = organizationID }); await RemotelyContext.SaveChangesAsync(); return(newEntity.Entity.ID); }
public void CleanupOldRecords() { if (AppConfig.DataRetentionInDays > 0) { var expirationDate = DateTimeOffset.Now - TimeSpan.FromDays(AppConfig.DataRetentionInDays); var eventLogs = RemotelyContext.EventLogs .Where(x => x.TimeStamp < expirationDate); RemotelyContext.RemoveRange(eventLogs); var commandResults = RemotelyContext.CommandResults .Where(x => x.TimeStamp < expirationDate); RemotelyContext.RemoveRange(commandResults); var sharedFiles = RemotelyContext.SharedFiles .Where(x => x.Timestamp < expirationDate); RemotelyContext.RemoveRange(sharedFiles); RemotelyContext.SaveChanges(); } }