public async Task <HttpResponseMessage> Import() { // Make sure that we actually got the right data if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } try { // Store the uploaded file into a temporary location var provider = new MultipartFormDataStreamProvider(Path.GetTempPath()); await Request.Content.ReadAsMultipartAsync(provider); string filename = provider.FormData.GetValues("filename").First(); var file = provider.FileData.First(); // Setup an import using RavenDb's Smuggler API or the DatabaseDumper API depending on whether the embedded database is being used SmugglerDatabaseApiBase importer; RavenConnectionStringOptions connectionStringOptions; if (Database.DocumentStore is EmbeddableDocumentStore embeddableDocumentStore) { importer = new DatabaseDataDumper(embeddableDocumentStore.DocumentDatabase); connectionStringOptions = new EmbeddedRavenConnectionStringOptions(); } else { importer = new SmugglerDatabaseApi(); connectionStringOptions = new RavenConnectionStringOptions() { Url = Database.DocumentStore.Url }; } var importOptions = new SmugglerImportOptions <RavenConnectionStringOptions>() { FromFile = file.LocalFileName, To = connectionStringOptions }; await importer.ImportData(importOptions); return(new HttpResponseMessage(HttpStatusCode.OK)); } catch (Exception exp) { return(this.Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exp)); } }
public async Task <HttpResponseMessage> Export() { try { // Setup an export using RavenDb's Smuggler API var exportTimestamp = DateTime.Now; var fileName = $"augurk-{exportTimestamp.ToString("yyyy-dd-M-HHmmss")}.bak"; var options = new SmugglerDatabaseOptions { OperateOnTypes = ItemType.Documents, Filters = new List <FilterSetting> { new FilterSetting { Path = "@metadata.@id", ShouldMatch = false, Values = new List <string> { ConfigurationManager.DOCUMENT_KEY, CustomizationManager.DOCUMENT_KEY, } } } }; // Determine the appropriate import method to use SmugglerDatabaseApiBase exporter; RavenConnectionStringOptions connectionStringOptions; if (Database.DocumentStore is EmbeddableDocumentStore embeddableDocumentStore) { exporter = new DatabaseDataDumper(embeddableDocumentStore.DocumentDatabase, options); connectionStringOptions = new EmbeddedRavenConnectionStringOptions(); } else { exporter = new SmugglerDatabaseApi(options); connectionStringOptions = new RavenConnectionStringOptions() { Url = Database.DocumentStore.Url }; } var exportOptions = new SmugglerExportOptions <RavenConnectionStringOptions>() { ToFile = Path.Combine(Path.GetTempPath(), fileName), From = connectionStringOptions }; // Perform the export await exporter.ExportData(exportOptions); // Stream the backup back to the client var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(File.ReadAllBytes(exportOptions.ToFile)) }; result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = fileName }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); return(result); } catch { return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "An exception occured while generating export.")); } }