Exemplo n.º 1
0
        public IActionResult Index(FileViewModel model)
        {
            var result = new Result();

            if (model.File != null)
            {
                var path = Path.Combine(_environment.ContentRootPath, model.File.FileName);
                using (var stream = new FileStream(path, FileMode.Create))
                {
                    try
                    {
                        model.File.CopyTo(stream);
                    }
                    catch (Exception ex)
                    {
                        result.Text    = ex.Message;
                        result.Success = false;
                        HttpContext.Session.SetString("result", JsonConvert.SerializeObject(result));
                        return(RedirectToAction("Result"));
                    }
                }
                var extractor = new TextExtractor();
                var watch     = Stopwatch.StartNew();
                var text      = extractor.SelectStrategy(path, model.Language);
                watch.Stop();
                if (text != null)
                {
                    System.IO.File.Delete(path);
                    result.Text          = text.Trim();
                    result.Success       = true;
                    result.ExecutionTime = watch.ElapsedMilliseconds;
                    HttpContext.Session.SetString("result", JsonConvert.SerializeObject(result));
                    return(RedirectToAction("Result"));
                }
                System.IO.File.Delete(path);
                result.Success = false;
                result.Text    = $"Supported Formats: {extractor.SupportedFormats()} {Environment.NewLine}{Environment.NewLine} Supported Languages: {extractor.SupportedLanguages()}";
                HttpContext.Session.SetString("result", JsonConvert.SerializeObject(result));
                return(RedirectToAction("Result"));
            }
            result.Success = false;
            result.Text    = "File is null";
            HttpContext.Session.SetString("result", JsonConvert.SerializeObject(result));
            return(RedirectToAction("Result"));
        }