/// <summary> /// This is the main thread, controls the whole update process. /// All other functions are fired of from here and once complete, the update is complete. /// </summary> private async void RefreshAllMedia() { try { // Start by cleaning the temp folder. CleanTempFolder(); var all = Database.Media; Lock.AcquireWriterLock(Timeout.Infinite); Total = all.Count(); Lock.ReleaseWriterLock(); foreach (var media in all) { CheckForCancel(); try { await _media.RefreshMedia(media, TempFolder); } catch (WebException ex) { StatusMessage = string.Format("Downloading file failed: {0} - {1}", media.Filename, ex.Message); } catch (Exception ex) { StatusMessage = string.Format("Error updating media: {0} - {1}", media.Filename, ex.Message); } Lock.AcquireWriterLock(Timeout.Infinite); Processed++; PercentComplete = (Processed / Total) * 60; StatusMessage = string.Format("Processed file: {0}", media.Filename); Lock.ReleaseWriterLock(); } Lock.AcquireWriterLock(Timeout.Infinite); PercentComplete = 60; StatusMessage = string.Format("Processed all files, commencing Content updates..."); Lock.ReleaseWriterLock(); await Database.SaveChangesAsync(); // now we need to iterate through any items that may have media attached, and re-attach. // content // properties // app users foreach (var content in Database.Content) { if (content.FeaturedImage != null) { var media = Database.Media.Find(content.FeaturedImage.Id); content.FeaturedImage = new ContentMedia(media); StatusMessage = string.Format("Refreshing content featured image: {0} {1}", content.Id, content.Title); } } await Database.SaveChangesAsync(); Lock.AcquireWriterLock(Timeout.Infinite); PercentComplete = 70; StatusMessage = string.Format("Content updated, commencing Properties updates..."); Lock.ReleaseWriterLock(); foreach (var property in Database.Properties) { if (property.FeaturedImage != null) { property.FeaturedImage = Database.Media.Find(property.FeaturedImage.Id); StatusMessage = string.Format("Refreshing property featured image: {0} {1}", property.Id, property.Title); } if (property.InfoDownload != null) { property.FeaturedImage = Database.Media.Find(property.FeaturedImage.Id); StatusMessage = string.Format("Refreshing property info download: {0} {1}", property.Id, property.Title); } } await Database.SaveChangesAsync(); Lock.AcquireWriterLock(Timeout.Infinite); PercentComplete = 80; StatusMessage = string.Format("Properties updated, commencing Users updates..."); Lock.ReleaseWriterLock(); foreach (var user in Database.Users) { if (user.Avatar != null) { user.Avatar = Database.Media.Find(user.Avatar.Id); StatusMessage = string.Format("Refreshing user avatar: {0} {1}", user.Id, user.ToInternalName()); } } await Database.SaveChangesAsync(); Lock.AcquireWriterLock(Timeout.Infinite); PercentComplete = 90; StatusMessage = string.Format("All done, emailing results..."); Lock.ReleaseWriterLock(); MailObject message = new MailObject() { PreHeader = "All media files have been refreshed.", Subject = "All media files have been refreshed." }; message.AddH1("Complete!"); message.AddParagraph("All media files have been successfully refreshed on " + _context.GetSiteUrl()); IEmailSender emailSender = Engine.Services.Resolve <IEmailSender>(); await emailSender.NotifyRoleAsync(message, "SuperUser"); // Clean the temp directory... CleanTempFolder(); Lock.AcquireWriterLock(Timeout.Infinite); PercentComplete = 100; Succeeded = true; Running = false; StatusMessage = "Update completed at " + DateTime.UtcNow.ToShortTimeString() + " on " + DateTime.UtcNow.ToLongDateString() + "."; Lock.ReleaseWriterLock(); return; } catch (Exception ex) { Lock.AcquireWriterLock(Timeout.Infinite); Running = false; StatusMessage = ex.Message; Lock.ReleaseWriterLock(); return; } }