Example #1
0
        /// <summary>
        /// Detect the mime type of file based by content with a fallback way by its extension
        /// </summary>
        /// <returns>instance of MimeMagicResult</returns>
        public static MimeMagicResult FindMimeType(this byte[] contentBytes, string filename = "")
        {
            var result = new MimeMagicResult()
            {
                MimeType = "application/octet-stream"
            };
            var watch = new Stopwatch();

            watch.Start();

            Initialize();

            try
            {
                var contentString = Encoding.UTF7.GetString(contentBytes, 0, Math.Min(_bufferLength, contentBytes.Length));

                var extension = "*" + Path.GetExtension(filename);

                // first, check all the base types
                var mimes = _allTypes;
                do
                {
                    // let's try to find by content
                    var found = mimes.Find(mime =>
                    {
                        return(EvaluateSingleMimeByContent(contentString, mime, result));
                    });

                    // fallback to its extension
                    if (found == null)
                    {
                        found = mimes.Find(mime =>
                        {
                            return(EvaluateSingleMimeByExtension(extension, mime, result));
                        });
                    }

                    // is there any sub type for this type
                    mimes = _allTypes.Where(p => p.SubClassOf != null && p.SubClassOf.Type == found?.Type).ToList();

                    // set the result
                    if (found != null)
                    {
                        result.Acronym  = string.IsNullOrEmpty(found?.ExpandedAcronym) ? found?.Comment : found.ExpandedAcronym;
                        result.MimeType = found?.Type;
                    }
                }while (mimes != null && mimes.Count > 0);

                return(result);
            }
            finally
            {
                watch.Stop();

                result.ElapsedtimeInMS = (int)watch.ElapsedMilliseconds;
            }
        }
Example #2
0
        private static bool EvaluateSingleMimeByExtension(string extension, MimeType mime, MimeMagicResult diagnostic)
        {
            var matching = false;

            // fallback to the file extension.
            if (!matching)
            {
                matching = mime.Globs.Any(ext => extension.Equals(ext.Pattern));

                if (matching)
                {
                    diagnostic.FoundByExtension = true;
                }
            }

            return(matching);
        }
Example #3
0
        /// <summary>
        /// According to its name
        /// </summary>
        private static bool EvaluateSingleMimeByContent(string content, MimeType mime, MimeMagicResult diagnostic)
        {
            var matching = false;

            // try to find the magic in the content
            if (mime.Magic != null && mime.Magic.Matches != null && mime.Magic.Matches.Count > 0)
            {
                matching = mime.Magic.Matches.Any(child => EvaluateSingleMatchRecursively(content, child));

                if (matching)
                {
                    diagnostic.FoundByContent = true;
                }
            }

            return(matching);
        }