/// <summary> /// Initializes a new instance of the <see cref="RemoteFileSubscriptionStreamReader"/> class. /// </summary> /// <param name="dataCacheProvider">The <see cref="IDataCacheProvider"/> used to retrieve a stream of data</param> /// <param name="source">The remote url to be downloaded via web client</param> /// <param name="downloadDirectory">The local directory and destination of the download</param> /// <param name="headers">Defines header values to add to the request</param> public RemoteFileSubscriptionStreamReader(IDataCacheProvider dataCacheProvider, string source, string downloadDirectory, IEnumerable <KeyValuePair <string, string> > headers) { // create a hash for a new filename var filename = Guid.NewGuid() + source.GetExtension(); var destination = Path.Combine(downloadDirectory, filename); using (var client = new WebClient()) { client.Proxy = WebRequest.GetSystemWebProxy(); if (headers != null) { foreach (var header in headers) { client.Headers.Add(header.Key, header.Value); } } client.DownloadFile(source, destination); } // Send the file to the dataCacheProvider so it is available when the streamReader asks for it dataCacheProvider.Store(destination, File.ReadAllBytes(destination)); // now we can just use the local file reader _streamReader = new LocalFileSubscriptionStreamReader(dataCacheProvider, destination); }
/// <summary> /// Initializes a new instance of the <see cref="RemoteFileSubscriptionStreamReader"/> class. /// </summary> /// <param name="dataCacheProvider">The <see cref="IDataCacheProvider"/> used to retrieve a stream of data</param> /// <param name="source">The remote url to be downloaded via web client</param> /// <param name="downloadDirectory">The local directory and destination of the download</param> /// <param name="headers">Defines header values to add to the request</param> public RemoteFileSubscriptionStreamReader(IDataCacheProvider dataCacheProvider, string source, string downloadDirectory, IEnumerable <KeyValuePair <string, string> > headers) { // create a hash for a new filename var filename = Guid.NewGuid() + source.GetExtension(); var destination = Path.Combine(downloadDirectory, filename); var contents = _downloader.Download(source, headers, null, null); File.WriteAllText(destination, contents); // Send the file to the dataCacheProvider so it is available when the streamReader asks for it dataCacheProvider.Store(destination, System.Text.Encoding.UTF8.GetBytes(contents)); // now we can just use the local file reader _streamReader = new LocalFileSubscriptionStreamReader(dataCacheProvider, destination); }
/// <summary> /// Initializes a new instance of the <see cref="RemoteFileSubscriptionStreamReader"/> class. /// </summary> /// <param name="dataCacheProvider">The <see cref="IDataCacheProvider"/> used to retrieve a stream of data</param> /// <param name="source">The remote url to be downloaded via web client</param> /// <param name="downloadDirectory">The local directory and destination of the download</param> /// <param name="headers">Defines header values to add to the request</param> public RemoteFileSubscriptionStreamReader(IDataCacheProvider dataCacheProvider, string source, string downloadDirectory, IEnumerable <KeyValuePair <string, string> > headers) { // don't use cache if data is ephemeral // will be false for live history requests and live subscriptions var useCache = !dataCacheProvider.IsDataEphemeral; // create a hash for a new filename var filename = (useCache ? source.ToMD5() : Guid.NewGuid().ToString()) + source.GetExtension(); var destination = Path.Combine(downloadDirectory, filename); string contents = null; if (useCache) { lock (_fileSystemLock) { if (!File.Exists(destination)) { contents = _downloader.Download(source, headers, null, null); File.WriteAllText(destination, contents); } } } else { contents = _downloader.Download(source, headers, null, null); File.WriteAllText(destination, contents); } if (contents != null) { // Send the file to the dataCacheProvider so it is available when the streamReader asks for it dataCacheProvider.Store(destination, System.Text.Encoding.UTF8.GetBytes(contents)); } // now we can just use the local file reader _streamReader = new LocalFileSubscriptionStreamReader(dataCacheProvider, destination); }
private void ReadAndWrite(IDataCacheProvider dataCacheProvider, byte[] data) { dataCacheProvider.Fetch(_tempZipFileEntry); dataCacheProvider.Store(_tempZipFileEntry, data); }