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 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 static object ToJsonModelRef(MimeMap mimeMap, Site site, string path, Fields fields = null)
 {
     if (fields == null || !fields.HasFields)
     {
         return(ToJsonModel(mimeMap, site, path, RefFields, false));
     }
     else
     {
         return(ToJsonModel(mimeMap, site, path, fields, false));
     }
 }
        public object Get(string id)
        {
            var mimeMapId = new MimeMapId(id);

            Site site = mimeMapId.SiteId == null ? null : SiteHelper.GetSite(mimeMapId.SiteId.Value);

            List <MimeMap> mimeMaps = MimeMapHelper.GetMimeMaps(site, mimeMapId.Path);

            MimeMap mimeMap = mimeMaps.FirstOrDefault(m => m.FileExtension.Equals(mimeMapId.FileExtension));

            if (mimeMap == null)
            {
                return(NotFound());
            }

            return(MimeMapHelper.ToJsonModel(mimeMap, site, mimeMapId.Path));
        }
        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 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));
        }
        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 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);
            }
        }
        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);
        }