public void TestAttachFile()
        {
            using (var model = Setup())
            {
                var contentObjects = model.GetAll().First();
                string fileName = "test.txt";
                byte[] data;
                using (MemoryStream stream = new MemoryStream())
                using (StreamWriter writer = new StreamWriter(stream))
                {

                    writer.Write("test file");
                    writer.Flush();
                    stream.Seek(0, SeekOrigin.Begin);
                    data = new byte[stream.Length];
                    stream.Read(data, 0, data.Length);
                }
                FileReference file = new FileReference();
                file.Filename = fileName;
                file.ContentType = model.GetMimeType(fileName);
                model.AttachFile(data, file, contentObjects);
                var savedData = model.GetFileBytes(file.FileId);
                var origHash = CalculateSHA1(data);
                var newHash = CalculateSHA1(savedData.Content);
                Assert.AreEqual(origHash, newHash);

            }
        }
Пример #2
0
        public void AttachFile(byte[] data, FileReference fileData, SCORMObject obj)
        {
            List<FileReference> files = new List<FileReference>();
            if (obj.Files != null)
            {
                files.AddRange(obj.Files);
            }

            var db = MongoDB.Driver.MongoDatabase.Create(_ConnectionString);
            var gfs = new MongoGridFS(db);
            var id = Guid.NewGuid().ToString();
            using (var file = gfs.OpenWrite(id))
            {
                file.Write(data, 0, data.Length);
                file.Flush();
            }
            var fileInfo = gfs.FindOne(id);
            gfs.SetContentType(fileInfo, fileData.ContentType);
            fileData.FileId = id;
            files.Add(fileData);
            obj.Files = files;
        }
Пример #3
0
 public void DeleteFile(FileReference fileData)
 {
     _FileStore.Delete(new ObjectId(fileData.FileId));
 }
Пример #4
0
 public void AttachFile(Stream data, FileReference fileData, SCORMObject obj)
 {
     var rawData = new byte[data.Length];
     data.Read(rawData, 0, rawData.Length);
     AttachFile(rawData, fileData, obj);
 }