Beispiel #1
0
        public void Ack()
        {
            var messageOne = new BsonDocument {
                { "key1", 0 }, { "key2", true }
            };

            using (var streamOne = new MemoryStream())
                using (var streamTwo = new MemoryStream())
                {
                    streamOne.WriteByte(111);
                    streamTwo.WriteByte(222);
                    streamOne.Position = 0;
                    streamTwo.Position = 0;
                    queue.Send(messageOne, DateTime.Now, 0.0, new Dictionary <string, Stream> {
                        { "one", streamOne }, { "two", streamTwo }
                    });
                }
            queue.Send(new BsonDocument("key", "value"));

            var result = queue.Get(new QueryDocument(messageOne), TimeSpan.MaxValue, TimeSpan.Zero);

            Assert.AreEqual(2, collection.CountDocuments(new QueryDocument()));

            queue.Ack(result.Handle);
            Assert.AreEqual(1, collection.CountDocuments(new QueryDocument()));
            Assert.AreEqual(0, gridfs.Find(new QueryDocument()).ToList().Count);
        }
        public BsonDocument browseFile()
        {
            var          filter = Builders <GridFSFileInfo> .Filter;
            BsonDocument document;

            using (var cursor = gridFSBucket.Find(filter.Eq(x => x.Filename, "G:\\Map\\Tasks\\6月Task\\13\\13-2-1603\\output\\floor\\tileset.json")))
            {
                var fileInfo = cursor.FirstOrDefault();
                document = fileInfo.BackingDocument;
            }
            return(document);
        }
        public void UploadImage(Image imageToUpload, string imageName)
        {
            var cardHashName = imageName.Substring(0, 4) +
                               imageName.Substring(imageName.Length - 8, 4) +
                               imageName.Substring(imageName.Length - 4, 4);

            MongoClient client = connection();
            var         db     = client.GetDatabase("CreditCardIMG");
            var         bucket = new GridFSBucket(db);
            Image       img    = imageToUpload;

            byte[] source = ImageToByteArray(img);

            if (IsImageExist(cardHashName))
            {
                var filter = Builders <GridFSFileInfo> .Filter.And(
                    Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, cardHashName));

                var options = new GridFSFindOptions
                {
                    Limit = 1
                };

                using (var cursor = bucket.Find(filter, options))
                {
                    var fileInfo = cursor.ToList().FirstOrDefault();
                    bucket.Delete(fileInfo.Id);
                    bucket.UploadFromBytes(cardHashName, source);
                }
            }
            else
            {
                bucket.UploadFromBytes(cardHashName, source);
            }
        }
        public bool IsImageExist(string imageName)
        {
            MongoClient client = connection();
            var         db     = client.GetDatabase("CreditCardIMG");
            var         bucket = new GridFSBucket(db);
            var         filter = Builders <GridFSFileInfo> .Filter.And(
                Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, imageName));

            var options = new GridFSFindOptions
            {
                Limit = 1
            };

            using (var cursor = bucket.Find(filter, options))
            {
                var fileInfo = cursor.ToList().FirstOrDefault();
                if (fileInfo == null)
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
        }
