6.1 GridFS 帮助类

/// <summary>
    /// 针对大文件 chunk的帮助类
    /// 不经常使用所以没有静态
    /// MongoDB 版本4.2
    /// 参考网址
    /// https://docs.mongodb.com/manual/tutorial/query-documents/
    /// https://www.runoob.com/mongodb/mongodb-tutorial.html
    /// </summary>
    public class GridFSHelper
    {
        private ObjectId oid;
        private readonly GridFSBucket bucket;
        private GridFSFileInfo fileInfo;
        private readonly IMongoCollection<BsonDocument> collection;

        /// <summary>
        /// 初始化数据库连接信息
        /// </summary>
        /// <param name="host">MongoDB连接信息,实例(链接字符串,数据库,[表])</param>
        public GridFSHelper(MongodbHostOptions host)
        {
            MongoClient client = new MongoClient(host.Connection);
            IMongoDatabase mongoDatabase = client.GetDatabase(host.DataBase);
            // 这里定义了默认的表名称  这里应该把"my_bucket.files"  设置为参数
            collection = mongoDatabase.GetCollection<BsonDocument>(host.CollectionName + ".files");

            bucket = new GridFSBucket(mongoDatabase, new GridFSBucketOptions
            {
                BucketName = host.CollectionName,
                ChunkSizeBytes = 1048576 //每个大文件被分割的块大小
            });
        }

        /// <summary>
        /// 上传文件
        ///
        ///
        /// </summary>
        /// <param name="gfsname"></param>
        /// <param name="source"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        public async Task<ObjectId> UploadFromStreamAsync(string gfsname, Stream source, string filename)
        {
            var options = new GridFSUploadOptions
            {
                Metadata = new BsonDocument {
                    { "filename", filename },
                    { "contentType",Path.GetDirectoryName(filename)}
                },
            };

            return await bucket.UploadFromStreamAsync(gfsname, source, options);
        }

        /// <summary>
        /// GridFS 上传图片  图片不是特别大的时候  这个时候是上传到内存当中
        /// </summary>
        /// <param name="host"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<ObjectId> AddAsync(Images_Mes imagesMes)
        {
            var options = new GridFSUploadOptions
            {
                Metadata = new BsonDocument
                {
                    { "filename", imagesMes.FileName },
                    { "contentType",imagesMes.FileType}
                },
                DisableMD5 = true,
            };

            ObjectId objectId = await bucket.UploadFromBytesAsync(imagesMes.FileName, imagesMes.ByteImg, options);

            return objectId;
        }

        /// <summary>
        /// FS 查询图片
        /// </summary>
        /// <param name="id">Objectid 主键</param>
        /// <returns></returns>
        public async Task<Images_Mes> DownSync(ObjectId id)
        {
            Images_Mes imagesMes = new Images_Mes();
            byte[] byteImg = await bucket.DownloadAsBytesAsync(id);
            imagesMes.ByteImg = byteImg;

            var filter = Builders<BsonDocument>.Filter.Eq("_id", id);

            var doucument = collection.FindAsync(filter).GetAwaiter().GetResult();
            var test = doucument.ToList().First();
            //Console.WriteLine(test.GetValue("filename"));
            imagesMes.FileName = test.GetValue("filename")?.ToString();
            return imagesMes;
        }



        //修改图片 对文档重新命名
        public async Task RenameSync(ObjectId id, string NewName)
        {
            await bucket.RenameAsync(id, NewName);

        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="id">Objectid 主键</param>
        /// <returns></returns>
        public async Task DeleteSync(ObjectId id)
        {
            try
            {
                await bucket.DeleteAsync(id);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }

        /// <summary>
        /// 删除多个图片
        /// </summary>
        /// <param name="idlist"></param>
        /// <returns></returns>
        public async Task DeleteListSync(List<ObjectId> idlist)
        {
            foreach (var id in idlist)
            {
                await bucket.DeleteAsync(id);
            }
        }


        public ObjectId UploadGridFSFromBytes(string filename, Byte[] source)
        {
            oid = bucket.UploadFromBytes(filename, source);
            return oid;
        }

        public ObjectId UploadGridFSFromStream(string filename, Stream source)
        {
            using (source)
            {
                oid = bucket.UploadFromStream(filename, source);
                return oid;
            }
        }

        public Byte[] DownloadAsByteArray(ObjectId id)
        {
            Byte[] bytes = bucket.DownloadAsBytes(id);
            return bytes;
        }

        public Stream DownloadToStream(ObjectId id)
        {
            Stream destination = new MemoryStream();
            bucket.DownloadToStream(id, destination);
            return destination;
        }

        public Byte[] DownloadAsBytesByName(string filename)
        {
            Byte[] bytes = bucket.DownloadAsBytesByName(filename);
            return bytes;
        }

        public Stream DownloadToStreamByName(string filename)
        {
            Stream destination = new MemoryStream();
            bucket.DownloadToStreamByName(filename, destination);
            return destination;
        }

        public GridFSFileInfo FindFiles(string filename)
        {
            var filter = Builders<GridFSFileInfo>.Filter.And(
            Builders<GridFSFileInfo>.Filter.Eq(x => x.Filename, "man"),
            Builders<GridFSFileInfo>.Filter.Gte(x => x.UploadDateTime, new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc)),
            Builders<GridFSFileInfo>.Filter.Lt(x => x.UploadDateTime, new DateTime(2017, 2, 1, 0, 0, 0, DateTimeKind.Utc)));
            var sort = Builders<GridFSFileInfo>.Sort.Descending(x => x.UploadDateTime);
            var options = new GridFSFindOptions
            {
                Limit = 1,
                Sort = sort
            };
            using (var cursor = bucket.Find(filter, options))
            {
                fileInfo = cursor.ToList().FirstOrDefault();
            }
            return fileInfo;
        }


        public void DeleteAndRename(ObjectId id)
        {
            bucket.Delete(id);
        }

        //The “fs.files” collection will be dropped first, followed by the “fs.chunks” collection. This is the fastest way to delete all files stored in a GridFS bucket at once.  
        public void DroppGridFSBucket()
        {
            bucket.Drop();
        }

        public void RenameAsingleFile(ObjectId id, string newFilename)
        {
            bucket.Rename(id, newFilename);
        }

        public void RenameAllRevisionsOfAfile(string oldFilename, string newFilename)
        {
            var filter = Builders<GridFSFileInfo>.Filter.Eq(x => x.Filename, oldFilename);
            var filesCursor = bucket.Find(filter);
            var files = filesCursor.ToList();
            foreach (var file in files)
            {
                bucket.Rename(file.Id, newFilename);
            }
        }
    }
posted @ 2020-06-12 10:43  maanshancss  阅读(243)  评论(0编辑  收藏  举报