public void MultiPartFileShouldBeDecoded()
        {
            const string expectedFileName = "joystick.jpg";

            byte[] expected = testData.GetEmbeddedFile(@"yenc.multipart.joystick.jpg").ReadAllBytes();

            YencStream part1 = YencStreamDecoder.Decode(
                testData.GetEmbeddedFile(@"yenc.multipart.00000020.ntx").ReadAllLines(UsenetEncoding.Default));
            YencStream part2 = YencStreamDecoder.Decode(
                testData.GetEmbeddedFile(@"yenc.multipart.00000021.ntx").ReadAllLines(UsenetEncoding.Default));

            using (var actual = new MemoryStream())
            {
                actual.Seek(part1.Header.PartOffset, SeekOrigin.Begin);
                part1.CopyTo(actual);

                actual.Seek(part2.Header.PartOffset, SeekOrigin.Begin);
                part2.CopyTo(actual);

                string actualFileName = part1.Header.FileName;

                Assert.Equal(expectedFileName, actualFileName);
                Assert.Equal(expected, actual.ToArray());
            }
        }
Esempio n. 2
0
        public void FilePartShouldBeDecoded()
        {
            const int  expectedDataLength = 11250;
            YencStream actualStream       = YencStreamDecoder.Decode(
                testData.GetEmbeddedFile(@"yenc.multipart.00000020.ntx").ReadAllLines(UsenetEncoding.Default));

            byte[] actualData = actualStream.ReadAllBytes();

            Assert.True(actualStream.Header.IsFilePart);
            Assert.Equal(expectedDataLength, actualData.Length);
        }
Esempio n. 3
0
        private static void TestDownloadNzbStreaming(NntpClient client, string nzbFileName)
        {
            string fullPath = Path.Combine(nzbFileName);
            string nzbData = File.ReadAllText(fullPath, UsenetEncoding.Default);
            NzbDocument nzbDocument = NzbParser.Parse(nzbData);

            string downloadDir = Path.Combine("downloads", nzbFileName);
            Directory.CreateDirectory(downloadDir);

            var sw = new Stopwatch();
            sw.Restart();

            log.LogInformation("Downloading nzb {nzbFileName}", nzbFileName);
            foreach (NzbFile file in nzbDocument.Files)
            {
                log.LogInformation("Downloading file {subject}", file.Subject);
                foreach (NzbSegment segment in file.Segments)
                {
                    log.LogInformation("Downloading article {messageId}", segment.MessageId);
                    NntpArticleResponse response = client.Article(segment.MessageId);
                    using (YencStream yencStream = YencStreamDecoder.Decode(response.Article.Body))
                    {
                        YencHeader header = yencStream.Header;

                        string fileName = Path.Combine(downloadDir, header.FileName);
                        if (!File.Exists(fileName))
                        {
                            log.LogInformation("Creating file {fileName}", fileName);
                            // create file and pre-allocate disk space for it
                            using (FileStream stream = File.Create(fileName))
                            {
                                stream.SetLength(header.FileSize);
                            }
                        }

                        log.LogInformation("Writing {size} bytes to file {fileName} at offset {offset}",
                            header.PartSize, fileName, header.PartOffset);

                        using (FileStream stream = File.Open(
                            fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite))
                        {
                            stream.Seek(header.PartOffset, SeekOrigin.Begin);
                            yencStream.CopyTo(stream);
                        }
                    }
                }
            }
            log.LogInformation("Nzb downloaded in {elapsed}", sw.Elapsed);
        }
Esempio n. 4
0
        public void SinglePartFileShouldBeDecoded()
        {
            byte[] expectedData = testData.GetEmbeddedFile(@"yenc.singlepart.testfile.txt").ReadAllBytes();

            YencStream actualStream = YencStreamDecoder.Decode(
                testData.GetEmbeddedFile(@"yenc.singlepart.00000005.ntx").ReadAllLines(UsenetEncoding.Default));

            byte[] actualData = actualStream.ReadAllBytes();

            Assert.False(actualStream.Header.IsFilePart);
            Assert.Equal(128, actualStream.Header.LineLength);
            Assert.Equal(584, actualStream.Header.FileSize);
            Assert.Equal("testfile.txt", actualStream.Header.FileName);
            Assert.Equal(584, actualData.Length);
            Assert.Equal(expectedData, actualData);
        }