Exemplo n.º 1
0
        public async Task CopyAsync(string name, string id, long version, string suffix,
                                    CancellationToken ct = default(CancellationToken))
        {
            try
            {
                var file   = GetFile(name);
                var toFile = GetFile(id, version, suffix);

                file.CopyTo(toFile.FullName);

                using (var readStream = await bucket.OpenDownloadStreamAsync(file.Name, cancellationToken: ct))
                {
                    using (var writeStream =
                               await bucket.OpenUploadStreamAsync(toFile.Name, toFile.Name, cancellationToken: ct))
                    {
                        var buffer = new byte[ChunkSizeBytes];
                        int bytesRead;
                        while ((bytesRead = await readStream.ReadAsync(buffer, 0, buffer.Length, ct)) > 0)
                        {
                            await writeStream.WriteAsync(buffer, 0, bytesRead, ct);
                        }

                        await writeStream.CloseAsync(ct);
                    }
                }
            }
            catch (FileNotFoundException ex)
            {
                throw new AssetNotFoundException($"Asset {name} not found.", ex);
            }
            catch (GridFSException ex)
            {
                throw new AssetNotFoundException($"Asset {name} not found.", ex);
            }
        }
Exemplo n.º 2
0
        public async Task CopyAsync(string sourceFileName, string id, long version, string suffix, CancellationToken ct = default)
        {
            try
            {
                var target = GetFileName(id, version, suffix);

                using (var readStream = await bucket.OpenDownloadStreamAsync(sourceFileName, cancellationToken: ct))
                {
                    await UploadFileCoreAsync(target, readStream, ct);
                }
            }
            catch (GridFSFileNotFoundException ex)
            {
                throw new AssetNotFoundException(sourceFileName, ex);
            }
        }
        public async Task CopyAsync(string sourceFileName, string targetFileName, CancellationToken ct = default)
        {
            try
            {
                var sourceName = GetFileName(sourceFileName, nameof(sourceFileName));

                using (var readStream = await bucket.OpenDownloadStreamAsync(sourceFileName, cancellationToken: ct))
                {
                    await UploadAsync(targetFileName, readStream, false, ct);
                }
            }
            catch (GridFSFileNotFoundException ex)
            {
                throw new AssetNotFoundException(sourceFileName, ex);
            }
        }
Exemplo n.º 4
0
        public async Task CopyAsync(string name, string id, long version, string suffix, CancellationToken ct = default(CancellationToken))
        {
            try
            {
                var target = GetFileName(id, version, suffix);

                using (var readStream = await bucket.OpenDownloadStreamAsync(name, cancellationToken: ct))
                {
                    await bucket.UploadFromStreamAsync(target, target, readStream, cancellationToken : ct);
                }
            }
            catch (GridFSFileNotFoundException ex)
            {
                throw new AssetNotFoundException($"Asset {name} not found.", ex);
            }
        }
 public async Task <GridFSDownloadStream <ObjectId> > downloadFileAsync(ObjectId id)
 {
     return(await bucket.OpenDownloadStreamAsync(
                id,
                new GridFSDownloadOptions()
     {
         Seekable = true
     }));
 }
Exemplo n.º 6
0
        private async Task <bool> CopyToImpl(Stream stream, CancellationToken cancellationToken)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (_stream.Position != 0)
            {
                var newStream = await _bucket.OpenDownloadStreamAsync(_stream.FileInfo.Id, null, cancellationToken);

                if (newStream == null)
                {
                    return(false);
                }

                using (_stream)
                {
                    _stream = newStream;
                }
            }

            const int MinBufferSize = 81920;
            const int MaxBufferSize = 1 << 20;
            var       bufferSize    = Math.Min(Math.Max(_stream.FileInfo.ChunkSizeBytes, MinBufferSize), MaxBufferSize);
            var       position      = stream.CanSeek ? stream.Position : 0;

            try
            {
                await _stream.CopyToAsync(stream, bufferSize, cancellationToken);
            }
            catch (GridFSChunkException) when(_stream.Position == 0 && position == 0)
            {
                return(false);
            }
            catch (GridFSChunkException) when(stream.CanSeek)
            {
                stream.SetLength(position);
                return(false);
            }
            catch when(stream.CanSeek)
                {
                    stream.SetLength(position);
                    throw;
                }

            return(true);
        }
        /// <inheritdoc />
        public async Task <Stream> ObtainFile(string id)
        {
            if (!ObjectId.TryParse(id, out var objectId))
            {
                return(null);
            }

            try
            {
                return(await _bucket.OpenDownloadStreamAsync(objectId, new GridFSDownloadOptions
                {
                    Seekable = true
                }));
            }
            catch (GridFSFileNotFoundException)
            {
                return(null);
            }
        }
