Is it possible to download a .txt file and manipulate said file in one endpoint using ASP.NET MVC controller?

Viewed 50

My task is to download a .txt file, remove some of the data and save it as .json. Currently I use one interface to download a file as .txt file and another interface to read all lines of said file, make it into an object and remove what I don't need.

Currently what happens is, that I have to go to download endpoint to get my .txt then to edit endpoint to parse and save as .json.

This is the filter method:

    public void FilterOutInvalidRates(string path)
    {
        try
        {
            var targetLocation = @"TargetLocation/";
            var name = File.ReadAllLines(path).First();
            var fileName = name.Substring(0,3);
            var lines = File.ReadAllLines(path).Skip(2);

            var model = lines.Select(p => new Rates
            {
                a = p.Split("|")[0],
                b = p.Split("|")[1],
                c = p.Split("|")[2],
                d = p.Split("|")[3],
                e = p.Split("|")[4],
            });

            List<Rates> rates = model.Where(model => IsTheCountryValid(model.Country)).ToList();

            var jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(rates.ToArray(), Formatting.Indented);

            System.IO.File.WriteAllText(targetLocation + fileName + ".json", jsonString);

            System.IO.File.Delete(path);
        }
        catch (FileNotFoundException)
        {
            Console.WriteLine(@"Unable to read file:" + path.ToString());
        }
    }

And this is the download service:

    public async void DownloadRatesTxtFile(string uri, string outputPath, int number)
    {
        var policy = BuildRetryPolicy();
        var path = await policy.ExecuteAsync(() => uri
        .DownloadFileAsync(outputPath, @"CurrencyRate" + number + ".txt"));
    }

This is the controller implementation - I know it's not ideal, if I could do it all in one endpoint or a different architecture I could get rid of a ton of code. The issue is, that before the endpoint returns OK I am not able to manipulate the files in any way or read them.

    [HttpGet("download")]
    public async Task<IActionResult> Download()
    {
        var fileCounter = 1;
        var outputPath2 = AppDomain.CurrentDomain.BaseDirectory + @"Data/";
        var outputPath = @"TargetLocation/";
        DateTime begindate = Convert.ToDateTime("01/07/2022");
        DateTime enddate = Convert.ToDateTime("5/07/2022");
        
        while (begindate < enddate)
        {
            if (begindate.DayOfWeek == DayOfWeek.Saturday)
            {
                begindate = begindate.AddDays(2);
            }
            _exchangeRateConnector.DownloadRatesTxtFile(_exchangeRateConnector.GenerateRatesUrl(begindate), outputPath2, fileCounter);
            begindate = begindate.AddDays(1);
            fileCounter++;
        }

        return Ok();
    }

    [HttpGet("edit")]
    public async Task<IActionResult> Edit()
    {
        var outputPath2 = AppDomain.CurrentDomain.BaseDirectory + @"Data/";
        var outputPath = @"TargetLocation/";
        var fileCount = (from file in Directory.EnumerateFiles(outputPath2, "*.txt", SearchOption.TopDirectoryOnly)
                         select file).Count();

        for (int i = 1; i <= fileCount; i++)
        {
            _exchangeRateRepository.FilterOutInvalidRates(outputPath2 + "CurrencyRate" + i + ".txt");
        }

        return Ok();
    }

Thank you so much for any sort of advice.

0 Answers
Related