示例#1
0
        public CacheableDataProvider(string storagePath)
        {
            Debug.WriteLine($"{nameof(storagePath)}: [{storagePath}]");

            StoragePath = storagePath;
            PortableDirectory.CreateDirectory(storagePath);
        }
示例#2
0
        /// <inheritdoc />
        public Stream FetchData(string url)
        {
            var cachedFilePath       = Path.Combine(StoragePath, GetCachedFileName(url));
            var cachedFilePathExists = PortableFile.Exists(cachedFilePath);

            if ((FetchMode == FetchMode.Online) ||
                ((FetchMode == FetchMode.OnlineIfMissing) && !cachedFilePathExists))
            {
                var directoryName = Path.GetDirectoryName(cachedFilePath);
                if (!String.IsNullOrWhiteSpace(directoryName))
                {
                    PortableDirectory.CreateDirectory(directoryName);
                }

                using (var binaryReader = new BinaryReader(dataProvider.FetchData(url)))
                {
                    using (var fileStream = PortableFile.Create(cachedFilePath))
                    {
                        binaryReader.BaseStream.CopyTo(fileStream);
                    }
                }
            }

            cachedFilePathExists = PortableFile.Exists(cachedFilePath);

            if (!cachedFilePathExists)
            {
                throw new FileNotInCacheException();
            }
            return(PortableFile.Open(cachedFilePath, PortableFileMode.Open));
        }
        public CacheableDataProvider()
        {
#if PORTABLE
            PortableDirectory.CreateDirectory(@"cache");
#else
            Directory.CreateDirectory(@"cache");
#endif
        }
        public Stream FetchData(string url)
        {
            var cachedFilePath = GetCacheStoragePath() + GetCachedFileName(url);

#if PORTABLE
            var cachedFilePathExists = PortableFile.Exists(cachedFilePath);
#else
            var cachedFilePathExists = File.Exists(cachedFilePath);
#endif

            if ((FetchMode == FetchMode.Online) ||
                ((FetchMode == FetchMode.OnlineIfMissing) && !cachedFilePathExists))
            {
                var directoryName = Path.GetDirectoryName(cachedFilePath);
                if (!String.IsNullOrWhiteSpace(directoryName))
                {
#if PORTABLE
                    PortableDirectory.CreateDirectory(directoryName);
#else
                    Directory.CreateDirectory(directoryName);
#endif
                }

                using (var binaryReader = new BinaryReader(dataProvider.FetchData(url)))
                {
#if PORTABLE
                    using (var fileStream = PortableFile.Create(cachedFilePath))
#else
                    using (var fileStream = File.Create(cachedFilePath))
#endif
                    {
                        binaryReader.BaseStream.CopyTo(fileStream);
                    }
                }
            }

#if PORTABLE
            cachedFilePathExists = PortableFile.Exists(cachedFilePath);
#else
            cachedFilePathExists = File.Exists(cachedFilePath);
#endif

            if (!cachedFilePathExists)
            {
                throw new FileNotInCacheException();
            }
#if PORTABLE
            return(PortableFile.Open(cachedFilePath, PortableFileMode.Open));
#else
            return(new FileStream(cachedFilePath, FileMode.Open));
#endif
        }
 public CacheableDataProvider(string storagePath)
 {
     StoragePath = storagePath;
     PortableDirectory.CreateDirectory(storagePath);
 }