Пример #1
0
        public static void LocalFileStore_Registers_Files()
        {
            // Arrange
            string dataFileName = "LocalFileStore.Test.eu";

            string hash = "7039d49e15fd4e164e2c07fe76fd61a2";
            string path = "MyFile.txt";

            try
            {
                using (LocalFileStore target = new LocalFileStore())
                {
                    // Act
                    Assert.False(target.ContainsFile(hash));
                    Assert.False(target.ContainsFile(hash.ToUpperInvariant()));
                }

                using (LocalFileStore target = new LocalFileStore(dataFileName))
                {
                    // Act
                    Assert.False(target.ContainsFile(hash));
                    target.RegisterFile(hash, path);
                }

                using (LocalFileStore target = new LocalFileStore(dataFileName))
                {
                    // Assert
                    Assert.True(target.ContainsFile(hash.ToUpperInvariant()));

                    string storedPath;
                    bool   result = target.TryGetFilePath(hash, out storedPath);

                    Assert.True(result);
                    Assert.Equal(Path.GetFullPath(path), storedPath);
                }
            }
            finally
            {
                File.Delete(dataFileName);
            }
        }
Пример #2
0
        /// <summary>
        /// Downloads the available data files from the Electronic Updates Metadata REST API as an asynchronous operation.
        /// </summary>
        /// <returns>
        /// A <see cref="Task"/> representing the asynchronous operation to download any data files.
        /// </returns>
        public static async Task MainAsync()
        {
            PrintBanner();

            // Create the service implementation
            MetadataApiFactory factory = new MetadataApiFactory(GetConfiguration());
            IMetadataApi       service = factory.CreateMetadataApi();

            // Get the configuration settings for downloading files
            string downloadRootPath      = factory.GetConfigSetting("downloadRootPath");
            string verifyDownloadsString = factory.GetConfigSetting("validateDownloads");

            bool verifyDownloads;

            if (!bool.TryParse(verifyDownloadsString, out verifyDownloads))
            {
                verifyDownloads = true;
            }

            if (string.IsNullOrEmpty(downloadRootPath))
            {
                downloadRootPath = "EDQData";
            }

            downloadRootPath = Path.GetFullPath(downloadRootPath);

            Console.WriteLine("Electronic Updates Metadata REST API: {0}", service.ServiceUri);
            Console.WriteLine();

            // Query the packages available to the account
            List <PackageGroup> response = await service.GetAvailablePackagesAsync();

            Console.WriteLine("Available Package Groups:");
            Console.WriteLine();

            // Enumerate the package groups and list their packages and files
            if (response != null && response.Count > 0)
            {
                using (CancellationTokenSource tokenSource = new CancellationTokenSource())
                {
                    // Cancel the tasks if Ctrl+C is entered to the console
                    Console.CancelKeyPress += (sender, e) =>
                    {
                        if (!tokenSource.IsCancellationRequested)
                        {
                            tokenSource.Cancel();
                        }

                        e.Cancel = true;
                    };

                    try
                    {
                        Stopwatch stopwatch = Stopwatch.StartNew();

                        // Create a file store in which to cache information about which files
                        // have already been downloaded from the Metadata API service.
                        using (IFileStore fileStore = new LocalFileStore())
                        {
                            foreach (PackageGroup group in response)
                            {
                                Console.WriteLine("Group Name: {0} ({1})", group.PackageGroupCode, group.Vintage);
                                Console.WriteLine();
                                Console.WriteLine("Packages:");
                                Console.WriteLine();

                                foreach (Package package in group.Packages)
                                {
                                    Console.WriteLine("Package Name: {0}", package.PackageCode);
                                    Console.WriteLine();
                                    Console.WriteLine("Files:");
                                    Console.WriteLine();

                                    foreach (DataFile file in package.Files)
                                    {
                                        if (fileStore.ContainsFile(file.MD5Hash))
                                        {
                                            // We already have this file, download not required
                                            Console.WriteLine("File with hash '{0}' already downloaded.", file.MD5Hash);
                                        }
                                        else
                                        {
                                            // Download the file
                                            await DownloadFileAsync(
                                                service,
                                                fileStore,
                                                group,
                                                file,
                                                downloadRootPath,
                                                verifyDownloads,
                                                tokenSource.Token);
                                        }
                                    }

                                    Console.WriteLine();
                                }

                                Console.WriteLine();
                            }
                        }

                        stopwatch.Stop();
                        Console.WriteLine("Downloaded data in {0:hh\\:mm\\:ss}.", stopwatch.Elapsed);
                    }
                    catch (OperationCanceledException ex)
                    {
                        // Only an error if not cancelled by the user
                        if (ex.CancellationToken != tokenSource.Token)
                        {
                            throw;
                        }

                        Console.WriteLine("File download cancelled by user.");
                    }

                    Console.WriteLine();
                }
            }
        }