Exemplo n.º 8
0
        public async Task <(Stream, StoredFileInfo)> GetAsync(string fileId, CancellationToken cancellationToken = default)
        {
            _ = fileId ?? throw new ArgumentNullException(nameof(fileId));

            IGridFSBucket bucket = getBucket(_dbClient);
            FilterDefinition <GridFSFileInfo> filter = Builders <GridFSFileInfo> .Filter.Eq(p => p.Filename, fileId);

            using var cursor = await bucket.FindAsync(filter, null, cancellationToken);

            var file = await cursor.FirstOrDefaultAsync(cancellationToken);

            if (file == null)
            {
                return(null, null);
            }
            var stream = await bucket.OpenDownloadStreamAsync(file.Id, null, cancellationToken);

            return(stream, BsonSerializer.Deserialize <StoredFileInfo>(file.Metadata));
        }
Exemplo n.º 9
0
        public async Task <LuceneDirectory> CreateDirectoryAsync(DomainId ownerId)
        {
            var fileId = $"index_{ownerId}";

            var directoryInfo = new DirectoryInfo(Path.Combine(Path.GetTempPath(), fileId));

            if (directoryInfo.Exists)
            {
                directoryInfo.Delete(true);
            }

            directoryInfo.Create();

            try
            {
                using (var stream = await bucket.OpenDownloadStreamAsync(fileId))
                {
                    using (var zipArchive = new ZipArchive(stream, ZipArchiveMode.Read, true))
                    {
                        foreach (var entry in zipArchive.Entries)
                        {
                            var file = new FileInfo(Path.Combine(directoryInfo.FullName, entry.Name));

                            using (var entryStream = entry.Open())
                            {
                                using (var fileStream = file.OpenWrite())
                                {
                                    await entryStream.CopyToAsync(fileStream);
                                }
                            }
                        }
                    }
                }
            }
            catch (GridFSFileNotFoundException)
            {
            }

            var directory = FSDirectory.Open(directoryInfo);

            return(directory);
        }
Exemplo n.º 10
0
        private async Task <CdnDownloadResult> GetFileById(string fileId)
        {
            using (var file = await gridFsBucket.OpenDownloadStreamAsync(ObjectId.Parse(fileId)))
            {
                byte[] fileContent = new byte[(int)file.Length];
                file.Read(fileContent, 0, (int)file.Length);

                CdnDownloadResult result = new CdnDownloadResult()
                {
                    FileId            = fileId,
                    ContentType       = file.FileInfo.Metadata.GetValue("contentType").AsString,
                    FileName          = file.FileInfo.Filename,
                    FileContentBase64 = Convert.ToBase64String(fileContent)
                };

                await file.CloseAsync();

                return(result);
            }
        }
Exemplo n.º 11
0
        public async Task <BucketItem> GetFileBytesAsync(ObjectId fileId)
        {
            byte[] bytes;
            string contentType = string.Empty;

            using (var stream = await gridFSBucket.OpenDownloadStreamAsync(fileId))
            {
                bytes = new byte[stream.Length];
                await stream.ReadAsync(bytes);

                contentType = stream.FileInfo?.Metadata?.GetValue(ContentTypeName)?.AsString;

                await stream.CloseAsync();
            }

            BucketItem bucketItem = new BucketItem()
            {
                Data = bytes, ContentType = contentType
            };

            return(bucketItem);
        }
Exemplo n.º 12
0
 public async Task <GridFSDownloadStream <ObjectId> > GetAsync(string id) =>
 await _documents.OpenDownloadStreamAsync(new ObjectId(id));
Exemplo n.º 13
0
 public async Task <Stream> OpenReadAsync()
 {
     return(await _bucket.OpenDownloadStreamAsync(_fileInfo.Id));
 }
        async Task <Stream> IMessageDataRepository.Get(Uri address, CancellationToken cancellationToken)
        {
            var id = _resolver.GetObjectId(address);

            return(await _bucket.OpenDownloadStreamAsync(id, null, cancellationToken).ConfigureAwait(false));
        }
        public async Task <Stream> Get(Uri address, CancellationToken cancellationToken = new CancellationToken())
        {
            var id = _mongoMessageUriResolver.Resolve(address);

            return(await _gridFsBucket.OpenDownloadStreamAsync(id, null, cancellationToken).ConfigureAwait(false));
        }