private void ConfigureStaticContent() { // Provide mvc with route Environment.Host.RouteBuilder.MapWebApiRoute(Defines.Resource.Guid, $"{Defines.PATH}/{{id?}}", new { controller = "staticcontent" }); // Register self hypermedia Environment.Hal.ProvideLink(Defines.Resource.Guid, "self", sc => new { href = StaticContentHelper.GetLocation(sc.id) }); // Web Server Environment.Hal.ProvideLink(WebServer.Defines.Resource.Guid, Defines.Resource.Name, _ => { var id = GetStaticContentId(null, null); return(new { href = StaticContentHelper.GetLocation(id.Uuid) }); }); // Site Environment.Hal.ProvideLink(Sites.Defines.Resource.Guid, Defines.Resource.Name, site => { var siteId = new SiteId((string)site.id); Site s = SiteHelper.GetSite(siteId.Id); var id = GetStaticContentId(s, "/"); return(new { href = StaticContentHelper.GetLocation(id.Uuid) }); }); // Application Environment.Hal.ProvideLink(Applications.Defines.Resource.Guid, Defines.Resource.Name, app => { var appId = new ApplicationId((string)app.id); Site s = SiteHelper.GetSite(appId.SiteId); var id = GetStaticContentId(s, appId.Path); return(new { href = StaticContentHelper.GetLocation(id.Uuid) }); }); }
public void Delete(string id) { var mimeMapId = new MimeMapId(id); Site site = mimeMapId.SiteId == null ? null : SiteHelper.GetSite(mimeMapId.SiteId.Value); if (mimeMapId.SiteId != null && site == null) { Context.Response.StatusCode = (int)HttpStatusCode.NoContent; return; } MimeMap mimeMap = MimeMapHelper.GetMimeMaps(site, mimeMapId.Path).FirstOrDefault(m => m.FileExtension.Equals(mimeMapId.FileExtension)); if (mimeMap != null) { var section = StaticContentHelper.GetSection(site, mimeMapId.Path, ManagementUnit.ResolveConfigScope()); MimeMapHelper.DeleteMimeType(mimeMap, section); ManagementUnit.Current.Commit(); } Context.Response.StatusCode = (int)HttpStatusCode.NoContent; }
public object Get(string id) { StaticContentId staticContentId = new StaticContentId(id); Site site = staticContentId.SiteId == null ? null : SiteHelper.GetSite(staticContentId.SiteId.Value); if (staticContentId.SiteId != null && site == null) { return(NotFound()); } return(StaticContentHelper.ToJsonModel(site, staticContentId.Path)); }
public async Task <object> Post() { if (StaticContentHelper.IsFeatureEnabled()) { throw new AlreadyExistsException(StaticContentHelper.DISPLAY_NAME); } await StaticContentHelper.SetFeatureEnabled(true); dynamic settings = StaticContentHelper.ToJsonModel(null, null); return(Created(StaticContentHelper.GetLocation(settings.id), settings)); }
public object Get() { Site site = ApplicationHelper.ResolveSite(); string path = ApplicationHelper.ResolvePath(); if (path == null) { return(NotFound()); } dynamic d = StaticContentHelper.ToJsonModel(site, path); return(LocationChanged(StaticContentHelper.GetLocation(d.id), d)); }
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); } }
internal static object ToJsonModel(MimeMap mimeMap, Site site, string path, Fields fields = null, bool full = true) { if (mimeMap == null) { return(null); } if (fields == null) { fields = Fields.All; } dynamic obj = new ExpandoObject(); // // file_extension if (fields.Exists("file_extension")) { obj.file_extension = mimeMap.FileExtension.TrimStart(new char[] { '.' }); } // // id obj.id = new MimeMapId(site?.Id, path, mimeMap.FileExtension).Uuid; // // mime_type if (fields.Exists("mime_type")) { obj.mime_type = mimeMap.MimeType; } // // static_content if (fields.Exists("static_content")) { obj.static_content = StaticContentHelper.ToJsonModelRef(site, path); } return(Core.Environment.Hal.Apply(Defines.MimeMapsResource.Guid, obj, full)); }
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 object Patch(string id, [FromBody] dynamic model) { var mimeMapId = new MimeMapId(id); Site site = mimeMapId.SiteId == null ? null : SiteHelper.GetSite(mimeMapId.SiteId.Value); string configPath = model == null ? null : ManagementUnit.ResolveConfigScope(model); List <MimeMap> mimeMaps = MimeMapHelper.GetMimeMaps(site, mimeMapId.Path, configPath); MimeMap mimeMap = mimeMaps.FirstOrDefault(m => m.FileExtension.Equals(mimeMapId.FileExtension)); if (mimeMap == null) { return(new NotFoundResult()); } if (model == null) { throw new ApiArgumentException("model"); } var section = StaticContentHelper.GetSection(site, mimeMapId.Path, configPath); MimeMapHelper.UpdateMimeMap(model, mimeMap, section); ManagementUnit.Current.Commit(); dynamic mm = MimeMapHelper.ToJsonModel(mimeMap, site, mimeMapId.Path); if (mm.id != id) { return(LocationChanged(MimeMapHelper.GetLocation(mm.id), mm)); } return(mm); }
public static List <MimeMap> GetMimeMaps(Site site, string path, string configPath = null) { var collection = StaticContentHelper.GetSection(site, path, configPath).MimeMaps; return(collection.ToList()); }
private StaticContentId GetStaticContentId(Site s, string path) { return(new StaticContentId(s?.Id, path, StaticContentHelper.IsSectionLocal(s, path))); }