public Task<HttpResponseMessage> ExportFilesystem(StudioTasksController.ExportData smugglerOptionsJson) { var requestString = smugglerOptionsJson.SmugglerOptions; SmugglerFilesOptions smugglerOptions; using (var jsonReader = new RavenJsonTextReader(new StringReader(requestString))) { var serializer = JsonExtensions.CreateDefaultJsonSerializer(); smugglerOptions = (SmugglerFilesOptions)serializer.Deserialize(jsonReader, typeof(SmugglerFilesOptions)); } var result = GetEmptyMessage(); // create PushStreamContent object that will be called when the output stream will be ready. result.Content = new PushStreamContent(async (outputStream, content, arg3) => { try { var dataDumper = new FilesystemDataDumper(FileSystem, smugglerOptions); await dataDumper.ExportData( new SmugglerExportOptions<FilesConnectionStringOptions> { ToStream = outputStream }).ConfigureAwait(false); } finally { outputStream.Close(); } }); var fileName = String.IsNullOrEmpty(smugglerOptions.NoneDefualtFileName) || (smugglerOptions.NoneDefualtFileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) ? string.Format("Dump of {0}, {1}", this.FileSystemName, DateTime.Now.ToString("yyyy-MM-dd HH-mm", CultureInfo.InvariantCulture)) : smugglerOptions.NoneDefualtFileName; result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName + ".ravendump" }; return new CompletedTask<HttpResponseMessage>(result); }
public async Task<HttpResponseMessage> ImportFilesystem(int batchSize) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string tempPath = Path.GetTempPath(); var fullTempPath = tempPath + Constants.TempUploadsDirectoryName; if (File.Exists(fullTempPath)) File.Delete(fullTempPath); if (Directory.Exists(fullTempPath) == false) Directory.CreateDirectory(fullTempPath); var streamProvider = new MultipartFileStreamProvider(fullTempPath); await Request.Content.ReadAsMultipartAsync(streamProvider).ConfigureAwait(false); var uploadedFilePath = streamProvider.FileData[0].LocalFileName; string fileName = null; var fileContent = streamProvider.Contents.SingleOrDefault(); if (fileContent != null) { fileName = fileContent.Headers.ContentDisposition.FileName.Replace("\"", string.Empty); } var status = new ImportOperationStatus(); var cts = new CancellationTokenSource(); var task = Task.Run(async () => { try { var dataDumper = new FilesystemDataDumper(FileSystem); dataDumper.Progress += s => status.LastProgress = s; var smugglerOptions = dataDumper.Options; smugglerOptions.BatchSize = batchSize; smugglerOptions.CancelToken = cts; await dataDumper.ImportData(new SmugglerImportOptions<FilesConnectionStringOptions> { FromFile = uploadedFilePath }); } catch (Exception e) { status.Faulted = true; status.State = RavenJObject.FromObject(new { Error = e.ToString() }); if (cts.Token.IsCancellationRequested) { status.State = RavenJObject.FromObject(new { Error = "Task was cancelled" }); cts.Token.ThrowIfCancellationRequested(); //needed for displaying the task status as canceled and not faulted } if (e is InvalidDataException) { status.ExceptionDetails = e.Message; } else { status.ExceptionDetails = e.ToString(); } throw; } finally { status.Completed = true; File.Delete(uploadedFilePath); } }, cts.Token); long id; FileSystem.Tasks.AddTask(task, status, new TaskActions.PendingTaskDescription { StartTime = SystemTime.UtcNow, TaskType = TaskActions.PendingTaskType.ImportFileSystem, Payload = fileName, }, out id, cts); return GetMessageWithObject(new { OperationId = id }); }
public async Task export_result_cannot_contain_neither_files_scheduled_for_deletion_nor_tombstones(string storage) { var exportStream = new MemoryStream(); using (var store = this.NewStore(requestedStorage: storage)) { using (var session = store.OpenAsyncSession()) { for (int i = 0; i < 10; i++) session.RegisterUpload("prefix-" + i + ".file", CreateUniformFileStream(10)); await session.SaveChangesAsync(); } using (var session = store.OpenAsyncSession()) { session.RegisterFileDeletion("prefix-3.file"); await session.SaveChangesAsync(); } var fs = GetFileSystem(); var exporter = new FilesystemDataDumper(fs); await exporter.ExportData( new SmugglerExportOptions<FilesConnectionStringOptions> { ToStream = exportStream }).ConfigureAwait(false); using (var import = NewStore(1)) { exportStream.Position = 0; var importedFs = GetFileSystem(1); var importer = new FilesystemDataDumper(importedFs); await importer.ImportData(new SmugglerImportOptions<FilesConnectionStringOptions> { FromStream = exportStream }); importedFs.Storage.Batch(accessor => { Assert.Equal(9, accessor.GetFilesAfter(Etag.Empty, 100).Count()); }); } } }