Exemple #1
0
        public void TestCreate1ByteFile()
        {
            gridFS.Files.RemoveAll();
            gridFS.Chunks.RemoveAll();
            gridFS.Chunks.ResetIndexCache();

            var fileInfo = gridFS.FindOne("test");

            Assert.IsNull(fileInfo);

            using (var stream = gridFS.Create("test"))
            {
                stream.WriteByte(1);
            }

            fileInfo = gridFS.FindOne("test");
            Assert.IsTrue(fileInfo.Exists);
            Assert.AreEqual(1, fileInfo.Length);
            Assert.IsNotNull(fileInfo.MD5);

            using (var stream = gridFS.OpenRead("test"))
            {
                var b = stream.ReadByte();
                Assert.AreEqual(1, b);
                b = stream.ReadByte();
                Assert.AreEqual(-1, b); // EOF
            }
        }
        public override Task <Stream> OpenAsync(string virtualPath, NameValueCollection queryString)
        {
            var _filename = virtualPath.Substring(VirtualFilesystemPrefix.Length);

            //First try to get it by id, next by filename
            if (_filename.StartsWith("id/", StringComparison.OrdinalIgnoreCase))
            {
                //Strip the extension and id/ prefix
                var sid = PathUtils.RemoveFullExtension(_filename.Substring(3));

                ObjectId id;

                if (ObjectId.TryParse(sid, out id))
                {
                    var file = _grid.FindOne(MongoDB.Driver.Builders.Query.EQ("_id", id));

                    if (file == null)
                    {
                        throw new FileNotFoundException("Failed to locate blob " + sid + " on GridFS.");
                    }

                    return(Task.FromResult <Stream>(file.OpenRead()));
                }
            }

            return(Task.FromResult <Stream>(_grid.OpenRead(_filename)));
        }
Exemple #3
0
        public Stream OpenFile(string virtualPath, bool readOnly = false)
        {
            string fixedPath = FixPath(virtualPath);

            if (!FileExists(fixedPath) && !readOnly)
            {
                InternalWriteFile(fixedPath, new MemoryStream(new byte[0]));
            }

            MongoGridFS gridFs = GetGridFS();

            if (readOnly)
            {
                return(gridFs.OpenRead(fixedPath));
            }

            MongoGridFSFileInfo fileInfo = gridFs.FindOne(fixedPath);

            gridFs.SetMetadata(fileInfo, CreateMetadata(fixedPath, false));

            return(gridFs.OpenWrite(fixedPath));
        }
Exemple #4
0
        /// <summary>
        /// 获取流
        /// </summary>
        /// <param name="remoteFile"></param>
        /// <returns></returns>
        public byte[] GetBytes(string remoteFile)
        {
            _logger.DebugFormat("Get Stream by Id {0}", remoteFile);

            try
            {
                MongoGridFS fs = new MongoGridFS(_context.DataBase);
                byte[]      bytes;
                using (MongoGridFSStream gfs = fs.OpenRead(remoteFile))
                {
                    bytes = new Byte[gfs.Length];
                    gfs.Read(bytes, 0, bytes.Length);
                }
                return(bytes);
            }
            catch (Exception ex)
            {
                _logger.Error(ex.Message);
                _logger.Error(ex.StackTrace);
                throw;
            }
        }