We have a problem to store images on mongoDB Should store link to images. and use GridFS for storing the files Example: http://stackoverflow.com/questions/4988436/mongodb-gridfs-with-c-how-to-store-files-such-as-images
Esempio n. 1
0
        public void TestDownloadImage_dishImageDataShouldReturnImage()
        {
            //Arrange
            ImageData imageData = new ImageData();
            Dish dishWithImages = new Dish();

            MongoGridFSFileInfo uploadedFile;
            using (var fs = new FileStream(fileName, FileMode.Open))
            {

                uploadedFile = dishWithImages.UploadImageToDish(m_Testdb.GridFS, fs, fileName);
            }

            //act

            //Example for controller - File(fileStream, ("image/jpg")

            Stream fileStream = GetImageStream(dishWithImages.Image, m_Testdb.GridFS);

            //assert
            Assert.IsTrue(fileStream.CanRead);
            Assert.IsTrue(fileStream.Length > 0);
        }
Esempio n. 2
0
        public void TestDownloadImage_should_existInStorage()
        {
            //Arrange
            ImageData imageData = new ImageData();
            Dish dishWithImages = new Dish();

            MongoGridFSFileInfo uploadedFile;
            using (var fs = new FileStream(fileName, FileMode.Open))
            {

                uploadedFile = dishWithImages.UploadImageToDish(m_Testdb.GridFS, fs, fileName);
            }

            //act
            var file = m_Testdb.GridFS.FindOne(Query.EQ("_id", dishWithImages.Image.Id));

            //assert
            Assert.IsTrue(file.Exists);
        }
Esempio n. 3
0
 private Stream GetImageStream(ImageData imageData)
 {
     using (Restaurants rests = new Restaurants())
     {
         var file = rests.DB.GridFS.FindOne(Query.EQ("_id", imageData.Id));
         return file.OpenRead();
     }
 }
Esempio n. 4
0
 private Stream GetImageStream(ImageData imageData, MongoGridFS mongoGridFS)
 {
     var file = mongoGridFS.FindOne(Query.EQ("_id", imageData.Id));
     return file.OpenRead();
 }
Esempio n. 5
0
        public void TestUploadImage_should_beSavedDishImageIdShouldHaveSameId()
        {
            //Arrange
            ImageData imageData = new ImageData();
            Dish dishWithImages = new Dish();

            MongoGridFSFileInfo uploadedFile;
            using (var fs = new FileStream(fileName, FileMode.Open))
            {
                //act
                uploadedFile = dishWithImages.UploadImageToDish(m_Testdb.GridFS, fs, fileName);
            }

            //assert
            Assert.IsNotNull(dishWithImages.Image);
            Assert.AreEqual(uploadedFile.Id, dishWithImages.Image.Id);
        }
Esempio n. 6
0
        public void TestSaveImage_RestaurantImageDataShouldBeSavedInFile()
        {
            //Arrange
            var newFileName = @"C:\Users\Vadik\Dropbox\Work\Downloaded_AlGauchoIcon.jpg";
            ImageData imageData = new ImageData();
            //Dish dishWithImages = new Dish();
            RestaurantBasicData restaurantWithImage = new RestaurantBasicData();

            MongoGridFSFileInfo uploadedFile;
            using (var fs = new FileStream(fileName, FileMode.Open))
            {
                uploadedFile = restaurantWithImage.UploadImageToRestaurant(m_Testdb.GridFS, fs, fileName, fileContentType);
            }

            //act
            //save image to file
            var file = m_Testdb.GridFS.FindOne(Query.EQ("_id", restaurantWithImage.Image.Id));

            using (var stream = file.OpenRead())
            {
                var bytes = new byte[stream.Length];
                stream.Read(bytes, 0, (int)stream.Length);
                using (var newFs = new FileStream(newFileName, FileMode.Create))
                {
                    newFs.Write(bytes, 0, bytes.Length);
                }
            }
            //open source and saved files
            var source = new FileStream(fileName, FileMode.Open);
            var savedFile = new FileStream(newFileName, FileMode.Open);

            //assert
            Assert.AreEqual(source.Length, savedFile.Length);
        }
Esempio n. 7
0
 public static ImageData ConvertToImageData(MongoGridFSFileInfo gridFsInfo)
 {
     ImageData returnValue = new ImageData();
     returnValue.Id = gridFsInfo.Id;
     returnValue.ImageFileName = gridFsInfo.Name;
     returnValue.CreatedAt = gridFsInfo.UploadDate;
     returnValue.ImageContentType = gridFsInfo.ContentType;
     return returnValue;
 }
Esempio n. 8
0
 // Not emplemented yet
 public ImageData GetImageDataById(string id)
 {
     ImageData returnValue = new ImageData();
     using (Restaurants rests = new Restaurants())
     {
         throw new NotImplementedException();
     }
 }