public static MimeMap CreateMimeMap(dynamic model, StaticContentSection section) { if (model == null) { throw new ApiArgumentException("model"); } string fileExtension = DynamicHelper.Value(model.file_extension); string mimeType = DynamicHelper.Value(model.mime_type); if (string.IsNullOrEmpty(fileExtension)) { throw new ApiArgumentException("file_extension"); } if (string.IsNullOrEmpty(mimeType)) { throw new ApiArgumentException("mime_type"); } MimeMap mimeMap = section.MimeMaps.CreateElement(); mimeMap.FileExtension = fileExtension.StartsWith(".") ? fileExtension : "." + fileExtension; mimeMap.MimeType = mimeType; return(mimeMap); }
public static void DeleteMimeType(MimeMap mimeMap, StaticContentSection section) { if (mimeMap == null) { throw new ArgumentNullException("mimeMap"); } MimeMapCollection collection = section.MimeMaps; MimeMap element = collection[mimeMap.FileExtension]; if (element == null) { return; } try { collection.Remove(mimeMap.FileExtension); } catch (FileLoadException e) { throw new LockedException(section.SectionPath, e); } catch (DirectoryNotFoundException e) { throw new ConfigScopeNotFoundException(e); } }
public static void UpdateFeatureSettings(dynamic model, StaticContentSection section) { if (model == null) { throw new ApiArgumentException("model"); } if (section == null) { throw new ArgumentException("section"); } if (model.client_cache != null) { dynamic cache = model.client_cache; DynamicHelper.If((object)cache.max_age, 0, (long)TimeSpan.MaxValue.TotalMinutes, v => section.ClientCache.CacheControlMaxAge = TimeSpan.FromMinutes(v)); DynamicHelper.If((object)cache.control_mode, v => section.ClientCache.CacheControlMode = JsonToCacheControlMode(v)); DynamicHelper.If((object)cache.control_custom, v => section.ClientCache.CacheControlCustom = v); DynamicHelper.If <bool>((object)cache.set_e_tag, v => section.ClientCache.SetETag = v); DynamicHelper.If((object)cache.http_expires, v => { DateTime httpExpires; if (!DateTime.TryParseExact(v, "r", CultureInfo.InvariantCulture, DateTimeStyles.AllowWhiteSpaces, out httpExpires)) { throw new ApiArgumentException("http_expires"); } section.ClientCache.HttpExpires = httpExpires; }); } DynamicHelper.If((object)model.default_doc_footer, v => section.DefaultDocFooter = v); DynamicHelper.If <bool>((object)model.is_doc_footer_file_name, v => { if (section.IsDocFooterFileName != v) { section.IsDocFooterFileName = v; } }); DynamicHelper.If <bool>((object)model.enable_doc_footer, v => section.EnableDocFooter = v); if (model.metadata != null) { DynamicHelper.If <OverrideMode>((object)model.metadata.override_mode, v => section.OverrideMode = v); } }
public static MimeMap UpdateMimeMap(dynamic model, MimeMap mimeMap, StaticContentSection section) { if (model == null) { throw new ArgumentNullException(nameof(model)); } if (mimeMap == null) { throw new ArgumentNullException(nameof(mimeMap)); } if (string.IsNullOrEmpty(mimeMap.FileExtension)) { throw new ArgumentNullException("mimeMap.FileExtension"); } string fileExtension = DynamicHelper.Value(model.file_extension); string mimeType = DynamicHelper.Value(model.mime_type); if (!string.IsNullOrEmpty(fileExtension)) { fileExtension = fileExtension.StartsWith(".") ? fileExtension : "." + fileExtension; if (!fileExtension.Equals(mimeMap.FileExtension, StringComparison.OrdinalIgnoreCase) && section.MimeMaps.Any(m => fileExtension.Equals(m.FileExtension, StringComparison.OrdinalIgnoreCase))) { throw new AlreadyExistsException("file_extension"); } } try { mimeMap.FileExtension = fileExtension ?? mimeMap.FileExtension; mimeMap.MimeType = mimeType ?? mimeMap.MimeType; } catch (FileLoadException e) { throw new LockedException(MimeTypesGlobals.StaticContentSectionName, e); } catch (DirectoryNotFoundException e) { throw new ConfigScopeNotFoundException(e); } return(mimeMap); }
public void Delete(string id) { StaticContentId staticContentId = new StaticContentId(id); Context.Response.StatusCode = (int)HttpStatusCode.NoContent; Site site = (staticContentId.SiteId != null) ? SiteHelper.GetSite(staticContentId.SiteId.Value) : null; if (site == null) { return; } StaticContentSection section = StaticContentHelper.GetSection(site, staticContentId.Path, ManagementUnit.ResolveConfigScope()); section.RevertToParent(); ManagementUnit.Current.Commit(); }
public object Post([FromBody] dynamic model) { if (model == null) { throw new ApiArgumentException("model"); } if (model.static_content == null) { throw new ApiArgumentException("static_content"); } if (!(model.static_content is JObject)) { throw new ApiArgumentException("static_content"); } string staticContentUuid = DynamicHelper.Value(model.static_content.id); if (staticContentUuid == null) { throw new ApiArgumentException("static_content.id"); } // Get the feature id StaticContentId staticContentId = new StaticContentId(staticContentUuid); Site site = staticContentId.SiteId == null ? null : SiteHelper.GetSite(staticContentId.SiteId.Value); string configPath = ManagementUnit.ResolveConfigScope(model); StaticContentSection section = StaticContentHelper.GetSection(site, staticContentId.Path, configPath); // Create mime map MimeMap mimeMap = MimeMapHelper.CreateMimeMap(model, section); // Add it to the config file MimeMapHelper.AddMimeMap(mimeMap, section); // Save changes ManagementUnit.Current.Commit(); // Show new mime map return(MimeMapHelper.ToJsonModel(mimeMap, site, staticContentId.Path)); }
public async Task Delete(string id) { StaticContentId staticContentId = new StaticContentId(id); Context.Response.StatusCode = (int)HttpStatusCode.NoContent; Site site = (staticContentId.SiteId != null) ? SiteHelper.GetSite(staticContentId.SiteId.Value) : null; if (site != null) { StaticContentSection section = StaticContentHelper.GetSection(site, staticContentId.Path, ManagementUnit.ResolveConfigScope()); section.RevertToParent(); ManagementUnit.Current.Commit(); } if (staticContentId.SiteId == null && StaticContentHelper.IsFeatureEnabled()) { await StaticContentHelper.SetFeatureEnabled(false); } }
public object Patch(string id, [FromBody] dynamic model) { StaticContentId staticContentId = new StaticContentId(id); Site site = staticContentId.SiteId == null ? null : SiteHelper.GetSite(staticContentId.SiteId.Value); if (staticContentId.SiteId != null && site == null) { return(NotFound()); } // Check for config_scope string configPath = model == null ? null : ManagementUnit.ResolveConfigScope(model);; StaticContentSection section = StaticContentHelper.GetSection(site, staticContentId.Path, configPath); StaticContentHelper.UpdateFeatureSettings(model, section); ManagementUnit.Current.Commit(); return(StaticContentHelper.ToJsonModel(site, staticContentId.Path)); }
public static void AddMimeMap(MimeMap mimeMap, StaticContentSection section) { if (mimeMap == null) { throw new ArgumentNullException("mimeMap"); } MimeMapCollection collection = section.MimeMaps; if (collection[mimeMap.FileExtension] != null) { throw new AlreadyExistsException("file_extension"); } try { collection.Add(mimeMap); } catch (FileLoadException e) { throw new LockedException(section.SectionPath, e); } catch (DirectoryNotFoundException e) { throw new ConfigScopeNotFoundException(e); } }