示例#1
0
 public IEnumerable <GridFSFileInfo> Find(FilterDefinition <GridFSFileInfo> filter)
 {
     if (filter == null)
     {
         throw new ArgumentNullException(nameof(filter));
     }
     return(_gridFsBucket.Find(filter).ToEnumerable());
 }
示例#2
0
        public IStorageFile CreateFile(string path, byte[] arr = null)
        {
            if (arr == null)
            {
                arr = new byte[0];
            }
            var id   = _bucket.UploadFromBytes(path, arr);
            var info = _bucket.Find(Builders <GridFSFileInfo> .Filter.Eq("_id", id)).First();

            return(new GridFSFileStorage(info, _bucket));
        }
        /// <summary>
        /// Renaming all revisions of a file
        /// To rename all revisions of a file you first use the Find or FindAsync method to find all the revisions,
        /// and then loop over the revisions and use the Rename or RenameAsync method to rename each revision one at a time.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newFilename"></param>

        public void RenamingAllFiles(string oldFilename, string newFilename)
        {
            //For example, to find the newest revision of the file named “securityvideo” uploaded in January 2015:

            IGridFSBucket bucket = GetGridFSBucket();

            //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);
            }

            //var filesCursor = await bucket.FindAsync(filter);
            //var files = await filesCursor.ToListAsync();

            //foreach (var file in files)
            //{
            //    await bucket.RenameAsync(file.Id, newFilename);
            //}
        }
        /// <summary>
        /// 查找一个文件信息
        /// </summary>
        /// <param name="id">文件id</param>
        /// <returns></returns>
        public GridFSFileInfo FindingFile(string filename)  //ObjectId objectId
        {
            //ObjectId objectId = new ObjectId(id);

            IGridFSBucket bucket = GetGridFSBucket();

            //FilterDefinition<GridFSFileInfo> filter = Builders<GridFSFileInfo>.Filter.Where(t => t.Id.ToString() == id);
            //FilterDefinition<GridFSFileInfo> filter = Builders<GridFSFileInfo>.Filter.Eq(t => t.Id, objectId);
            FilterDefinition <GridFSFileInfo> filter = Builders <GridFSFileInfo> .Filter.Eq(t => t.Filename, filename);

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

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

            //var cursor = bucket.Find(filter, options);
            var cursor   = bucket.Find(filter);
            var fileInfo = cursor.ToList().FirstOrDefault();

            cursor.Dispose();

            return(fileInfo);
        }
示例#5
0
        public List <GridFSFileInfo> FindFilesByTags(KeyValuePair <string, List <string> > Filter, string Bucketname)
        {
            List <FilterDefinition <GridFSFileInfo> > FilterDef = new List <FilterDefinition <GridFSFileInfo> >();

            foreach (string tag in Filter.Value)
            {
                FilterDef.Add(Builders <GridFSFileInfo> .Filter.AnyEq(Filter.Key, tag));
            }

            IEnumerable <FilterDefinition <GridFSFileInfo> > FilterFinal = new List <FilterDefinition <GridFSFileInfo> >(FilterDef);

            var filter = Builders <GridFSFileInfo> .Filter.And(FilterFinal);

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

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

            Bucket = new GridFSBucket(mongoDatabase, new GridFSBucketOptions {
                BucketName = Bucketname
            });

            using (var cursor = Bucket.Find(filter, options))
            {
                var fileInfo = cursor.ToList();
                return(fileInfo);
            }
        }
示例#6
0
        /// <summary>
        /// Get all files from database by their ids as photo models.
        /// </summary>
        /// <param name="fileIds">Ids of the files to be find.</param>
        /// <returns></returns>
        public async Task <IEnumerable <Photo> > GetPhotosByIds(IEnumerable <ObjectId> fileIds)
        {
            var resultList = new List <Photo>();

            foreach (var fileId in fileIds)
            {
                var fileContent = _gridFs.DownloadAsBytes(fileId);

                var filter = Builders <GridFSFileInfo> .Filter.Eq("_id", ObjectId.Parse(fileId.ToString()));

                var fileInfo = _gridFs.Find(filter).FirstOrDefault();

                if (fileInfo != null)
                {
                    var photoModel = new Photo
                    {
                        Id       = fileInfo.Id.ToString(),
                        Content  = fileContent,
                        FileName = fileInfo.Filename
                    };
                    resultList.Add(photoModel);
                }
            }
            return(resultList);
        }
