Ejemplo n.º 1
0
        public void Test(string mediaType, string mediaSubType, string fileName, bool gZipCalled, bool zipCalled, bool gzipSuccess = true, bool zipSuccess = true, bool success = true)
        {
            MimePart mimePart = CreateMimePart(mediaType, mediaSubType, fileName);

            A.CallTo(() => _contentTypeProvider.GetContentType(A <MimePart> ._)).Returns($@"{mediaType}/{mediaSubType}");

            if (!gzipSuccess)
            {
                A.CallTo(() => _gZipDecompressor.Decompress(A <Stream> ._)).Throws <Exception>();
            }

            if (!zipSuccess)
            {
                A.CallTo(() => _zipDecompressor.Decompress(A <Stream> ._)).Throws <Exception>();
            }

            AttachmentInfo attachment = _attachmentStreamNormaliser.Normalise(mimePart);

            A.CallTo(() => _gZipDecompressor.Decompress(A <Stream> ._)).MustHaveHappened(gZipCalled ? Repeated.Exactly.Once : Repeated.Never);
            A.CallTo(() => _zipDecompressor.Decompress(A <Stream> ._)).MustHaveHappened(zipCalled ? Repeated.Exactly.Once : Repeated.Never);

            if (success)
            {
                Assert.That(attachment, Is.Not.Null);
                Assert.That(attachment.AttachmentMetadata.Filename, Is.EqualTo(mimePart.FileName));
            }
            else
            {
                Assert.That(attachment, Is.EqualTo(AttachmentInfo.EmptyAttachmentInfo));
            }
        }
Ejemplo n.º 2
0
        public void ContentTypeApplicationGzipDecompressesAsGzip()
        {
            MimePart mimePart = CreateMimePart("application", "gzip", "test.gz");

            AttachmentInfo attachment = _attachmentStreamNormaliser.Normalise(mimePart);

            A.CallTo(() => _gZipDecompressor.Decompress(A <Stream> ._)).MustHaveHappened(Repeated.Exactly.Once);

            Assert.That(attachment, Is.Not.Null);
            Assert.That(attachment.AttachmentMetadata.Filename, Is.EqualTo(mimePart.FileName));
        }
Ejemplo n.º 3
0
        public AttachmentInfo Normalise(MimePart mimePart)
        {
            string contentType = mimePart.ContentType.MimeType.ToLower();

            switch (contentType)
            {
            case ContentType.ApplicationGzip:
                return(new AttachmentInfo(new AttachmentMetadata(mimePart.FileName), _gZipDecompressor.Decompress(GetDecodedStream(mimePart))));

            case ContentType.ApplicationZip:
                return(new AttachmentInfo(new AttachmentMetadata(mimePart.FileName), _zipDecompressor.Decompress(GetDecodedStream(mimePart))));

            case ContentType.ApplicationOctetStream:
                string extension = Path.GetExtension(mimePart.FileName.Split('!').LastOrDefault() ?? string.Empty);
                if (extension == ".gz")
                {
                    return(new AttachmentInfo(new AttachmentMetadata(mimePart.FileName), _gZipDecompressor.Decompress(GetDecodedStream(mimePart))));
                }
                if (extension == ".zip")
                {
                    return(new AttachmentInfo(new AttachmentMetadata(mimePart.FileName), _zipDecompressor.Decompress(GetDecodedStream(mimePart))));
                }
                if (extension == string.Empty)
                {
                    _log.Info($"No extension for {mimePart.FileName}, trying to decompress as gzip.");
                    try
                    {
                        return(new AttachmentInfo(new AttachmentMetadata(mimePart.FileName), _gZipDecompressor.Decompress(GetDecodedStream(mimePart))));
                    }
                    catch (Exception)
                    {
                        _log.Info($"Failed to decompress {mimePart.FileName} as gzip trying to decode as zip.");
                        try
                        {
                            return(new AttachmentInfo(new AttachmentMetadata(mimePart.FileName), _zipDecompressor.Decompress(GetDecodedStream(mimePart))));
                        }
                        catch (Exception)
                        {
                            _log.Info($"Ignoring {mimePart.FileName} as failed to decompress as gzip or zip.");
                        }
                    }
                }

                _log.Warn($"Ignoring {mimePart.FileName}, unknown file type {(extension)}.");
                return(AttachmentInfo.EmptyAttachmentInfo);

            case ContentType.ApplicationXZipCompressed:
                return(new AttachmentInfo(new AttachmentMetadata(mimePart.FileName), _zipDecompressor.Decompress(GetDecodedStream(mimePart))));

            case ContentType.TextXml:
                return(new AttachmentInfo(new AttachmentMetadata(mimePart.FileName), GetDecodedStream(mimePart)));

            default:
                return(AttachmentInfo.EmptyAttachmentInfo);
            }
        }