Beispiel #5
0
        public async Task <IList <FileDataModel> > DownloadsAsync(string siteId = null, string parentId = null, bool isGetStream = true, bool isGetUrl = false)
        {
            var builder = Builders <GridFSFileInfo> .Filter;
            var filter  = builder.Empty;

            if (!string.IsNullOrEmpty(siteId))
            {
                filter = builder.Eq("metadata.id", siteId);
                if (!string.IsNullOrEmpty(parentId))
                {
                    filter = filter & builder.Eq("metadata.parentId", parentId);
                }
            }
            var gridFs = new GridFSBucket(_database);
            var files  = await gridFs.Find(filter).ToListAsync();

            var results = new List <FileDataModel>();

            if (files == null)
            {
                return(results);
            }
            foreach (var mongoGridFsFileInfo in files)
            {
                var fileDataModel = MapFileDataModel(mongoGridFsFileInfo);
                if (isGetStream)
                {
                    fileDataModel.FileData.Stream = await gridFs.OpenDownloadStreamAsync(mongoGridFsFileInfo.Id);
                }
                results.Add(fileDataModel);
            }

            return(results);
        }
        public FileData Download(string fileId)
        {
            FileData file = null;

            try
            {
                GridFSBucket   gridFS     = new GridFSBucket(this.GetDB());
                GridFSFileInfo gridFSFile = gridFS.Find(Builders <GridFSFileInfo> .Filter.Eq("_id", new ObjectId(fileId))).FirstOrDefault();
                if (gridFSFile != null)
                {
                    file = new FileData
                    {
                        Id             = fileId,
                        Name           = gridFSFile.Filename,
                        Type           = gridFSFile.Filename.Substring(gridFSFile.Filename.IndexOf('.') + 1),
                        Bytes          = gridFS.DownloadAsBytes(new ObjectId(fileId)),
                        UploadDateTime = gridFSFile.UploadDateTime.ToUniversalTime()
                    };
                }
                else
                {
                    throw new Exception($"cannot find {fileId} file.");
                }
            }
            catch { throw; }
            return(file);
        }
Beispiel #7
0
        /// <summary>
        /// Update dump of the file on disks.
        /// </summary>
        /// <param name="fileId"></param>
        /// <param name="originalBlobBucket"></param>
        /// <param name="deleted">If true this is an operation of deletion of the artifact.</param>
        /// <returns></returns>
        private Boolean UpdateDumpOfFile(
            string fileId,
            GridFSBucket <String> originalBlobBucket,
            Boolean deleted)
        {
            var findIdFilter = Builders <GridFSFileInfo <string> > .Filter.Eq(x => x.Id, fileId);

            var source = originalBlobBucket.Find(findIdFilter).FirstOrDefault();

            if (source == null)
            {
                return(false); //source stream does not exists
            }
            if (!deleted)
            {
                using (var stream = originalBlobBucket.OpenDownloadStream(fileId))
                {
                    _store.Store(stream, source.Filename, fileId);
                }
            }
            else
            {
                _store.Delete(source.Filename, fileId);
            }

            return(true);
        }
        public List <GridFSFileInfo> GetGridFSFiles(FilterDefinition <GridFSFileInfo> filter)
        {
            GridFSBucket          gridFS      = new GridFSBucket(this.GetDB());
            List <GridFSFileInfo> gridFSFiles = gridFS.Find(filter).ToList();

            return(gridFSFiles);
        }
        public byte[] ReadImage(string cardNumber)
        {
            MongoClient client = connection();
            var         db     = client.GetDatabase("CreditCardIMG");
            var         bucket = new GridFSBucket(db);
            var         filter = Builders <GridFSFileInfo> .Filter.And(
                Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, cardNumber));

            var options = new GridFSFindOptions
            {
                Limit = 1
            };

            using (var cursor = bucket.Find(filter, options))
            {
                var fileInfo = cursor.ToList().FirstOrDefault();
                if (fileInfo == null)
                {
                    return(ReadImage("000000001111")); //Image Not Available.
                }
                else
                {
                    var imageToReturn = bucket.DownloadAsBytes(fileInfo.Id);
                    return(imageToReturn);
                }
            }
        }
