Exemplo n.º 1
0
        public async Task <Guid> AddDocument(Stream fileStream, string fileName)
        {
            if (fileStream == null)
            {
                throw new ArgumentNullException(nameof(fileStream));
            }

            var extension = Path.GetExtension(fileName);

            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException($"{fileName} cannot be empty");
            }

            if (string.IsNullOrEmpty(extension))
            {
                throw new ArgumentException($"{extension} cannot be empty");
            }

            using (var unitOfWork = new DocumentUOW(_frissFileSystemContext, _frissDbContext))
            {
                var documentId = Guid.NewGuid();

                _documentRepository.Add(new Document()
                {
                    Id             = documentId,
                    CreatedDate    = DateTime.UtcNow,
                    LastAccessDate = null,
                    FileName       = fileName,
                    OwnerName      = "TODO",
                    Size           = fileStream.Length
                });

                _fileRepository.AddFile(documentId, extension, fileStream);

                await unitOfWork.SaveChanges();

                return(documentId);
            }
        }
Exemplo n.º 2
0
        public async Task <Stream> GetDocumentFile(Document document)
        {
            if (document == null)
            {
                throw new ArgumentNullException(nameof(document));
            }

            using (var unitOfWork = new DocumentUOW(_frissFileSystemContext, _frissDbContext))
            {
                var stream = _fileRepository.ReadFile(document.Id, Path.GetExtension(document.FileName));

                if (stream == null)
                {
                    throw new FileNotFoundException($"{document.Id} file is not found");
                }

                document.LastAccessDate = DateTime.UtcNow;
                await unitOfWork.SaveChanges();

                return(stream);
            }
        }