コード例 #1
0
    /// <summary>
    /// Registers file extension mapping.
    /// </summary>
    /// <param name="json">The mapping source.</param>
    /// <param name="overrideIfExist">true if override the existed one; otherwise, false.</param>
    /// <returns>The count of item added or changed.</returns>
    public static int RegisterFileExtensionMapping(JsonObjectNode json, bool overrideIfExist = false)
    {
        var mapping = FileExtensionMapping;

        if (json == null || mapping == null)
        {
            return(0);
        }
        var arr = json.TryGetArrayValue("mime");

        if (arr != null)
        {
            return(RegisterFileExtensionMapping(arr, overrideIfExist));
        }
        var body = json.TryGetObjectValue("mime");

        if (body != null)
        {
            json = body;
        }
        var i = 0;

        foreach (var item in json)
        {
            if (item.Value?.ValueKind != JsonValueKind.String)
            {
                continue;
            }
            if (!item.Value.TryGetString(out var s))
            {
                continue;
            }
            if (mapping.Set(item.Key, s, overrideIfExist))
            {
                i++;
            }
        }

        return(i);
    }