Cloud Stack Ninja

I want to support partial requests for a file using FtpWebRequests. Is there a way to define a range just like with HttpWebRequests. I only found ContentOffset for a initial position. If its not possible, is it possible to cut the response stream after a given amount of bytes:

Here some code to clearify to usage and to mention I cannot just stop reading out bytes after a given amount of bytes:

namespace GDL.Protocols {
    public class FtpProtocolProvider : ProtocolProvider {
        /// <summary>
        /// Creates a stream to the resource, specified by its <see cref="ResourceIdentifier"/>.
        /// </summary>
        /// <param name="resourceIdentifier">The identifier of the resource.</param>
        /// <param name="initialPosition">The offset of the stream.</param>
        /// <param name="endPosition">The end position of the stream.</param>
        /// <returns>The created stream.</returns>
        public override Stream CreateStream(ResourceIdentifier resourceIdentifier, long initialPosition, long endPosition) {
            FtpWebRequest request = CreateRequest(resourceIdentifier) as FtpWebRequest;

            request.Method = WebRequestMethods.Ftp.DownloadFile;

            if (initialPosition > 0)
                request.ContentOffset = initialPosition;

            return request.GetResponse().GetResponseStream();
        }
    }
}

Here tests to show usage:

namespace GDL.IntegrationTests {
    [TestClass]
    public class FtpProtocolProviderTests {
        [TestMethod]
        public void CreateStream_FtpTestTxt_ReturnsValidStream() {
            string url = "ftp://test-domain.com/public/provider-test/test.txt";
            ResourceIdentifier resourceIdentifier = new ResourceIdentifier(new Uri(url), new FtpTestNetworkCredential());

            ProtocolProvider provider = ProtocolProviderFactory.CreateProvider(url);
            Stream stream = provider.CreateStream(resourceIdentifier, 0, 0);

            StreamReader reader = new StreamReader(stream);
            Assert.IsTrue(reader.ReadToEnd().Equals("If this text was read out, the stream works!"));
        }

        [TestMethod]
        public void CreateStream_FtpTestTxtInitialPositionSet_ReturnsValidStream() {
            string url = "ftp://test-domain.com/public/provider-test/test.txt";
            ResourceIdentifier resourceIdentifier = new ResourceIdentifier(new Uri(url), new FtpTestNetworkCredential());

            ProtocolProvider provider = ProtocolProviderFactory.CreateProvider(url);
            Stream stream = provider.CreateStream(resourceIdentifier, 3, 0);

            StreamReader reader = new StreamReader(stream);
            Assert.IsTrue(reader.ReadToEnd().Equals("this text was read out, the stream works!"));
        }

        [TestMethod]
        public void CreateStream_FtpTestTxtEndPositionSet_ReturnsValidStream() {
            string url = "ftp://test-domain.com/public/provider-test/test.txt";
            ResourceIdentifier resourceIdentifier = new ResourceIdentifier(new Uri(url), new FtpTestNetworkCredential());

            ProtocolProvider provider = ProtocolProviderFactory.CreateProvider(url);
            Stream stream = provider.CreateStream(resourceIdentifier, 0, 37);

            StreamReader reader = new StreamReader(stream);
            Assert.IsTrue(reader.ReadToEnd().Equals("If this text was read out, the stream"));  //<---- this should be possible.
        }
    }
}

Edit:

Unfortunately, the suggested answer does not help me. As I said above the code, I dont want to abort after a given amount of bytes. The architecture of my library defines that for a given url the ProtocolProviderFactory creates a suiting ProtocolProvider to get the information about the file and the stream to the data. The actual download is independent of different kinds of data transmission protocols. It just needs a stream to the data. Providing a valid stream is the task of an ProtocolProvider.



Read more here: https://stackoverflow.com/questions/64398290/c-sharp-ftp-cut-stream-at-defined-end-position

Content Attribution

This content was originally published by Matze at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: