Ejemplo n.º 1
0
        public (string, string) GetPath(DependencyFileType dependencyType, int dependencyId)
        {
            var relativePath = $@"arquivos\{dependencyType.ToString().ToLower()}\{dependencyId.ToString()}";
            var path         = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), relativePath);

            return(path, relativePath);
        }
Ejemplo n.º 2
0
        public void SaveFiles(DependencyFileType dependencyType, int dependencyId, IFormFileCollection files)
        {
            (string path, string relativePath) = GetPath(dependencyType, dependencyId);
            Directory.CreateDirectory(path);

            List <Arquivo> arquivos = new List <Arquivo>();

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    string file = GetFileName(path, formFile.FileName);
                    using (var stream = new FileStream(file, FileMode.Create))
                    {
                        formFile.CopyTo(stream);
                    }

                    arquivos.Add(new Arquivo
                    {
                        DataUpload     = DateTime.Now,
                        DependencyId   = dependencyId,
                        DependencyType = dependencyType,
                        Nome           = formFile.FileName,
                        Tamanho        = formFile.Length,
                        Path           = Path.Combine(relativePath, Path.GetFileName(file)),
                        ContentType    = formFile.ContentType
                    });
                }
            }
            _db.Arquivos.AddRange(arquivos);
            _db.SaveChanges();
        }
Ejemplo n.º 3
0
        public void ExcluiArquivos(DependencyFileType dependency, int id)
        {
            var arquivos = (_repositoryBase as IArquivoRepository).BuscaArquivos(dependency, id);

            foreach (var arquivo in arquivos)
            {
                Excluir(arquivo);
            }
        }
Ejemplo n.º 4
0
 public Arquivo(string nome, long tamanho, string contentType, string emailUsuario, string nomeUsuario, DependencyFileType dependencyType, int dependencyId)
 {
     Nome           = nome;
     Tamanho        = tamanho;
     ContentType    = contentType;
     EmailUsuario   = emailUsuario;
     NomeUsuario    = nomeUsuario;
     DependencyType = dependencyType;
     DependencyId   = dependencyId;
 }
Ejemplo n.º 5
0
        public async Task <Arquivo> SalvarArquivo(Stream file, DependencyFileType dependencyType, int dependencyId, string emailUsuario, string nomeUsuario, string nomeArquivo, string contentType)
        {
            var arquivosExistentes = _unitOfWork.ArquivoRepository.BuscaArquivos(dependencyType, dependencyId);

            if (arquivosExistentes.Any(a => a.Nome == nomeArquivo))
            {
                throw new DuplicatedException("Já há um arquivo salvo com esse nome.");
            }

            var novoArquivo = new Arquivo(
                nomeArquivo, file.Length, contentType, emailUsuario,
                nomeUsuario, dependencyType, dependencyId
                );
            var arquivoSalvo = await _unitOfWork.ArquivoRepository.Adicionar(novoArquivo, file);

            _unitOfWork.Commit();
            return(arquivoSalvo);
        }
Ejemplo n.º 6
0
 public void DeleteFiles(DependencyFileType dependency, int id)
 {
     Delete(a => a.DependencyId == id && a.DependencyType == dependency);
 }
Ejemplo n.º 7
0
 public List <Arquivo> GetFiles(DependencyFileType dependency, int id)
 {
     return(Query(a => a.DependencyId == id && a.DependencyType == dependency).ToList());
 }
Ejemplo n.º 8
0
 public IEnumerable <Arquivo> BuscaArquivos(DependencyFileType dependency, int id) =>
 (_repositoryBase as IArquivoRepository).BuscaArquivos(dependency, id);
Ejemplo n.º 9
0
 private string GetRelativePath(DependencyFileType dependencyType, int dependencyId)
 {
     return($@"{dependencyType.ToString().ToLower()}/{dependencyId.ToString()}");
 }
Ejemplo n.º 10
0
 public IEnumerable <Arquivo> BuscaArquivos(DependencyFileType dependency, int id) =>
 _db.Arquivos.Where(a => a.DependencyId == id && a.DependencyType == dependency);
Ejemplo n.º 11
0
 /// <summary>
 /// Initializes a dependency file.
 /// </summary>
 /// <param name="filePath">File path.</param>
 /// <param name="fileType">File type.</param>
 public DependencyFile(string filePath, DependencyFileType fileType)
 {
     FilePath = filePath;
     FileType = fileType;
 }