Пример #1
0
        public IActionResult Upload(IFormFile file, string Delimeter, string Location, string Name)
        {
            if (file == null || Delimeter == null || Location == null || Name == null)
            {
                return(View());
            }
            else
            {
                this._verifyUploadFolderExistence();
            }
            Console.WriteLine("Načtení controlleru a souboru: " + file.FileName);
            Uploader uploader = new Uploader();

            if (Delimeter == "tabulator")
            {
                Delimeter = "\t";
            }
            uploader.Delimeter = Delimeter;
            if (file != null)
            {
                if (Path.GetExtension(file.FileName) != ".csv")
                {
                    uploader.SetIncorrectExtensionMessage(Path.GetExtension(file.FileName));
                    return(View(uploader));
                }
                //Generate file name
                string FileName = Guid.NewGuid().ToString() + Path.GetExtension(file.FileName);
                Console.WriteLine("Vygenerováno jméno souboru");

                //Generate url to save
                string SavePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/uploads", FileName);
                uploader.FilePath = SavePath;
                Console.WriteLine("Příprava na přesun");

                try{
                    using (var stream = new FileStream(SavePath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                    }
                }catch (Exception ex) {
                    Console.WriteLine(ex.Message);
                }


                Console.WriteLine("Přesun kompletní");

                //Check status of upload
                bool result = uploader.CheckFileExistence(FileName);
                Console.WriteLine("Stav uploadu zkontrolován: " + result);
                List <string> delimeter = new List <string>();
                if (Delimeter == "auto")
                {
                    delimeter.Add(";");
                    delimeter.Add(",");
                    delimeter.Add("\t");
                }
                else
                {
                    if (Delimeter == "tabulator")
                    {
                        Delimeter = "\t";
                    }
                    else
                    {
                        delimeter.Add(Delimeter);
                    }
                }

                if (result)
                {
                    int  iteration           = 0;
                    bool importDoneCorrectly = false;
                    while (importDoneCorrectly == false)
                    {
                        if (this._processCSV(FileName, Name, Location, delimeter[iteration]) == true)
                        {
                            importDoneCorrectly = true;
                        }
                        else
                        {
                            iteration++;
                        }

                        if (iteration == delimeter.Count())
                        {
                            break;
                        }
                        Console.WriteLine(importDoneCorrectly);
                    }
                    if (importDoneCorrectly == false)
                    {
                        LogMessage lm = new LogMessage("Zpracování CSV", "500", "Nebylo možné přečíst CSV soubor. Zkustě jiný oddělovač", "Error");
                        new Alerter(lm, HttpContext);
                    }
                    else
                    {
                        LogMessage lm = new LogMessage("Zpracování CSV", "200", "CSV bylo zpracováno a nahráno", "OK");
                        new Alerter(lm, HttpContext);
                    }
                    //Odstraň CSV soubor
                    uploader.Delete();
                    if (importDoneCorrectly == true)
                    {
                        return(RedirectToAction("Imports", "User"));
                    }
                    else
                    {
                        uploader.AlertStatus = "bg-danger";
                        uploader.Status      = "Došlo k chybě při zpracování souboru";
                        return(View(uploader));
                    }
                }
                else
                {
                    //Odstraň CSV soubor
                    uploader.Delete();

                    return(View(uploader));
                }
            }
            return(View(uploader));
        }