Ejemplo n.º 1
0
        public async Task <IActionResult> Download(int id)
        {
            var item = await _sampleFramesService.GetById(id, User.GetUserId());

            if (item.Status == SampleFrameGenerationStatuses.GenerationCompleted || item.Status == SampleFrameGenerationStatuses.Downloaded)
            {
                try
                {
                    if (!System.IO.File.Exists(item.FilePath))
                    {
                        await _sampleFramesService.Edit(id, item, User.GetUserId());
                    }
                    else
                    {
                        var stream = new FileStream(item.FilePath, FileMode.Open);
                        await _sampleFramesService.SetAsDownloaded(id, User.GetUserId());

                        string regexSearch = new string(Path.GetInvalidFileNameChars()) + new string(Path.GetInvalidPathChars());
                        Regex  r           = new Regex(string.Format("[{0}]", Regex.Escape(regexSearch)));
                        var    filename    = r.Replace(item.Name, "");
                        if (string.IsNullOrWhiteSpace(filename))
                        {
                            filename = item.Id.ToString();
                        }
                        return(File(stream, "text/csv;charset=utf-8", filename + ".csv"));
                    }
                }
                catch (Exception e) {
                    throw new BadRequestException("Error occurred during file downloading. " + e.Message);
                }
            }

            return(Ok($"{Localization.GetString(nameof(Resource.FileDoesntExistOrInQueue))}"));
        }
Ejemplo n.º 2
0
        private async Task Edit()
        {
            using (var context = InMemoryDb.CreateDbContext())
            {
                context.Initialize();

                await CreateStatisticalUnitsAsync(context);

                var expressionTree = CreateExpressionGroup();

                var service = new SampleFramesService(context, null);
                await service.Create(
                    new SampleFrameM
                {
                    Name      = "Sample frame name",
                    Predicate = expressionTree,
                    Fields    = new[] { FieldEnum.Address }
                },
                    (await context.Users.FirstAsync()).Id);

                Assert.Equal(1, await context.SampleFrames.CountAsync());

                await service.Edit(
                    (await context.SampleFrames.FirstOrDefaultAsync()).Id,
                    new SampleFrameM
                {
                    Id        = (await context.SampleFrames.FirstOrDefaultAsync()).Id,
                    Predicate = expressionTree,
                    Name      = "New sample frame name",
                    Fields    = new[] { FieldEnum.Address }
                },
                    (await context.Users.FirstAsync()).Id);

                Assert.Equal(1, await context.SampleFrames.CountAsync());
                Assert.Equal("New sample frame name", (await context.SampleFrames.FirstOrDefaultAsync()).Name);
            }
        }