public void SampleDataImportBackground(Uri url, string tmpPath, SampleDataImportPushNotification pushNotification) { Action<ExportImportProgressInfo> progressCallback = x => { pushNotification.InjectFrom(x); _pushNotifier.Upsert(pushNotification); }; try { pushNotification.Description = "Start downloading from " + url; _pushNotifier.Upsert(pushNotification); if (!Directory.Exists(tmpPath)) { Directory.CreateDirectory(tmpPath); } var tmpFilePath = Path.Combine(tmpPath, Path.GetFileName(url.ToString())); using (var client = new WebClient()) { client.DownloadProgressChanged += (sender, args) => { pushNotification.Description = string.Format("Sample data {0} of {1} downloading...", args.BytesReceived.ToHumanReadableSize(), args.TotalBytesToReceive.ToHumanReadableSize()); _pushNotifier.Upsert(pushNotification); }; var task = client.DownloadFileTaskAsync(url, tmpFilePath); task.Wait(); } using (var stream = File.Open(tmpFilePath, FileMode.Open)) { var manifest = _platformExportManager.ReadExportManifest(stream); if (manifest != null) { _platformExportManager.Import(stream, manifest, progressCallback); } } } catch (Exception ex) { pushNotification.Errors.Add(ex.ExpandExceptionMessage()); } finally { _settingsManager.SetValue(_sampledataStateSetting, SampleDataState.Completed); pushNotification.Description = "Import finished"; pushNotification.Finished = DateTime.UtcNow; _pushNotifier.Upsert(pushNotification); } }
public IHttpActionResult ImportSampleData([FromUri]string url = null) { lock (_lockObject) { var sampleDataState = EnumUtility.SafeParse<SampleDataState>(_settingsManager.GetValue<string>(_sampledataStateSetting, SampleDataState.Undefined.ToString()), SampleDataState.Undefined); if (sampleDataState == SampleDataState.Undefined) { //Sample data initialization if (Uri.IsWellFormedUriString(url, UriKind.Absolute)) { _settingsManager.SetValue(_sampledataStateSetting, SampleDataState.Processing); var pushNotification = new SampleDataImportPushNotification("System"); _pushNotifier.Upsert(pushNotification); BackgroundJob.Enqueue(() => SampleDataImportBackground(new Uri(url), HostingEnvironment.MapPath(Startup.VirtualRoot + "/App_Data/Uploads/"), pushNotification)); return Ok(pushNotification); } } } return StatusCode(HttpStatusCode.NoContent); }
public IHttpActionResult TryToImportSampleData([FromUri]string url = null) { lock (_lockObject) { if (!_settingsManager.GetValue("VirtoCommerce:SampleDataInstalled", false)) { _settingsManager.SetValue("VirtoCommerce:SampleDataInstalled", true); //Sample data initialization if (Uri.IsWellFormedUriString(url, UriKind.Absolute)) { var pushNotification = new SampleDataImportPushNotification("System"); _pushNotifier.Upsert(pushNotification); BackgroundJob.Enqueue(() => SampleDataImportBackground(new Uri(url), HostingEnvironment.MapPath("~/App_Data/Uploads/"), pushNotification)); return Ok(pushNotification); } } } return StatusCode(HttpStatusCode.NoContent); }