Exemplo n.º 1
0
        public static void InserirAnexo(IFoto.TipoFoto tipo, uint idParent, byte[] buffer, string fileName, string descricao)
        {
            // Cadastra a foto
            var foto = IFoto.Nova(tipo);

            foto.IdParent = idParent;

            var extensao = Path.GetExtension(fileName);

            if (string.IsNullOrEmpty(extensao))
            {
                throw new Exception("O arquivo informado não possui extensão.");
            }

            foto.Extensao = extensao;

            if (foto.ApenasImagens && !Arquivos.IsImagem(foto.Extensao))
            {
                throw new Exception("Apenas imagens podem ser cadastradas.");
            }

            if (foto.Tipo == IFoto.TipoFoto.Pedido)
            {
                if (PedidoEspelhoDAO.Instance.IsPedidoImpresso(null, foto.IdParent))
                {
                    throw new Exception("Não é possível inserir imagem em pedidos que já possuam etiqueta(s) impressa(s).");
                }
            }

            foto.Descricao = descricao;
            foto.IdFoto    = foto.Insert();

            if (foto.IdFoto == 0)
            {
                throw new Exception("Falha ao cadastrar foto.");
            }

            try
            {
                // Salva o arquivo da foto
                if (!Directory.Exists(foto.Path))
                {
                    Directory.CreateDirectory(foto.Path);
                }

                ManipulacaoImagem.SalvarImagem(foto.FilePath, buffer);

                if (tipo == IFoto.TipoFoto.Pedido)
                {
                    // Cria o Log de inserção do Anexo imagem Pedido
                    LogAlteracao log = new LogAlteracao();
                    log.Tabela        = (int)LogAlteracao.TabelaAlteracao.Pedido;
                    log.IdRegistroAlt = (int)idParent;
                    log.NumEvento     = LogAlteracaoDAO.Instance.GetNumEvento(LogAlteracao.TabelaAlteracao.Pedido, (int)idParent);
                    log.Campo         = "Anexo Pedido";
                    log.DataAlt       = DateTime.Now;
                    log.IdFuncAlt     = UserInfo.GetUserInfo.CodUser;
                    log.ValorAnterior = null;
                    log.ValorAtual    = string.Format("{0} - Imagem Anexada", foto.IdFoto);
                    log.Referencia    = LogAlteracao.GetReferencia(log.Tabela, idParent);
                    LogAlteracaoDAO.Instance.Insert(log);
                }
            }
            catch (Exception ex)
            {
                foto.Delete();
                throw ex;
            }
        }
Exemplo n.º 2
0
        private string InserirMult(string descricao, IFoto.TipoFoto tipo)
        {
            var hfc = Request.Files;
            var arquivosNaoAnexados = "";

            for (var i = 0; i < hfc.Count; i++)
            {
                // Cadastra a foto.
                var foto = IFoto.Nova(tipo);

                // Recupera os dados do arquivo.
                var arquivo = hfc[i];

                // Recupera o nome do arquivo, que deve ser exatamente o id da referência.
                var idReferencia = Conversoes.StrParaUint(arquivo.FileName.Split('-', '.', ' ')[0]);

                // Verifica se são arquivos de pedidos ou de liberações.
                switch (Request["tipo"])
                {
                case "pedido":

                    if (PedidoEspelhoDAO.Instance.IsPedidoImpresso(null, foto.IdParent))
                    {
                        return("Não é possível inserir imagem em pedidos que já possuam etiqueta(s) impressa(s).");
                    }

                    // Se o tipo for pedido e o nome do arquivo não for o id de um pedido válido então o arquivo não é anexado.
                    else if (!PedidoDAO.Instance.Exists(idReferencia))
                    {
                        arquivosNaoAnexados += arquivo.FileName + ", ";
                        continue;
                    }

                    break;

                case "liberacao":
                    // Se o tipo for pedido e o nome do arquivo não for o id de uma liberação válida então o arquivo não é anexado.
                    if (!LiberarPedidoDAO.Instance.Exists(idReferencia))
                    {
                        arquivosNaoAnexados += arquivo.FileName + ", ";
                        continue;
                    }

                    break;

                default:
                    continue;
                }

                foto.IdParent = idReferencia;
                foto.Extensao = arquivo.FileName.Substring(arquivo.FileName.LastIndexOf('.'));

                if (foto.ApenasImagens && !Arquivos.IsImagem(foto.Extensao))
                {
                    arquivosNaoAnexados += arquivo.FileName + ", ";
                    continue;
                }

                foto.Descricao = descricao;
                foto.IdFoto    = foto.Insert();

                if (foto.IdFoto == 0)
                {
                    arquivosNaoAnexados += arquivo.FileName + ", ";
                    foto.Delete();
                    continue;
                }

                try
                {
                    ManipulacaoImagem.SalvarImagem(foto.FilePath, arquivo.InputStream);
                }
                catch
                {
                    foto.Delete();
                    arquivosNaoAnexados += arquivo.FileName + ", ";
                    continue;
                }
            }

            return(arquivosNaoAnexados = !String.IsNullOrEmpty(arquivosNaoAnexados)
                ? "Arquivos não anexados: " + arquivosNaoAnexados.Trim(' ').Trim(',') +
                                         ".\\n\\nCertifique-se de que os nomes dos arquivos estão corretos."
                : "Arquivos anexados com sucesso.");
        }