예제 #1
0
        public async Task PlatformExportBackgroundAsync(PlatformImportExportRequest exportRequest, PlatformExportPushNotification pushNotification, IJobCancellationToken cancellationToken, PerformContext context)
        {
            void progressCallback(ExportImportProgressInfo x)
            {
                pushNotification.Path(x);
                pushNotification.JobId = context.BackgroundJob.Id;
                _pushNotifier.Send(pushNotification);
            }

            try
            {
                var fileName       = string.Format(_platformOptions.DefaultExportFileName, DateTime.UtcNow);
                var localTmpFolder = Path.GetFullPath(Path.Combine(_platformOptions.DefaultExportFolder));
                var localTmpPath   = Path.Combine(localTmpFolder, Path.GetFileName(fileName));

                if (!Directory.Exists(localTmpFolder))
                {
                    Directory.CreateDirectory(localTmpFolder);
                }

                if (System.IO.File.Exists(localTmpPath))
                {
                    System.IO.File.Delete(localTmpPath);
                }

                //Import first to local tmp folder because Azure blob storage doesn't support some special file access mode
                using (var stream = System.IO.File.OpenWrite(localTmpPath))
                {
                    var manifest = exportRequest.ToManifest();
                    await _platformExportManager.ExportAsync(stream, manifest, progressCallback, new JobCancellationTokenWrapper(cancellationToken));

                    pushNotification.DownloadUrl = $"api/platform/export/download/{fileName}";
                }
            }
            catch (JobAbortedException)
            {
                //do nothing
            }
            catch (Exception ex)
            {
                pushNotification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                pushNotification.Description = "Export finished";
                pushNotification.Finished    = DateTime.UtcNow;
                await _pushNotifier.SendAsync(pushNotification);
            }
        }
        public async Task PlatformExportBackgroundAsync(PlatformImportExportRequest exportRequest, PlatformExportPushNotification pushNotification, IJobCancellationToken cancellationToken, PerformContext context)
        {
            Action <ExportImportProgressInfo> progressCallback = x =>
            {
                pushNotification.Path(x);
                pushNotification.JobId = context.BackgroundJob.Id;
                _pushNotifier.SendAsync(pushNotification);
            };

            try
            {
                const string relativeUrl    = "tmp/exported_data.zip";
                var          localTmpFolder = _hostEnv.MapPath(Path.Combine(_platformOptions.LocalUploadFolderPath, "tmp"));
                var          localTmpPath   = Path.Combine(localTmpFolder, "exported_data.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 = System.IO.File.Open(localTmpPath, FileMode.OpenOrCreate))
                {
                    var manifest = exportRequest.ToManifest();
                    await _platformExportManager.ExportAsync(stream, manifest, progressCallback, new JobCancellationTokenWrapper(cancellationToken));
                }
                //Copy export data to blob provider for get public download url
                using (var localStream = System.IO.File.Open(localTmpPath, FileMode.Open))
                    using (var blobStream = _blobStorageProvider.OpenWrite(relativeUrl))
                    {
                        localStream.CopyTo(blobStream);
                        //Get a download url
                        pushNotification.DownloadUrl = _blobUrlResolver.GetAbsoluteUrl(relativeUrl);
                    }
            }
            catch (JobAbortedException)
            {
                //do nothing
            }
            catch (Exception ex)
            {
                pushNotification.Errors.Add(ex.ExpandExceptionMessage());
            }
            finally
            {
                pushNotification.Description = "Export finished";
                pushNotification.Finished    = DateTime.UtcNow;
                await _pushNotifier.SendAsync(pushNotification);
            }
        }