public IActionResult Upload(IFormFile file) { string path = ""; DtoUploadResponse response = new DtoUploadResponse(); if (file == null || file.Length == 0) { _logger.LogError(LoggingEvents.Critial, "Arquivo não selecionado."); response.AddNotification(Notifications.ErroInesperado, "Arquivo não selecionado."); return(BadRequest(response)); } try { path = Directory.GetCurrentDirectory() + "\\wwwroot\\Log"; response = _logQuakeService.UploadFile(path, file.FileName, file.OpenReadStream()); if (response.Notifications != null) { _logger.LogWarning(LoggingEvents.Information, "Falha ao realizar o Upload do arquivo {0}", file.FileName); return(BadRequest(response)); } else { _logger.LogInformation(LoggingEvents.Information, "Upload realizado com sucesso do arquivo {0}", file.FileName); return(Ok(response)); } } catch (Exception ex) { _logger.LogCritical(LoggingEvents.Critial, ex, "Upload do log {0}", file.FileName); response.AddNotification(Notifications.ErroInesperado, ex); return(BadRequest(response)); } }
/// <summary> /// Método responsável receber o Upload do arquivo de log. /// </summary> /// <param name="folder">local de destino do arquivo de log</param> /// <param name="filename">arquivo a ser lido</param> /// <param name="stream">arquivo propriamente dito no formato Stream</param> /// <returns> /// Retorna um objeto contendo nome do arquivo, quantidade de registro inseridos, se ouve sucesso Sim ou Não. /// </returns> public DtoUploadResponse UploadFile(string folder, string fileName, Stream file) { DtoUploadResponse response = new DtoUploadResponse(); string path = folder; int RegistrosInseridos = 0; //caso não exista o destino, deve-se criar as pastas try { if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path += "\\" + fileName; } catch (Exception ex) { response.AddNotification(Notifications.ErroInesperado, string.Format("Falha ao criar o diretório {0}", path), ex); return(response); } //criando o arquivo de log em seu destino try { using (var stream = new FileStream(path, FileMode.Create)) { file.CopyTo(stream); } } catch (Exception ex) { response.AddNotification(Notifications.ErroInesperado, string.Format("Falha ao copiar o arquivo {0} para o diretório {1}", fileName, path), ex); return(response); } //Processando o arquivo de log: //1 - Efetua a leitura do arquivo de log //2 - Converte arquivo de log em lista de objetos Kill //3 - Inserindo no Banco de Dados a lista de Kill //4 - Retorna o objeto para o método que solicitou o Upload try { List <Kill> Kills; List <string> linhas = ReadLogFile(path); if (linhas.Count > 0) { Kills = ConvertLogFileInListKill(linhas); RegistrosInseridos = AddKillListInDB(Kills); } response.FileName = fileName; response.Length = file.Length; response.RegistrosInseridos = RegistrosInseridos; return(response); } catch (Exception ex) { response.AddNotification(Notifications.ErroInesperado, "Falha ao processar o arquivo " + fileName, ex); return(response); } }