예제 #1
0
        public async Task <Stream> Get(string key)
        {
            var stream = new MemoryStream();
            await _fs.DownloadToStreamAsync(ObjectId.Parse(key), stream).ConfigureAwait(false);

            stream.Position = 0;
            return(stream);
        }
예제 #2
0
        async Task <DocumentInfo?> IQueryHandler <GetImgQuery, DocumentInfo?> .Handle(GetImgQuery query, CancellationToken cancellationToken)
        {
            var filter = Builders <GridFSFileInfo> .Filter.Eq(info => info.Filename, query.ImageId.ToString());

            var fileInfo = (await _gridFs.FindAsync(filter, null, cancellationToken)).FirstOrDefault();

            if (fileInfo == null)
            {
                return(null);
            }
            var memstream = new MemoryStream();
            await _gridFs.DownloadToStreamAsync(fileInfo.Id, memstream, cancellationToken : cancellationToken);

            return(new DocumentInfo(
                       id: query.ImageId,
                       memoryStream: memstream,
                       contentType: fileInfo.Metadata["ContentType"].ToString(),
                       length: fileInfo.Length,
                       fileName: fileInfo.Metadata.Contains("FileName") ? fileInfo.Metadata["FileName"].ToString() : "",
                       nonUniqueExtId: fileInfo.Metadata.Contains("NonUniqueExtId") ? fileInfo.Metadata["NonUniqueExtId"].ToString() : ""
                       ));
        }