/// <summary> /// Salva o registro passado como parâmetro. /// </summary> public override SecaoArquivoViewModel Add(SecaoArquivoViewModel model, bool commit = true) { #region Validações var extensaoArquivo = model.Nome.RegexReplace(@"^(.*\.)", string.Empty); if (extensaoArquivo != "docx") { throw new PropostasException("Permitido apenas arquivos no formato docx."); } #endregion Validações var arquivoAzurePath = "https://testeblendit.blob.core.windows.net/dev-propostas/arquivosessao/" + Guid.NewGuid().ToString() + ".docx"; model.UrlArquivo = arquivoAzurePath; model.Versao = 1; #region Controle de Versão var ultimoArquivoAtivo = this.repository.GetBy(arquivo => arquivo.SecaoId == model.SecaoId && arquivo.Ativo == true).LastOrDefault(); // Se havia um arquivo ativo anteriormente, devemos deixá-lo desativado e incrementar a versao do novo if (ultimoArquivoAtivo != null) { ultimoArquivoAtivo.Ativo = false; this.repository.Update(ultimoArquivoAtivo); model.Versao = (ultimoArquivoAtivo.Versao ?? 0) + 1; } #endregion Controle de Versão var entity = this.mapper.Map <SecaoArquivo>(model); // Realizando o upload do arquivo solicitado var bytes = Convert.FromBase64String(model.Base64Arquivo.Split(',')[1]); this.azureBlobService.Upload(bytes, arquivoAzurePath); var localPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".docx"; //var resultados = new List<SecaoArquivo>(); //var secaoArquivo = new SecaoArquivo() //{ // Nome = nomeArquivo, // Publicado = DateTime.Now, // Url = arquivoAzurePath, // Versao = 1, // SecaoArquivoTags = new List<SecaoArquivoTag>() //}; try { //grava os bytes do arquivo em localPath File.WriteAllBytes(localPath, bytes); // this.azureBlobService.Download(localPath, arquivoAzurePath, true); // Controle das tags var tags = this.documentService.GetTags(localPath); foreach (var tag in tags) { var tagSplit = tag.Split(':'); var tagTipo = TagTipoEnum.Texto; var tagChave = string.Empty; if (tagSplit.Length == 2) { var tagTipoString = tagSplit[0]; switch (tagTipoString) { case "texto": tagTipo = TagTipoEnum.Texto; break; case "imagem": tagTipo = TagTipoEnum.Imagem; break; default: tagTipo = TagTipoEnum.Texto; break; } tagChave = tagSplit[1]; } // verifica se a tag já existe no bd var tagDb = this.tagService.GetBy(t => t.Chave == tagChave && t.Tipo == tagTipo.GetDescription()).FirstOrDefault(); // Salva tag que ainda não existe no banco if (tagDb == null) { var tagViewModel = new TagViewModel() { Tipo = tagTipo.GetDescription(), Chave = tagChave }; // Incluindo uma nova tag tagDb = this.tagService.Add(tagViewModel, false); } //verifica se SeçãoArquivoTag não existe var secaoArquivoTag = new SecaoArquivoTag() { TagId = tagDb.Id, SecaoArquivoId = entity.Id }; entity.SecaoArquivoTags.Add(secaoArquivoTag); } } finally { File.Delete(localPath); } this.repository.Add(entity); this.Commit(commit); model.Id = entity.Id; return(model); }
public override SecaoViewModel Add(SecaoViewModel model, bool commit = true) { var entity = this.mapper.Map <Secao>(model); var extensaoArquivo = model.SecaoArquivo.Nome.RegexReplace(@"^(.*\.)", string.Empty); if (extensaoArquivo != "docx") { throw new PropostasException("Permitido apenas arquivos no formato docx."); } var arquivoAzurePath = "https://testeblendit.blob.core.windows.net/dev-propostas/arquivoSessao/" + Guid.NewGuid().ToString() + ".docx"; entity.SecaoArquivo = new List <SecaoArquivo>() { new SecaoArquivo() { Nome = model.SecaoArquivo.Nome, Publicado = DateTime.Now, Url = arquivoAzurePath, Versao = 1, SecaoArquivoTags = new List <SecaoArquivoTag>() } }; // 1) Para cada tag encontrada no documento: // 1.1) Salva tag se nao existir // 1.2) Obtem o ID da tag; // 1.3) Cria um SecaoArquivoTag contendo a referencia de TagId // 1.4) Inclui o SecaoArquivoTag criado em 3 na lista entity.SecaoArquivo.First().SecaoArquivoTags.Add(); // 2) Salva objeto em this.repository.Add(entity); // Salvando no Azure var bytes = Convert.FromBase64String(model.SecaoArquivo.Base64Arquivo.Split(',')[1]); //encontrar as tags do arquivo var localPath = Path.GetTempPath() + Guid.NewGuid().ToString() + ".docx"; //this.secaoArquivoService.Add(model.SecaoArquivo, false); this.azureBlobService.Upload(bytes, arquivoAzurePath); try { //grava os bytes do arquivo em localPath File.WriteAllBytes(localPath, bytes); // Controle das tags var tags = this.documentService.GetTags(localPath); foreach (var tag in tags) { var tagSplit = tag.Split(':'); var tagTipo = TagTipoEnum.Texto; var tagChave = string.Empty; if (tagSplit.Length == 2) { var tagTipoString = tagSplit[0]; switch (tagTipoString) { case "texto": tagTipo = TagTipoEnum.Texto; break; case "imagem": tagTipo = TagTipoEnum.Imagem; break; default: tagTipo = TagTipoEnum.Texto; break; } tagChave = tagSplit[1]; } // verifica se a tag já existe no bd var tagDb = this.tagService.GetBy(t => t.Chave == tagChave && t.Tipo == tagTipo.GetDescription()).FirstOrDefault(); // Salva tag que ainda não existe no banco if (tagDb == null) { var tagViewModel = new TagViewModel() { Tipo = tagTipo.GetDescription(), Chave = tagChave }; // Incluindo uma nova tag tagDb = this.tagService.Add(tagViewModel, false); } var secaoArquivoTag = new SecaoArquivoTag() { TagId = tagDb.Id }; entity.SecaoArquivo.First().SecaoArquivoTags.Add(secaoArquivoTag); } } finally { File.Delete(localPath); } this.repository.Add(entity); this.Commit(commit); model.Id = entity.Id; return(model); }