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);
        }
        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);
        }
        public void TestSaveImage_dishImageDataShouldBeSavedInFile()
        {
            //Arrange
            var newFileName = @"C:\Users\Vadik\Dropbox\Work\Downloaded_AlGauchoIcon.jpg";
            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
            //save image to file
            var file = m_Testdb.GridFS.FindOne(Query.EQ("_id", dishWithImages.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);
            //Example for controller - File(fileStream, ("image/jpg")

            //assert
            Assert.AreEqual(source.Length, savedFile.Length);
        }
        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);
        }
Beispiel #5
0
        public bool UploadImageToDish(Dish dish, string restId, int menuPartId, string imageUrl)
        {
            log.DebugFormat("[UploadImageToDish] RestaurantId={0}, menuPartId={1}, dishId={2}, dishName={4}, ImageUrl={5}.", restId, menuPartId, dish.Id, dish.Name, imageUrl);

            try
            {
                using (Restaurants rests = new Restaurants())
                {
                    string imageName = "dish_" + restId + "_" + menuPartId + "_" + dish.Id;
                    string contentType;
                    Stream imageStream = ImageServices.urlToStream(imageUrl);
                    if (imageUrl.StartsWith("data:image"))
                    {
                        var startIndex = imageUrl.IndexOf(":") + 1;
                        var endIndex = imageUrl.IndexOf(";");
                        contentType = imageUrl.Substring(startIndex, endIndex - startIndex);
                    }
                    else{
                        contentType = getUrlContentType(imageUrl);
                    }
                    dish.UploadImageToDish(rests.DB.GridFS, imageStream, imageName, contentType);
                }
                m_backofficeServiceLayer.UpdateDish(dish, restId, menuPartId);
                return true;
            }
            catch (Exception ex)
            {
                log.DebugFormat("[UploadImageToDish] Error. Exception={0}, RestaurantId={1}, menuPartId={2}, dishId={3}, dishName={4}, ImageUrl={5}.", ex.ToString(), restId, menuPartId, dish.Id, dish.Name, imageUrl);
                return false;
            }
        }