Beispiel #10
0
        public IFileProperties GetProperties(string fileReference)
        {
            var id = new ObjectId(fileReference);
            FilterDefinition <GridFSFileInfo> filter = Builders <GridFSFileInfo> .Filter.Eq("_id", id);

            var fs    = new GridFSBucket(_context.Database);
            var files = fs.Find(filter).ToList();

            if (files != null)
            {
                var fileInfo = files.FirstOrDefault();
                if (fileInfo != null)
                {
                    return(new FileProperties
                    {
                        FileReference = fileInfo.Id.ToString(),
                        FileName = fileInfo.Filename,
                        FileType = Path.GetExtension(fileInfo.Filename)?.Substring(1),
                        FileSize = fileInfo.Length,
                        ContentType = fileInfo.Metadata?.GetValue("contentType").ToString()
                    });
                }
            }
            return(null);
        }
        private void button5_Click(object sender, EventArgs e)        //download
        {
            //var client = new MongoClient("mongodb+srv://admin:[email protected]/group1db?retryWrites=true&w=majority");
            var client   = new MongoClient("mongodb+srv://admin:[email protected]/group1db?retryWrites=true&w=majority");          //personal mongoDB account made to test after issues with provided one
            var database = client.GetDatabase("group1db");
            var fs       = new GridFSBucket(database);
            //var collecFiles = database.GetCollection<BsonDocument>("fs.files");
            var filter = Builders <GridFSFileInfo> .Filter.And(Builders <GridFSFileInfo> .Filter.Regex(x => x.Filename, "csv"));

            var list     = fs.Find(filter).ToList();
            var userpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var endpath  = userpath + "/Analysis/";

            Directory.CreateDirectory(endpath);
            foreach (GridFSFileInfo doc in list)
            {
                string path = endpath + doc.Filename;
                using (var stream = File.OpenWrite(path))
                {
                    fs.DownloadToStream(doc.Id, stream);
                }
            }
            //Analysis.LoadJson();
            debugLabel.Text = "Download Complete";
        }
        public void DeleteDocument(string documentKey)
        {
            metadataCollection.DeleteOne($"{{{IdKey}: '{documentKey}'}}");
            var file = fileBucket.Find(Builders <GridFSFileInfo> .Filter.Eq(e => e.Filename, documentKey)).FirstOrDefault();

            if (file != null)
            {
                fileBucket.Delete(file.Id);
            }
        }
 public Task <IActionResult> Get(string id)
 {
     return(this.InvokeAsync(() => {
         var bucket = new GridFSBucket(this.GetMonDatabase().MongoDatabase);
         var objId = new ObjectId(id);
         var fileInfo = bucket.Find(new BsonDocument("_id", objId)).FirstOrDefault();
         var contentType = fileInfo.Metadata["ContentType"].ToString();
         var output = bucket.OpenDownloadStream(objId);
         return File(output, contentType, fileInfo.Filename);
     }));
 }
        public List <GridFSFileInfo> Find(FilterDefinition <GridFSFileInfo> filter, GridFSFindOptions options = null)
        {
            MongoClient client = GetClient(m_connectionStr);
            var         db     = client.GetDatabase(DatabaseName);
            var         bucket = new GridFSBucket(db, BucksOptions);

            using (var cursor = bucket.Find(filter, options))
            {
                return(cursor.ToList());
            }
        }
Beispiel #15
0
        public override IEnumerable <string> GetAttachmentNames(string id)
        {
            var bucket = new GridFSBucket(database, new GridFSBucketOptions()
            {
                BucketName = "attachments"
            });

            var document = bucket.Find(Builders <GridFSFileInfo> .Filter.Eq(e => e.Metadata["document-id"], id)).ToList();

            return(document.Select(e => e.Metadata["attachment-name"].AsString));
        }
Beispiel #16
0
        private GridFSFileInfo CheckGridFsFileExists(GridFSBucket gridFS, ObjectId id)
        {
            var filter = Builders <GridFSFileInfo> .Filter.Where(p => p.Id == id);

            var fs = gridFS.Find(filter).FirstOrDefault();

            if (fs == null)
            {
                throw new MongoException(string.Format("标识为{0}文件在数据库{1}中没有找到。", id, gridFS.Database.DatabaseNamespace.DatabaseName));
            }
            return(fs);
        }
Beispiel #17
0
        /// <summary>
        /// 查询文件
        /// </summary>
        /// <param name="filename">文件名</param>
        /// <returns></returns>
        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);
        }
Beispiel #18
0
        private GridFSFileInfo GetMongoFile(IMongoDatabase database, string uid)
        {
            var bucket = new GridFSBucket(database, new GridFSBucketOptions
            {
                BucketName     = ChannelDataChunkAdapter.BucketName,
                ChunkSizeBytes = WitsmlSettings.ChunkSizeBytes
            });

            var mongoFileFilter = Builders <GridFSFileInfo> .Filter.Eq(fi => fi.Metadata[ChannelDataChunkAdapter.FileName], uid);

            return(bucket.Find(mongoFileFilter).FirstOrDefault());
        }
