public async Task <HttpResponseMessage> FileSystemPut(string id, bool update = false) { MessageWithStatusCode fileSystemNameFormat = CheckNameFormat(id, Database.Configuration.FileSystem.DataDirectory); if (fileSystemNameFormat.Message != null) { return(GetMessageWithString(fileSystemNameFormat.Message, fileSystemNameFormat.ErrorCode)); } var docKey = "Raven/FileSystems/" + id; // There are 2 possible ways to call this put. We either want to update a filesystem configuration or we want to create a new one. if (!update) { // As we are not updating, we should fail when the filesystem already exists. var existingFilesystem = Database.Documents.Get(docKey, null); if (existingFilesystem != null) { return(GetEmptyMessage(HttpStatusCode.Conflict)); } } var fsDoc = await ReadJsonObjectAsync <DatabaseDocument>(); FileSystemsLandlord.Protect(fsDoc); var json = RavenJObject.FromObject(fsDoc); json.Remove("Id"); Database.Documents.Put(docKey, null, json, new RavenJObject(), null); return(GetEmptyMessage(HttpStatusCode.Created)); }
public async Task <HttpResponseMessage> FileSystemPut(string id, bool update = false) { MessageWithStatusCode fileSystemNameFormat = CheckNameFormat(id, Database.Configuration.FileSystem.DataDirectory); if (fileSystemNameFormat.Message != null) { return(GetMessageWithObject(new { Error = fileSystemNameFormat.Message }, fileSystemNameFormat.ErrorCode)); } if (Authentication.IsLicensedForRavenFs == false) { return(GetMessageWithObject(new { Error = "Your license does not allow the use of RavenFS!" }, HttpStatusCode.BadRequest)); } var docKey = Constants.FileSystem.Prefix + id; // There are 2 possible ways to call this put. We either want to update a filesystem configuration or we want to create a new one. if (!update) { // As we are not updating, we should fail when the filesystem already exists. var existingFilesystem = Database.Documents.Get(docKey, null); if (existingFilesystem != null) { return(GetEmptyMessage(HttpStatusCode.Conflict)); } } var fsDoc = await ReadJsonObjectAsync <FileSystemDocument>(); EnsureFileSystemHasRequiredSettings(id, fsDoc); string bundles; if (fsDoc.Settings.TryGetValue(Constants.ActiveBundles, out bundles) && bundles.IndexOf("Encryption", StringComparison.OrdinalIgnoreCase) != -1) { if (fsDoc.SecuredSettings == null || !fsDoc.SecuredSettings.ContainsKey(Constants.EncryptionKeySetting) || !fsDoc.SecuredSettings.ContainsKey(Constants.AlgorithmTypeSetting)) { return(GetMessageWithString(string.Format("Failed to create '{0}' file system, because of invalid encryption configuration.", id), HttpStatusCode.BadRequest)); } } FileSystemsLandlord.Protect(fsDoc); var json = RavenJObject.FromObject(fsDoc); json.Remove("Id"); Database.Documents.Put(docKey, null, json, new RavenJObject(), null); return(GetEmptyMessage(HttpStatusCode.Created)); }
public async Task <HttpResponseMessage> DatabasesPut(string id) { if (IsSystemDatabase(id)) { return(GetMessageWithString("System database document cannot be changed!", HttpStatusCode.Forbidden)); } MessageWithStatusCode databaseNameFormat = CheckNameFormat(id, Database.Configuration.DataDirectory); if (databaseNameFormat.Message != null) { return(GetMessageWithString(databaseNameFormat.Message, databaseNameFormat.ErrorCode)); } Etag etag = GetEtag(); string error = CheckExistingDatabaseName(id, etag); if (error != null) { return(GetMessageWithString(error, HttpStatusCode.BadRequest)); } var dbDoc = await ReadJsonObjectAsync <DatabaseDocument>(); string bundles; if (dbDoc.Settings.TryGetValue(Constants.ActiveBundles, out bundles) && bundles.Contains("Encryption")) { if (dbDoc.SecuredSettings == null || !dbDoc.SecuredSettings.ContainsKey(Constants.EncryptionKeySetting) || !dbDoc.SecuredSettings.ContainsKey(Constants.AlgorithmTypeSetting)) { return(GetMessageWithString(string.Format("Failed to create '{0}' database, because of invalid encryption configuration.", id), HttpStatusCode.BadRequest)); } } //TODO: check if paths in document are legal DatabasesLandlord.Protect(dbDoc); var json = RavenJObject.FromObject(dbDoc); json.Remove("Id"); var metadata = (etag != null) ? ReadInnerHeaders.FilterHeadersToObject() : new RavenJObject(); var docKey = Constants.Database.Prefix + id; var putResult = Database.Documents.Put(docKey, etag, json, metadata, null); return((etag == null) ? GetEmptyMessage() : GetMessageWithObject(putResult)); }
protected static bool IsValidName(string name, string dataDirectory, out MessageWithStatusCode errorMessageWithStatusCode) { string errorMessage = null; bool isValid = true; const HttpStatusCode errorCode = HttpStatusCode.BadRequest; if (name == null) { errorMessage = "An empty name is forbidden for use!"; isValid = false; } else if (name.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0) { errorMessage = string.Format("The name '{0}' contains characters that are forbidden for use!", name); isValid = false; } else if (Array.IndexOf(Constants.WindowsReservedFileNames, name.ToLower()) >= 0) { errorMessage = string.Format("The name '{0}' is forbidden for use!", name); isValid = false; } else if ((Environment.OSVersion.Platform == PlatformID.Unix) && (name.Length > Constants.LinuxMaxFileNameLength) && (dataDirectory.Length + name.Length > Constants.LinuxMaxPath)) { int theoreticalMaxFileNameLength = Constants.LinuxMaxPath - dataDirectory.Length; int maxfileNameLength = (theoreticalMaxFileNameLength > Constants.LinuxMaxFileNameLength) ? Constants.LinuxMaxFileNameLength : theoreticalMaxFileNameLength; errorMessage = string.Format("Invalid name! Name cannot exceed {0} characters", maxfileNameLength); isValid = false; } else if (Path.Combine(dataDirectory, name).Length > Constants.WindowsMaxPath) { int maxfileNameLength = Constants.WindowsMaxPath - dataDirectory.Length; errorMessage = string.Format("Invalid name! Name cannot exceed {0} characters", maxfileNameLength); isValid = false; } errorMessageWithStatusCode = new MessageWithStatusCode { Message = errorMessage, ErrorCode = errorCode }; return(isValid); }
public async Task <HttpResponseMessage> DatabasesPut(string id) { MessageWithStatusCode databaseNameFormat = CheckDatabaseNameFormat(id); if (databaseNameFormat.Message != null) { return(GetMessageWithString(databaseNameFormat.Message, databaseNameFormat.ErrorCode)); } Etag etag = GetEtag(); string error = CheckDatbaseName(id, etag); if (error != null) { return(GetMessageWithString(string.Format(error, id), HttpStatusCode.BadRequest)); } var dbDoc = await ReadJsonObjectAsync <DatabaseDocument>(); if (dbDoc.Settings.ContainsKey("Bundles") && dbDoc.Settings["Bundles"].Contains("Encryption")) { if (!dbDoc.SecuredSettings.ContainsKey(Constants.EncryptionKeySetting) || !dbDoc.SecuredSettings.ContainsKey(Constants.AlgorithmTypeSetting)) { return(GetMessageWithString(string.Format("Failed to create '{0}' database, becuase of not valid encryption configuration.", id), HttpStatusCode.BadRequest)); } } DatabasesLandlord.Protect(dbDoc); var json = RavenJObject.FromObject(dbDoc); json.Remove("Id"); var metadata = (etag != null) ? InnerHeaders.FilterHeadersToObject() : new RavenJObject(); var docKey = "Raven/Databases/" + id; var putResult = Database.Documents.Put(docKey, etag, json, metadata, null); return((etag == null) ? GetEmptyMessage() : GetMessageWithObject(putResult)); }