示例#7
0
        public byte[] DownloadAsBytes(string filename)
        {
            var img = _bucket.Find(GetFilterByName(filename)).SingleOrDefault();

            if (img != null)
            {
                return(_bucket.DownloadAsBytesByName(filename));
            }
            return(default);
        public IEnumerable <GridFSFileInfo> GetFilesEnumerable()
        {
            IGridFSBucket bucket = GetGridFSBucket();

            FilterDefinition <GridFSFileInfo> filter = Builders <GridFSFileInfo> .Filter.Empty;

            var cursor = bucket.Find(filter).ToEnumerable();

            return(cursor);
        }
        public IStorageFolder GetParent()
        {
            var pathParts = _fileInfo.Filename.TrimEnd('\\').Split('\\');
            var result    = string.Empty;

            for (var i = 0; i < pathParts.Length - 1; i++)
            {
                result += pathParts[i] + "\\";
            }
            var files      = _bucket.Find(Builders <GridFSFileInfo> .Filter.Empty).ToList();
            var fileResult = files.FirstOrDefault(x => x.Filename == result);

            if (fileResult == null)
            {
                var arr = new byte[0];
                result = result.TrimEnd('\\') + '\\';
                var id = _bucket.UploadFromBytes(result, arr);
                fileResult = _bucket.Find(Builders <GridFSFileInfo> .Filter.Eq("_id", id)).First();
            }
            return(new GridFSFolderStorage(_bucket, fileResult));
        }
示例#10
0
        public async Task <long> GetSizeAsync(string fileName, CancellationToken ct = default)
        {
            var name = GetFileName(fileName, nameof(fileName));

            var file = await bucket.Find(Builders <GridFSFileInfo <string> > .Filter.Eq(x => x.Id, name)).FirstOrDefaultAsync();

            if (file == null)
            {
                throw new AssetNotFoundException(fileName);
            }

            return(file.Length);
        }
        public void DeletingAllFiles(string filename)
        {
            IGridFSBucket bucket = GetGridFSBucket();

            var filter = Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, filename);

            var filesCursor = bucket.Find(filter);
            var files       = filesCursor.ToList();

            foreach (var file in files)
            {
                bucket.Delete(file.Id);
            }
        }
示例#12
0
        private void DeleteMongoFile(IGridFSBucket bucket, string fileId)
        {
            Logger.Debug($"Deleting MongoDb Channel Data file: {fileId}");

            var filter = Builders <GridFSFileInfo> .Filter.Eq(fi => fi.Metadata[FileQueryField], fileId);

            var mongoFile = bucket.Find(filter).FirstOrDefault();

            if (mongoFile == null)
            {
                return;
            }

            bucket.Delete(mongoFile.Id);
        }
示例#13
0
        private ObjectId GetId(Uri uri)
        {
            // Attempting to match default index { "filename": 1, "uploadDate": 1 }
            // Ref: https://github.com/mongodb/specifications/blob/master/source/gridfs/gridfs-spec.rst#indexes
            var filter = Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, uri.OriginalString);

            var options = new GridFSFindOptions
            {
                Limit = 1,
                Sort  = Builders <GridFSFileInfo> .Sort.Ascending(x => x.UploadDateTime)
            };

            using (var cursor = _fileBucket.Find(filter, options))
            {
                return(cursor.FirstOrDefault()?.Id ?? ObjectId.Empty);
            }
        }
        //Each file stored in GridFS has a unique Id assigned to it, and that is the primary way of accessing the stored files.
        public List <GridFSFileInfo> FindingAllFiles()
        {
            //For example, to find the newest revision of the file named “securityvideo” uploaded in January 2015:

            IGridFSBucket bucket = GetGridFSBucket();

            //FilterDefinition<GridFSFileInfo> filter = Builders<GridFSFileInfo>.Filter.And(
            //    Builders<GridFSFileInfo>.Filter.Eq(x => x.Filename, "securityvideo"),
            //    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(2015, 2, 1, 0, 0, 0, DateTimeKind.Utc)));

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

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

            //using (var cursor = bucket.Find(filter, options))
            //{
            //    var fileInfo = cursor.ToList().FirstOrDefault();
            //    // fileInfo either has the matching file information or is null
            //}

            //using (var cursor = await bucket.FindAsync(filter, options))
            //{
            //    var fileInfo = (await cursor.ToListAsync()).FirstOrDefault();
            //    // fileInfo either has the matching file information or is null
            //}

            FilterDefinition <GridFSFileInfo> filter = Builders <GridFSFileInfo> .Filter.Empty;

            var cursor    = bucket.Find(filter);
            var fileInfos = cursor.ToList();

            cursor.Dispose();
            return(fileInfos);
        }
示例#15
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static Task <ObjectId> UploadFromBytes(string fileName, byte[] source)
        {
            //构建查询,用于检查是否已存在
            FilterDefinition <GridFSFileInfo> filter = Builders <GridFSFileInfo> .Filter.And(
                Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, fileName)
                //Builders<GridFSFileInfo>.Filter.Gte(x => x.UploadDateTime, new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc)),
                );

            //排序规则
            SortDefinition <GridFSFileInfo> sort = Builders <GridFSFileInfo> .Sort.Descending(x => x.UploadDateTime);

            //查找限制,提高处理速度
            GridFSFindOptions options = new GridFSFindOptions {
                Limit = 1,
                Sort  = sort
            };

            using (var cursor = bucket.Find(filter, options))
            {
                GridFSFileInfo fileInfo = cursor.ToList().FirstOrDefault();
                return(fileInfo == null?Task.FromResult(bucket.UploadFromBytesAsync(fileName, source).Result) : Task.FromResult(fileInfo.Id));
            }
        }
示例#16
0
        private bool HasEmployeePicture(Guid employeeId)
        {
            FilterDefinition <GridFSFileInfo> filter = CreateEmployeePictureFilter(employeeId);

            return(binaryFiles.Find(filter).Any());
        }
示例#17
0
        private static GridFSFileInfo GetMongoFile(IGridFSBucket bucket, ObjectId fileId)
        {
            var filter = Builders <GridFSFileInfo> .Filter.Eq("_id", fileId);

            return(bucket.Find(filter).FirstOrDefault());
        }