Beispiel #19
0
        protected GridFSFileInfo GetGridFsFile(GridFSBucket gridFS, ObjectId id, bool nullThrowException = true)
        {
            var filter = Builders <GridFSFileInfo> .Filter.Where(p => p.Id == id);

            var fs = gridFS.Find(filter).FirstOrDefault();

            if (fs == null && nullThrowException == true)
            {
                throw new MongoException(string.Format(FileNotExistsErrorMessage, id, gridFS.Database.DatabaseNamespace.DatabaseName));
            }
            return(fs);
        }
        public string IsExist(string tag)
        {
            string            id      = "";
            GridFSFindOptions options = new GridFSFindOptions();

            options.Limit = 1;
            var filter = MongoDB.Driver.Builders <GridFSFileInfo> .Filter.Eq("md5", tag);

            var filterDefintion = FilterDefinition <GridFSFileInfo> .Empty;

            var objFile = _gridFs.Find(filter, options);

            if (objFile == null)
            {
                return(id);
            }
            var files = objFile.ToList();

            if (files.Count > 0)
            {
                id = files.FirstOrDefault().Id.ToString();
            }
            return(id);
        }
Beispiel #21
0
        /// <summary>
        /// 根据文件名称查询GridFS中的文档
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="collectionName"></param>
        /// <returns></returns>
        public List <GridFSFileInfo> QueryGridFSByFileName(string FileName, string collectionName)
        {
            var bucket = new GridFSBucket(this.MongoDatabase, new GridFSBucketOptions
            {
                BucketName     = collectionName,
                ChunkSizeBytes = 358400,
                WriteConcern   = WriteConcern.WMajority,
                ReadPreference = ReadPreference.Secondary
            });
            var filter = Builders <GridFSFileInfo> .Filter.Eq("metadata.name", FileName);

            var gridfsList = bucket.Find(filter).ToList();

            return(gridfsList);
        }
Beispiel #22
0
        private static GridFSFileInfo FindOrCreate(GridFSBucket bucked)
        {
            while (true)
            {
                var fullName = Path.Combine(RootName, DicId);
                var file     = bucked.Find(Builders <GridFSFileInfo> .Filter.Eq(f => f.Filename, fullName)).FirstOrDefault();

                if (file != null)
                {
                    return(file);
                }

                bucked.UploadFromBytes(fullName, Array.Empty <byte>());
            }
        }
Beispiel #23
0
        public void DownloadFileAsync(String fileName, String ruta)
        {
            var           laBaseDeDatos = ConectarConBaseDeDatos();
            IGridFSBucket bucket        = new GridFSBucket(laBaseDeDatos);
            var           filter        = Builders <GridFSFileInfo <ObjectId> > .Filter.Eq(x => x.Filename, fileName);

            var searchResult = bucket.Find(filter);
            var fileEntry    = searchResult.FirstOrDefault();
            var file         = ruta + fileName;

            using (Stream fs = new FileStream(file, FileMode.CreateNew, FileAccess.Write))
            {
                bucket.DownloadToStream(fileEntry.Id, fs);
                fs.Close();
            }
        }
        /// <summary>
        /// 改变所有版本文件的名称
        /// </summary>
        /// <param name="newFileName">新的文件名</param>
        /// <param name="filter">过滤器,用于筛选出文件</param>
        /// <param name="options"></param>
        public void RenameAllRevisions(string newFileName, FilterDefinition <GridFSFileInfo> filter, GridFSFindOptions options = null)
        {
            MongoClient client = GetClient(m_connectionStr);
            var         db     = client.GetDatabase(DatabaseName);
            var         bucket = new GridFSBucket(db, BucksOptions);

            using (var cursor = bucket.Find(filter, options))
            {
                var files = cursor.ToList();

                foreach (var file in files)
                {
                    bucket.Rename(file.Id, newFileName);
                }
            }
        }
