Exemplo n.º 1
0
        private static async Task StartDownloadAsync(ConfigFile config)
        {
            var appSettings              = ApplicationService.GetAppSettings();
            var datasetService           = new DatasetService(config);
            var updatedDatasetToDownload = new List <Download>();
            var downloadLog              = new DownloadLog();
            var downloader        = new FileDownloader();
            var datasetToDownload = datasetService.GetSelectedFilesToDownload();
            var downloadUsage     = config.DownloadUsage;

            downloadLog.TotalDatasetsToDownload = datasetToDownload.Count;

            foreach (var localDataset in datasetToDownload)
            {
                var updatedDatasetFileToDownload = new List <DatasetFile>();

                if (localDataset.Subscribe)
                {
                    Log.Information("Subscribe to Dataset files");
                    var datasetFilesFromFeed = datasetService.GetDatasetFiles(localDataset);

                    var filterDatasetFromFeed = datasetFilesFromFeed.Where(p => localDataset.Projections.Where(s => s.Selected == false).All(p2 => p2.Epsg != p.Projection)).ToList();

                    if (localDataset.AutoDeleteFiles)
                    {
                        Log.Debug("Delete files");
                        localDataset.Files = RemoveFiles(filterDatasetFromFeed, localDataset.Files, config);
                    }

                    if (localDataset.AutoAddFiles)
                    {
                        Log.Debug("Add new files");
                        localDataset.Files = AddFiles(filterDatasetFromFeed, localDataset.Files);
                    }
                }

                foreach (var datasetFile in localDataset.Files)
                {
                    var fileLog = new DatasetFileLog(datasetFile);

                    try
                    {
                        Console.WriteLine(datasetFile.DatasetId + " - " + datasetFile.Title);

                        DirectoryInfo   downloadDirectory   = GetDownloadDirectory(config, datasetFile);
                        DatasetFile     datasetFromFeed     = datasetService.GetDatasetFile(datasetFile);
                        DownloadHistory downloadHistory     = datasetService.GetFileDownloaHistory(datasetFile.Url);
                        bool            newDatasetAvailable = NewDatasetAvailable(downloadHistory, datasetFromFeed, downloadDirectory);

                        if (newDatasetAvailable)
                        {
                            Console.WriteLine("Updated version of dataset is available.");
                            Console.WriteLine("Starting download process.");
                            downloader.ProgressChanged += (totalFileSize, totalBytesDownloaded, progressPercentage) =>
                            {
                                Console.CursorLeft = 0;
                                Console.Write($"{progressPercentage}% ({HumanReadableBytes(totalBytesDownloaded)}/{HumanReadableBytes(totalFileSize.Value)})                "); // add som extra whitespace to blank out previous updates
                            };

                            var downloadRequest = new DownloadRequest(datasetFile.Url, downloadDirectory, datasetFile.IsRestricted());
                            datasetFile.FilePath = await downloader.StartDownload(downloadRequest, appSettings);

                            downloadLog.Updated.Add(fileLog);

                            Console.WriteLine();

                            downloadUsage?.Entries.Add(new DownloadUsageEntries(datasetFile));
                            updatedDatasetFileToDownload.Add(datasetFile);
                        }
                        else
                        {
                            fileLog.Message = "Not necessary to download dataset." + datasetFromFeed.LastUpdated;
                            downloadLog.NotUpdated.Add(fileLog);
                            Console.WriteLine("Not necessary to download dataset.");
                            datasetFile.FilePath = downloadHistory.FilePath;
                            updatedDatasetFileToDownload.Add(datasetFile);
                        }
                        datasetFile.DownloadSuccess = true;
                    }

                    catch (Exception e)
                    {
                        Log.Error(e, "Error while downloading file " + datasetFile.Title);
                        updatedDatasetFileToDownload.Add(datasetFile);
                        fileLog.Message = "Error while downloading dataset: " + e.Message;
                        downloadLog.Faild.Add(fileLog);
                        Console.WriteLine("Error while downloading dataset: " + e.Message);
                        datasetFile.DownloadSuccess = true;
                    }

                    Console.WriteLine("-------------");
                }
                updatedDatasetToDownload.Add(localDataset);
            }

            Log.Information("Send download usage");
            datasetService.SendDownloadUsage(downloadUsage);
            Log.Information("Write to config file");
            datasetService.WriteToConfigFile(updatedDatasetToDownload);
            Log.Information("Write to download history file");
            datasetService.WriteToDownloadHistoryFile(updatedDatasetToDownload);
            Log.Information("Write to download log file");
            datasetService.WriteToDownloadLogFile(downloadLog);
        }