public async Task <IActionResult> DownloadSitemap(string storeId, string baseUrl) { var notification = new SitemapDownloadNotification(_userNameResolver.GetCurrentUserName()) { Title = "Download sitemaps", Description = "Processing download sitemaps..." }; await _notifier.SendAsync(notification); BackgroundJob.Enqueue(() => BackgroundDownload(storeId, baseUrl, notification)); return(Ok(notification)); }
public async Task BackgroundDownload(string storeId, string baseUrl, SitemapDownloadNotification notification) { void SendNotificationWithProgressInfo(ExportImportProgressInfo c) { // TODO: is there a better way to copy ExportImportProgressInfo properties to SitemapDownloadNotification without using ValueInjecter? notification.Description = c.Description; notification.ProcessedCount = c.ProcessedCount; notification.TotalCount = c.TotalCount; notification.Errors = c.Errors?.ToList() ?? new List <string>(); _notifier.Send(notification); } try { var relativeUrl = $"tmp/sitemap-{storeId}.zip"; var localTmpFolder = MapPath(_hostingEnvironment, "~/App_Data/Uploads/tmp"); var localTmpPath = Path.Combine(localTmpFolder, $"sitemap-{storeId}.zip"); if (!Directory.Exists(localTmpFolder)) { Directory.CreateDirectory(localTmpFolder); } //Import first to local tmp folder because Azure blob storage doesn't support some special file access mode using (var stream = SystemFile.Open(localTmpPath, FileMode.OpenOrCreate)) { using (var zipPackage = ZipPackage.Open(stream, FileMode.Create)) { await CreateSitemapPartAsync(zipPackage, storeId, baseUrl, "sitemap.xml", SendNotificationWithProgressInfo); var sitemapUrls = await _sitemapXmlGenerator.GetSitemapUrlsAsync(storeId); foreach (var sitemapUrl in sitemapUrls) { if (!string.IsNullOrEmpty(sitemapUrl)) { await CreateSitemapPartAsync(zipPackage, storeId, baseUrl, sitemapUrl, SendNotificationWithProgressInfo); } } } } //Copy export data to blob provider for get public download url using (var localStream = SystemFile.Open(localTmpPath, FileMode.Open)) using (var blobStream = _blobStorageProvider.OpenWrite(relativeUrl)) { localStream.CopyTo(blobStream); notification.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(relativeUrl); notification.Description = "Sitemap download finished"; } } catch (Exception exception) { notification.Description = "Sitemap download failed"; notification.Errors.Add(exception.ExpandExceptionMessage()); } finally { notification.Finished = DateTime.UtcNow; await _notifier.SendAsync(notification); } }