Example #1
0
        /// <summary>
        /// 修改图片数据
        /// </summary>
        /// <param name="groupName">数据库组</param>
        /// <param name="collection">集合</param>
        /// <param name="file">文件对象</param>
        /// <returns></returns>
        public bool UpdateImg(string groupName, string collection, SunFile file)
        {
            SunMongoClient server;

            if (!MongoDbServer.GetClentByGroupName(groupName, out server))
            {
                throw new Exception("数据库群组名不存在");
            }
            var origDatabase   = server.GetDatabase(MUpload.originalImgDatabase);//原图数据库
            var origCollection = origDatabase.GetCollection <SunFile>(collection);
            var filter         = Builders <SunFile> .Filter.Eq(f => f.Id, file.Id);

            origCollection.ReplaceOneAsync(filter, file);
            return(true);
        }
Example #2
0
 /// <summary>
 /// 获取图片数据
 /// </summary>
 /// <param name="groupName">数据库组名</param>
 /// <param name="collection">文件所在的集合</param>
 /// <param name="id">文件id</param>
 /// <returns></returns>
 public static async Task <SunFile> GetImg(string groupName = null, string collection = null, string id = null)
 {
     try
     {
         SunMongoClient server;
         if (!MongoDbServer.GetClentByGroupName(groupName, out server))
         {
             throw new Exception("数据库群组名不存在");
         }
         var origDatabase   = server.GetDatabase(MUpload.originalImgDatabase);//原图数据库
         var origCollection = origDatabase.GetCollection <SunFile>(collection);
         return(await origCollection.Find <SunFile>(f => f.Id == id).FirstOrDefaultAsync());
     }
     catch
     {
         throw new Exception("文件不存在");
     }
 }
Example #3
0
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="dbGroupName">文件数据库组</param>
        /// <param name="collection">文件集合</param>
        /// <param name="id">文件ID</param>
        /// <returns></returns>
        public static bool DeleteFile(string dbGroupName, string collection, string id)
        {
            try
            {
                SunMongoClient server;
                if (!MongoDbServer.GetClentByGroupName(dbGroupName, out server))
                {
                    throw new Exception("数据库群组名不存在");
                }
                var origDatabase   = server.GetDatabase(MUpload.originalImgDatabase);                                 //原图数据库
                var origCollection = origDatabase.GetCollection <SunFile>(collection);
                var editCollection = server.GetDatabase(MUpload.editImgDatabase).GetCollection <SunFile>(collection); //缩略图数据库
                origCollection.DeleteOneAsync <SunFile>(f => f.Id == id);
                var filter = Builders <SunFile> .Filter.ElemMatch(f => f.Id, @"^\w+" + id + "$");

                editCollection.DeleteOneAsync(filter);
                return(true);
            }
            catch
            {
                return(false);
            }
        }