Example #1
0
        public Stream NormaliseStream(MimePart mimePart)
        {
            string contentType = _contentTypeProvider.GetContentType(mimePart);

            switch (contentType)
            {
            case ContentType.ApplicationGzip:
                return(Decompress(mimePart, _gZipDecompressor, _zipDecompressor));

            case ContentType.ApplicationXZipCompressed:
            case ContentType.ApplicationZip:
                return(Decompress(mimePart, _zipDecompressor, _gZipDecompressor));

            case ContentType.ApplicationOctetStream:
                string extension = Path.GetExtension(mimePart.FileName.Split('!').LastOrDefault());
                if (extension?.StartsWith(".z") ?? false)
                {
                    return(Decompress(mimePart, _zipDecompressor, _gZipDecompressor));
                }
                return(Decompress(mimePart, _gZipDecompressor, _zipDecompressor));

            case ContentType.TextXml:
                return(mimePart.GetDecodedStream());

            default:
                return(Stream.Null);
            }
        }
Example #2
0
        private Stream Decompress(MimePart mimePart, params IDecompressor[] decompressors)
        {
            foreach (var decompressor in decompressors)
            {
                try
                {
                    using (Stream decodedStream = mimePart.GetDecodedStream())
                    {
                        Stream decompressedStream = decompressor.Decompress(decodedStream);

                        _log.Info(
                            $"Successfully decompressed {mimePart.FileName ?? "<null>"} as {decompressor.StreamType}.");

                        return(decompressedStream);
                    }
                }
                catch (Exception)
                {
                    _log.Info($"Failed to decompress {mimePart.FileName ?? "<null>"} as {decompressor.StreamType}.");
                }
            }
            return(Stream.Null);
        }