public Stream GetImageContent(string id) { if (string.IsNullOrEmpty(id)) { return(null); } var objId = new ObjectId(); if (!ObjectId.TryParse(id, out objId)) { return(null); } var image = Get(id); if (string.IsNullOrEmpty(image.ContentId)) { return(null); } var content = new MemoryStream(); GridFs.DownloadToStream(ObjectId.Parse(image.ContentId), content); content.Position = 0; return(content); }
public async Task <IEnumerable <GridFSFileInfo> > GetImages(IEnumerable <String> ImagesId) { var imagesList = new List <GridFSFileInfo>(); foreach (var item in ImagesId) { imagesList.AddRange(await GridFs.Find(Builders <GridFSFileInfo> . Filter.Eq(x => x.Metadata["FileID"], item)).ToListAsync()); } return(imagesList); }
public async Task UploadBytes(Byte[] source, String fileName) { _imageId = ObjectId.GenerateNewId().ToString(); await GridFs.UploadFromBytesAsync(fileName, source, new GridFSUploadOptions { Metadata = new BsonDocument { { "FileID", ImageId } } }); }
public async Task UploadStream(Stream stream, String fileName) { _imageId = ObjectId.GenerateNewId().ToString(); await GridFs.UploadFromStreamAsync(fileName, stream, new GridFSUploadOptions { Metadata = new BsonDocument { { "FileID", ImageId } } }); }
public Stream GetByContentId(string id) { var objId = new ObjectId(); if (!ObjectId.TryParse(id, out objId)) { return(null); } var content = new MemoryStream(); GridFs.DownloadToStream(ObjectId.Parse(id), content); content.Position = 0; return(content); }
public void Delete(string id) { var image = Get(id); if (image != null) { if (image.ContentId != null) { GridFs.Delete(ObjectId.Parse(image.ContentId)); } File.Delete(image.Filename); GridFs.Delete(ObjectId.Parse(image.Thumb.ContentId)); var builder = Builders <Image> .Filter; var filter = builder.Eq("_id", ObjectId.Parse(id)); Collection.DeleteOne(filter); } }
public async Task Delete(ObjectId fileId) { var fileIdFilter = Builders <BsonDocument> .Filter.Eq("files_id", fileId); await Task.WhenAll(GridFs.DeleteAsync(fileId), ChunksCollection.DeleteManyAsync(fileIdFilter)); }
public async Task <string> Get(ObjectId fileId) { var file = await GridFs.DownloadAsBytesAsync(fileId); return(Encoding.UTF8.GetString(file)); }
public async Task <ObjectId> Create(Guid id, Stream stream) => await GridFs.UploadFromStreamAsync($"{id}.txt", stream);
public async Task <ObjectId> Create(Guid id, string data) => await GridFs.UploadFromBytesAsync($"{id}.txt", Encoding.UTF8.GetBytes(data));
public string CreateThumb(string filename, Stream thumbContent) { thumbContent.Position = 0; return(GridFs.UploadFromStream(filename, thumbContent).ToString()); }