Beispiel #25
0
        public ActionResult GetImage(string id)
        {
            var gridFs = new GridFSBucket(Context.MongoDatabase);
            //var filter = Builders<GridFSFileInfo>.Filter.Eq(x => x.Filename, "please-keep-your-voice-down.png");
            var filter = Builders <GridFSFileInfo> .Filter.Eq(x => x.Metadata["Id"], new ObjectId(id));

            var imageFileInfo = gridFs.Find(filter).FirstOrDefault();

            var imageBytes = gridFs.DownloadAsBytes(imageFileInfo.Id);

            if (imageBytes == null)
            {
                return(HttpNotFound());
            }
            return(File(imageBytes, imageFileInfo.Metadata["ContentType"].ToString()));
        }
        public FileInfo Get(string id)
        {
            var fileInfo = _fileStore.Get(id);

            if (fileInfo == null)
            {
                throw new ArgumentNullException("未找到此文件");
            }
            var mongdbFileInfo = _gridFSBucket.Find(Builders <GridFSFileInfo <ObjectId> > .Filter.Eq(f => f.Metadata["fileInfoId"], id))
                                 .FirstOrDefault();

            if (mongdbFileInfo != null)
            {
                fileInfo.FileData = _gridFSBucket.DownloadAsBytes(mongdbFileInfo.Id);
            }
            return(fileInfo);
        }
 public void Download(string fileId, Stream destination)
 {
     try
     {
         GridFSBucket   gridFS     = new GridFSBucket(this.GetDB());
         GridFSFileInfo gridFSFile = gridFS.Find(Builders <GridFSFileInfo> .Filter.Eq("_id", new ObjectId(fileId))).FirstOrDefault();
         if (gridFSFile != null)
         {
             gridFS.DownloadToStream(new ObjectId(fileId), destination);
         }
         else
         {
             throw new Exception($"cannot find {fileId} file.");
         }
     }
     catch { throw; }
 }
Beispiel #28
0
        public override void RemoveAttachment(string id, string attachmentName)
        {
            var bucket = new GridFSBucket(database, new GridFSBucketOptions()
            {
                BucketName = "attachments"
            });

            var document = bucket.Find(Builders <GridFSFileInfo> .Filter.Eq(e => e.Metadata["attachment-id"], id + "-" + attachmentName)).FirstOrDefault();

            if (document != null)
            {
                bucket.Delete(document.Id);
            }
            else
            {
                throw new AttachmentNotFoundNoSQLException();
            }
        }
Beispiel #29
0
        public async Task <GridFSFileInfo> GetFileInfo(string fileName, CancellationToken token = default(CancellationToken))
        {
            if (_bucket == null)
            {
                return(null);
            }
            var filter = Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, fileName);

            var sort = Builders <GridFSFileInfo> .Sort.Descending(x => x.UploadDateTime);

            var options = new GridFSFindOptions
            {
                Limit = 1,
                Sort  = sort
            };

            return(await _bucket.Find(filter, options).FirstAsync(token));
        }
Beispiel #30
0
        public async Task <ObjectId> UploadAsync(string filename, byte[] bytes)
        {
            var bucket = new GridFSBucket(Repository.Collection.Database); //这个是初始化gridFs存储的

            // 计算MD5;
            System.Security.Cryptography.MD5 md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
            var           hash   = md5.ComputeHash(bytes);
            StringBuilder result = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                result.Append(hash[i].ToString("x2"));
            }
            var md5Str = result.ToString();
            // 处理重复文件;
            ObjectId fileId;
            var      fileList = bucket.Find(new { md5 = md5Str }.ToBsonDocument(), new GridFSFindOptions
            {
                Limit = 1
            }).ToList();

            if (fileList.Any())
            {
                var fInfo = fileList.First();
                fileId = fInfo.Id;
            }
            else
            {
                fileId = await bucket.UploadFromBytesAsync(filename, bytes, new GridFSUploadOptions
                {
                    DisableMD5 = false
                });
            }
            // 插入素材记录;
            var entity = new Material
            {
                FileId = fileId.ToString(),
                Name   = filename,
                MD5    = md5Str
            };

            Repository.Insert(entity);
            return(entity.ObjectId);
        }