public async Task <IActionResult> Download() { var model = new DownloadViewModel { Downloads = new List <DownloadViewModel.Download>() }; const string filePattern = "GPGData_????-????.csv"; foreach (var file in await SharedBusinessLogic.FileRepository.GetFilesAsync( SharedBusinessLogic.SharedOptions.DownloadsLocation, filePattern)) { var download = new DownloadViewModel.Download { Title = Path.GetFileNameWithoutExtension(file).AfterFirst("GPGData_"), Count = await SharedBusinessLogic.FileRepository.GetMetaDataAsync(file, "RecordCount"), Extension = Path.GetExtension(file).TrimI("."), Size = Numeric.FormatFileSize(await SharedBusinessLogic.FileRepository.GetFileSizeAsync(file)) }; download.Url = Url.Action("DownloadData", new { year = download.Title.BeforeFirst("-") }); model.Downloads.Add(download); } //Sort downloads by descending year model.Downloads = model.Downloads.OrderByDescending(d => d.Title).ToList(); //Return the view with the model return(View("Download", model)); }
public ActionResult Download() { //Get the latest return accounting date var returnYears = DataRepository.GetAll <Return>().Where(r => r.Status == ReturnStatuses.Submitted).Select(r => r.AccountingDate.Year).Distinct().ToList(); //Ensure we have a directory if (!MvcApplication.FileRepository.GetDirectoryExists(Settings.Default.DownloadsLocation)) { MvcApplication.FileRepository.CreateDirectory(Settings.Default.DownloadsLocation); } string filePattern; foreach (var year in returnYears) { //If another server is already in process of creating a file then skip filePattern = $"GPGData_{year}-{year + 1}_*.csv"; var file = MvcApplication.FileRepository.GetFiles(Settings.Default.DownloadsLocation, filePattern).FirstOrDefault(); //Skip if the file already exists and is newer than 1 hour or older than 1 year if (file != null) { var lastWriteTime = MvcApplication.FileRepository.GetLastWriteTime(file); if (lastWriteTime.AddHours(1) >= DateTime.Now || lastWriteTime.AddYears(1) <= DateTime.Now) { continue; } } var tempfile = new FileInfo(System.IO.Path.GetTempFileName()); try { var count = 0; using (var textWriter = tempfile.CreateText()) { var downloadData = DataRepository.GetAll <Return>().Where(r => r.AccountingDate.Year == year).OrderBy(r => r.Organisation.OrganisationName).ToList(); var records = downloadData.Where(r => !r.Organisation.OrganisationName.StartsWithI(MvcApplication.TestPrefix)).Select(r => r.ToDownloadRecord()); using (var writer = new CsvWriter(textWriter)) { writer.WriteRecords(records); } count = downloadData.Count; } try { //Generate a new file name var newFilePath = MvcApplication.FileRepository.GetFullPath(Path.Combine(Settings.Default.DownloadsLocation, $"GPGData_{year}-{year + 1}_{count}.csv")); MvcApplication.FileRepository.Write(newFilePath, tempfile); //Delete the old file if it exists if (file != null && MvcApplication.FileRepository.GetFileExists(file) && !file.EqualsI(newFilePath)) { MvcApplication.FileRepository.DeleteFile(file); } } catch (Exception ex) { MvcApplication.ErrorLog.WriteLine(ex.Message); } } finally { System.IO.File.Delete(tempfile.FullName); } } var model = new DownloadViewModel(); model.Downloads = new List <DownloadViewModel.Download>(); filePattern = $"GPGData_????-????_*.csv"; foreach (var file in MvcApplication.FileRepository.GetFiles(Settings.Default.DownloadsLocation, filePattern)) { var download = new DownloadViewModel.Download(); download.Title = Path.GetFileNameWithoutExtension(file).AfterFirst("GPGData_"); download.Count = download.Title.AfterLast("_"); download.Title = download.Title.BeforeLast("_"); download.Extension = Path.GetExtension(file).TrimI("."); download.Size = Numeric.FormatFileSize(MvcApplication.FileRepository.GetFileSize(file)); download.Url = Url.Action("DownloadData", new { year = download.Title.BeforeFirst("-") }); model.Downloads.Add(download); } //Sort downloads by descending year model.Downloads = model.Downloads.OrderByDescending(d => d.Title).ToList(); //Return the view with the model return(View("Download", model)); }