public void TestCopyTo() { _gridFS.Delete(Query.Null); Assert.AreEqual(0, _gridFS.Chunks.Count()); Assert.AreEqual(0, _gridFS.Files.Count()); var contents = "Hello World"; var bytes = Encoding.UTF8.GetBytes(contents); var uploadStream = new MemoryStream(bytes); var createOptions = new MongoGridFSCreateOptions { Aliases = new[] { "HelloWorld", "HelloUniverse" }, ChunkSize = _gridFS.Settings.ChunkSize, ContentType = "text/plain", Id = ObjectId.GenerateNewId(), Metadata = new BsonDocument { { "a", 1 }, { "b", 2 } }, UploadDate = DateTime.UtcNow }; var fileInfo = _gridFS.Upload(uploadStream, "HelloWorld.txt", createOptions); var copyInfo = fileInfo.CopyTo("HelloWorld2.txt"); Assert.AreEqual(2, _gridFS.Chunks.Count()); Assert.AreEqual(2, _gridFS.Files.Count()); Assert.IsNull(copyInfo.Aliases); Assert.AreEqual(fileInfo.ChunkSize, copyInfo.ChunkSize); Assert.AreEqual(fileInfo.ContentType, copyInfo.ContentType); Assert.AreNotEqual(fileInfo.Id, copyInfo.Id); Assert.AreEqual(fileInfo.Length, copyInfo.Length); Assert.AreEqual(fileInfo.MD5, copyInfo.MD5); Assert.AreEqual(fileInfo.Metadata, copyInfo.Metadata); Assert.AreEqual("HelloWorld2.txt", copyInfo.Name); Assert.AreEqual(fileInfo.UploadDate, copyInfo.UploadDate); }
/// <summary> /// Ack handle and send payload to queue, atomically. /// </summary> /// <param name="handle">handle to ack received from Get()</param> /// <param name="payload">payload to send</param> /// <param name="earliestGet">earliest instant that a call to Get() can return message</param> /// <param name="priority">priority for order out of Get(). 0 is higher priority than 1</param> /// <param name="newTimestamp">true to give the payload a new timestamp or false to use given message timestamp</param> /// <param name="streams">streams to upload into gridfs or null to forward handle's streams</param> /// <exception cref="ArgumentNullException">handle or payload is null</exception> /// <exception cref="ArgumentException">priority was NaN</exception> public void AckSend(Handle handle, BsonDocument payload, DateTime earliestGet, double priority, bool newTimestamp, IEnumerable <KeyValuePair <string, Stream> > streams) { if (handle == null) { throw new ArgumentNullException("handle"); } if (payload == null) { throw new ArgumentNullException("payload"); } if (Double.IsNaN(priority)) { throw new ArgumentException("priority was NaN", "priority"); } var toSet = new BsonDocument { { "payload", payload }, { "running", false }, { "resetTimestamp", DateTime.MaxValue }, { "earliestGet", earliestGet }, { "priority", priority }, }; if (newTimestamp) { toSet["created"] = DateTime.UtcNow; } if (streams != null) { var streamIds = new BsonArray(); foreach (var stream in streams) { streamIds.Add(gridfs.Upload(stream.Value, stream.Key).Id); } toSet["streams"] = streamIds; } //using upsert because if no documents found then the doc was removed (SHOULD ONLY HAPPEN BY SOMEONE MANUALLY) so we can just send collection.Update(new QueryDocument("_id", handle.Id), new UpdateDocument("$set", toSet), UpdateFlags.Upsert); foreach (var existingStream in handle.Streams) { existingStream.Value.Dispose(); } if (streams != null) { foreach (var existingStream in handle.Streams) { gridfs.DeleteById(existingStream.Key); } } }
public void TestUploadArrayFile2GridFS() { var server = MongoServer.Create("mongodb://*****:*****@"D:\Download\FileTestUpload\Hello2.txt", FileMode.Open, FileAccess.Read); System.IO.Stream strm = fs; List <byte> readByte = new List <byte>(); int bufferSize = 4048; byte[] buffer = new byte[bufferSize]; int readCount = strm.Read(buffer, 0, bufferSize); while (readCount > 0) { readByte.AddRange(buffer.Take(readCount).ToArray()); readCount = strm.Read(buffer, 0, bufferSize); } var micBytes = readByte.ToArray(); Stream stream = new MemoryStream(micBytes); MongoGridFS file = new MongoGridFS(db); var upFile = file.Upload(stream, "ArrayHi"); Console.WriteLine("MD5: {0}", upFile.MD5); }
/// <summary> /// 流模式添加文件 /// </summary> /// <param name="stream"></param> /// <param name="remoteFile"></param> /// <returns></returns> public MetaInfo AddStream(Stream stream, string mimetype, string remoteFile) { _logger.DebugFormat("Add File By Stream, Id {0}.", remoteFile); try { MongoGridFSCreateOptions option = new MongoGridFSCreateOptions { Id = remoteFile, UploadDate = DateTime.Now, ContentType = mimetype, }; MongoGridFS fs = new MongoGridFS(_context.DataBase); var info = fs.Upload(stream, remoteFile, option); return(new MetaInfo { fileName = remoteFile, MD5 = info.MD5, MimeType = info.ContentType, }); } catch (Exception ex) { _logger.Error(ex.Message); _logger.Error(ex.StackTrace); throw; } }
/// <summary> /// 添加本地文件 /// </summary> /// <param name="filePath">本地文件路径</param> /// <param name="remoteFile">服务Id</param> /// <returns></returns> public MetaInfo Add(string filePath, string remoteFile) { try { _logger.DebugFormat("Add File filePath:{0}, remoteId:{1}", filePath, remoteFile); MongoGridFSCreateOptions option = new MongoGridFSCreateOptions { Id = remoteFile, UploadDate = DateTime.Now, ContentType = MimeMapper.GetMimeMapping(filePath), }; using (var stream = new FileStream(filePath, FileMode.Open)) { MongoGridFS fs = new MongoGridFS(_context.DataBase); var info = fs.Upload(stream, remoteFile, option); return(new MetaInfo { fileName = remoteFile, MD5 = info.MD5, MimeType = info.ContentType, }); } } catch (Exception ex) { _logger.Error(ex.Message); _logger.Error(ex.StackTrace); throw; } }
protected void Page_Load(object sender, EventArgs e) { MongoGridFS g = Config.Current.Plugins.Get <MongoReaderPlugin>().GridFS; //Loop through each uploaded file foreach (string fileKey in HttpContext.Current.Request.Files.Keys) { HttpPostedFile file = HttpContext.Current.Request.Files[fileKey]; if (file.ContentLength <= 0) { continue; //Skip unused file controls. } //Resize to a memory stream, max 2000x2000 jpeg MemoryStream temp = new MemoryStream(4096); new ImageJob(file.InputStream, temp, new ResizeSettings("width=2000;height=2000;mode=max;format=jpg")).Build(); //Reset the stream temp.Seek(0, SeekOrigin.Begin); MongoGridFSCreateOptions opts = new MongoGridFSCreateOptions(); opts.ContentType = file.ContentType; MongoGridFSFileInfo fi = g.Upload(temp, Path.GetFileName(file.FileName), opts); lit.Text += "<img src=\"" + ResolveUrl("~/gridfs/id/") + fi.Id + ".jpg?width=100&height=100\" />"; } }
static void Main(string[] args) { MongoServer ms = MongoServer.Create(); string _dbName = "docs"; MongoDatabase md = ms.GetDatabase(_dbName); if (!md.CollectionExists(_dbName)) { md.CreateCollection(_dbName); } MongoCollection <Doc> _documents = md.GetCollection <Doc>(_dbName); _documents.RemoveAll(); //add file to GridFS MongoGridFS gfs = new MongoGridFS(md); MongoGridFSFileInfo gfsi = gfs.Upload(@"c:\mongodb.rtf"); _documents.Insert(new Doc() { DocId = gfsi.Id.AsObjectId, DocName = @"c:\foo.rtf" } ); foreach (Doc item in _documents.FindAll()) { ObjectId _documentid = new ObjectId(item.DocId.ToString()); MongoGridFSFileInfo _fileInfo = md.GridFS.FindOne(Query.EQ("_id", _documentid)); gfs.Download(item.DocName, _fileInfo); Console.WriteLine("Downloaded {0}", item.DocName); Console.WriteLine("DocName {0} dowloaded", item.DocName); } Console.ReadKey(); }
public string InserirArquivo(Stream arquivo, string nome, string contentType) { var mySetting = new MongoGridFSSettings(); var gfs = new MongoGridFS(server, database.Name, mySetting); var fileInfo = gfs.Upload(arquivo, nome); gfs.SetContentType(fileInfo, contentType); return(fileInfo.Id.AsObjectId.ToString()); }
/// <summary> /// 文件上传 /// </summary> /// <param name="collection"></param> /// <returns></returns> public UploadResult UploadFile(HttpContextBase context, string collection) { var client = MongoDbServer.MongoDbClient; var server = client.GetServer(); HttpPostedFileBase file = context.Request.Files[0]; UploadResult Result = new UploadResult() { State = UploadState.Unknown }; var Size = file.ContentLength * 1.0 / 1024 / 1024; var extName = System.IO.Path.GetExtension(file.FileName).ToLower(); Result.Url = FileDomain; Result.OriginFileName = file.FileName; if (CustomConfig.FileExts.Contains(extName)) { Random rd = new Random(); var fileId = DateTime.Now.Ticks + rd.Next(9999) + extName; if (Size < CustomConfig.CanUploadFileSize) { if (Size <= 6) { SunFile sunfile = new SunFile(); byte[] data = new byte[file.ContentLength]; file.InputStream.Read(data, 0, data.Length); sunfile.Id = fileId; sunfile.Data = data; sunfile.Time = DateTime.Now; var imgCollection = server.GetDatabase(fileDatabase).GetCollection <SunFile>(collection); imgCollection.Insert(sunfile); Result.Url += "/files/SmallFile/" + client.DbGroupName + "/" + collection + "/" + fileId; Result.State = UploadState.Success; } else { MongoGridFS gfs = new MongoGridFS(server, fileDatabase, new MongoGridFSSettings()); gfs.Upload(file.InputStream, fileId); Result.Url += "/files/LargeFile/" + client.DbGroupName + "/" + fileId; Result.State = UploadState.Success; } Result.FileId = fileId; } else { Result.State = UploadState.SizeLimitExceed; } } else { Result.State = UploadState.TypeNotAllow; } return(Result); }
public FileModel UploadFile(Stream data, FileModel fileinfo) { //if (fileinfo.ID != null) // throw new Exception("File already has id!?!?!?"); var info = _gridFS.Upload(data, fileinfo.Filename, new MongoGridFSCreateOptions { Metadata = fileinfo.ToBsonDocument() }); fileinfo.ID = info.Id.AsObjectId; return(fileinfo); }
public void CopyTo() { gridFS.Delete(Query.Null); gridFS.Chunks.Count().Should().Be(0); gridFS.Files.Count().Should().Be(0); var uploadStream = new MemoryStream(ContentBytes); var createOptions = new MongoGridFSCreateOptions { Aliases = new[] { "애국가", "HelloWorld" }, ChunkSize = gridFS.Settings.ChunkSize, ContentType = "text/plain", Id = ObjectId.GenerateNewId(), Metadata = new BsonDocument { { "a", 1 }, { "b", 2 } }, UploadDate = DateTime.UtcNow }; var fileInfo = gridFS.Upload(uploadStream, "HelloWorld.txt", createOptions); fileInfo.Should().Not.Be.Null(); var copyInfo = fileInfo.CopyTo("HelloWorld2.txt"); copyInfo.Should().Not.Be.Null(); gridFS.Chunks.Count().Should().Be(2); // 하나의 파일 크기가 ChunkSize 보다 작으므로 gridFS.Files.Count().Should().Be(2); copyInfo.Aliases.Should().Be.Null(); // Alias는 복사되지 않습니다. copyInfo.ChunkSize.Should().Be(fileInfo.ChunkSize); copyInfo.ContentType.Should().Be(fileInfo.ContentType); copyInfo.Id.Should().Not.Be(fileInfo.Id); copyInfo.Length.Should().Be(fileInfo.Length); copyInfo.MD5.Should().Be(fileInfo.MD5); Assert.AreEqual(fileInfo.Metadata, copyInfo.Metadata); copyInfo.Name.Should().Be("HelloWorld2.txt"); copyInfo.UploadDate.Should().Be(fileInfo.UploadDate); }
public void Upload() { var pathToFile = "credolab.jpg"; var fileStream = File.Open(pathToFile, FileMode.Open); var option = new MongoGridFSCreateOptions() { Id = ObjectId.GenerateNewId(), ContentType = Path.GetExtension(pathToFile), Metadata = new BsonDocument() { { "creator", "Vitek" } } }; _mongoGridFs.Upload(fileStream, Path.GetFileName(pathToFile), option); }
public void TestUploadStreamFile2GridFS() { var server = MongoServer.Create("mongodb://*****:*****@"D:\Download\FileTestUpload\Hello2.txt", FileMode.Open, FileAccess.Read); System.IO.Stream strm = fs; MongoGridFS file = new MongoGridFS(db); MongoGridFSCreateOptions op = new MongoGridFSCreateOptions(); op.Aliases = new string[] { "Home" }; var upFile = file.Upload(strm, "SayHi", op); Console.WriteLine("MD5: {0}", upFile.MD5); }
/// <summary> /// 文件流的方式保存 /// </summary> /// <param name="dataStream">数据流</param> /// <param name="fileName">保存到MongoDB中的名称</param> /// <param name="fileCollectionName">文件集合名称</param> /// <returns></returns> public virtual MongoGridFSFileInfo SaveFileByStream(Stream dataStream, string fileName, string fileCollectionName) { try { if (0 == dataStream.Length || string.IsNullOrEmpty(fileName) || string.IsNullOrEmpty(fileCollectionName)) { throw new KnownException("缺失必需数据,无法保存数据!"); } MongoGridFSSettings fsSetting = new MongoGridFSSettings() { Root = fileCollectionName }; MongoGridFS fs = new MongoGridFS(_server, _database.Name, fsSetting); return(fs.Upload(dataStream, fileName)); } catch (Exception ex) { throw ex; } }
private static void Main() { var exit = false; _mongoClient = new MongoClient("mongodb://localhost"); _db = new MongoGridFS(_mongoClient.GetServer(), "Default", new MongoGridFSSettings()); while (!exit) { Console.Write(GetPrepend()); var command = Console.ReadLine(); Debug.Assert(command != null, "command != null"); if (command.Length == 0) { continue; } //What the f**k? //Handles quotation marks in incoming commands var result = command.Split('"') .Select((element, index) => index % 2 == 0 ? element.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries) : new[] { element }) .SelectMany(element => element).ToList(); switch (result[0].ToLower()) { case "exit": exit = true; break; case "cd": if (result.Count == 2) { if (result[1] == "..") { var sarray = _currentdirectory.Split('/').Where(x => x != "").ToArray(); var str = "/"; for (var i = 0; i < sarray.Length - 1; i++) { str += sarray[i] + "/"; } _currentdirectory = str; } else { _currentdirectory += result[1] + "/"; } } else { Console.WriteLine(" Usage: cd <dir>"); } break; case "server": _mongoClient = new MongoClient("mongodb://" + result[1]); _db = new MongoGridFS(_mongoClient.GetServer(), "Default", new MongoGridFSSettings()); break; case "db": _db = new MongoGridFS(_mongoClient.GetServer(), result[1], new MongoGridFSSettings()); break; case "ls": if (_db != null) { var query = from file in _db.FindAll() where file.Name.StartsWith(_currentdirectory) select file; foreach (var file in query) { Console.WriteLine(" " + file.Name); } } break; case "put": if (result.Count == 2) { var file = new FileInfo(result[1]); if (!file.Exists) { Console.WriteLine(" File doesn't exist!"); break; } Console.Write(" Uploading " + file.Name + "..."); Debug.Assert(_db != null, "_db != null"); _db.Upload(file.FullName, _currentdirectory + file.Name); Console.WriteLine("Done!"); } else if (result.Count == 3) { var file = new FileInfo(result[1]); if (!file.Exists) { Console.WriteLine(" File doesn't exist!"); break; } Console.Write(" Uploading " + file.Name + "..."); Debug.Assert(_db != null, "_db != null"); _db.Upload(file.FullName, result[2]); Console.WriteLine("Done!"); } else { Console.WriteLine(" Usage: put <src> [dest]"); } break; case "get": if (result.Count == 2) { Debug.Assert(_db != null, "_db != null"); if (!_db.Exists(result[1])) { Console.WriteLine(" File doesn't exist!"); break; } Console.Write(" Downloading " + result[1] + "..."); _db.Download(result[1], result[1]); Console.WriteLine("Done!"); } else if (result.Count == 3) { Debug.Assert(_db != null, "_db != null"); if (!_db.Exists(result[1])) { Console.WriteLine(" File doesn't exist!"); break; } Console.Write(" Downloading " + result[1] + "..."); _db.Download(result[2], result[1]); Console.WriteLine("Done!"); } else { Console.WriteLine(" Usage: get <src> [dest]"); } break; case "del": if (result.Count == 2) { Debug.Assert(_db != null, "_db != null"); if (!_db.Exists(result[1])) { Console.WriteLine(" File doesn't exist!"); break; } _db.Delete(result[1]); } else { Console.WriteLine(" Usage: del <filename>"); } break; default: Console.WriteLine(" Invalid Command"); break; } } }
/// <summary> /// 上传文件 /// </summary> /// <remarks>Mongo允许同名文件,因为id才是主键</remarks> /// <param name="strFileName"></param> public static UploadResult UpLoadFile(String strFileName, UpLoadFileOption Option) { MongoDatabase mongoDB = SystemManager.GetCurrentDataBase(); MongoGridFS gfs = mongoDB.GetGridFS(new MongoGridFSSettings()); String RemoteName = String.Empty; if (Option.FileNameOpt == enumGFSFileName.filename) { RemoteName = new FileInfo(strFileName).Name; } else { if (Option.DirectorySeparatorChar != Path.DirectorySeparatorChar) { RemoteName = strFileName.Replace(Path.DirectorySeparatorChar, Option.DirectorySeparatorChar); } else { RemoteName = strFileName; } } try { OnActionDone(new ActionDoneEventArgs(RemoteName + " Uploading ")); if (!gfs.Exists(RemoteName)) { gfs.Upload(strFileName, RemoteName); return(UploadResult.Complete); } else { switch (Option.AlreadyOpt) { case enumGFSAlready.JustAddIt: gfs.Upload(strFileName, RemoteName); return(UploadResult.Complete); case enumGFSAlready.RenameIt: String ExtendName = new FileInfo(strFileName).Extension; String MainName = RemoteName.Substring(0, RemoteName.Length - ExtendName.Length); int i = 1; while (gfs.Exists(MainName + i.ToString() + ExtendName)) { i++; } gfs.Upload(strFileName, MainName + i.ToString() + ExtendName); return(UploadResult.Complete); case enumGFSAlready.SkipIt: return(UploadResult.Skip); case enumGFSAlready.OverwriteIt: gfs.Delete(RemoteName); gfs.Upload(strFileName, RemoteName); return(UploadResult.Complete); case enumGFSAlready.Stop: return(UploadResult.Skip); } return(UploadResult.Skip); } } catch (Exception ex) { SystemManager.ExceptionDeal(ex); return(UploadResult.Exception); } }
public ObjectId AddFile(System.IO.Stream fileStream, string fileName) { MongoGridFSFileInfo fileInfo = _gridFs.Upload(fileStream, fileName); return((ObjectId)fileInfo.Id); }
public ObjectId AddFile(Stream fileStream, string fileName) { var fileInfo = _gridFs.Upload(fileStream, fileName); return((ObjectId)fileInfo.Id); }
public void Send() { var now = DateTime.Now; var payload = new BsonDocument { { "key1", 0 }, { "key2", true } }; using (var streamOne = new MemoryStream()) using (var streamTwo = new MemoryStream()) { gridfs.Upload(streamOne, "one");//making sure same file names are ok as long as their ids are diffrent streamOne.WriteByte(111); streamTwo.WriteByte(222); streamOne.Position = 0; streamTwo.Position = 0; queue.Send(payload, now, 0.8, new Dictionary <string, Stream> { { "one", streamOne }, { "two", streamTwo } }); } var expected = new BsonDocument { //_id added below { "payload", payload }, { "running", false }, { "resetTimestamp", new BsonDateTime(DateTime.MaxValue) }, { "earliestGet", new BsonDateTime(now) }, { "priority", 0.8 }, //streams added below //created added below }; var message = collection.FindOneAs <BsonDocument>(); var actualCreated = message["created"]; expected["created"] = actualCreated; actualCreated = actualCreated.ToUniversalTime(); var actualStreamIds = message["streams"].AsBsonArray; expected["streams"] = actualStreamIds; Assert.IsTrue(actualCreated <= DateTime.UtcNow); Assert.IsTrue(actualCreated > DateTime.UtcNow - TimeSpan.FromSeconds(10)); expected.InsertAt(0, new BsonElement("_id", message["_id"])); Assert.AreEqual(expected, message); var fileOne = gridfs.FindOneById(actualStreamIds[0]); Assert.AreEqual("one", fileOne.Name); using (var stream = fileOne.OpenRead()) Assert.AreEqual(111, stream.ReadByte()); var fileTwo = gridfs.FindOneById(actualStreamIds[1]); Assert.AreEqual("two", fileTwo.Name); using (var stream = fileTwo.OpenRead()) Assert.AreEqual(222, stream.ReadByte()); }