예제 #1
0
        public Picture Save(Picture picture, byte[] image)
        {
            picture.Id = Guid.NewGuid();
            picture.Url = new Uri(this.storageContext.SaveBlob(picture.RowKey, image, "image/jpeg"));
            this.storageContext.AddEntity(picture, StorageAccountConfiguration.PicturesTable);
            this.storageContext.AddEntities(picture.Tags.Split(',').Select(t => new PictureTag { TagName = t, PictureId = picture.Id }), StorageAccountConfiguration.PictureTagTable);

            return picture;
        }
예제 #2
0
        private async void DoApiPost()
        {
            Debug.WriteLine("in the DoApiPost");
            TextBlockResponse = "Begin Post test";

            HttpMessageHandler handler = new HttpClientHandler();
            var client = new HttpClient(handler);
            client.DefaultRequestHeaders.Add("Accept", "application/json");
            client.BaseAddress = new Uri(Settings.Default.ApiUrl);

            string fileName = Path.GetFileName(_filePickerFullyQualifiedFileName);

            var picture = new Picture
            {
                Name = fileName,
                Description = TextBlockDescription,
                Tags = "Image,Test"
            };

            string filename = _filePickerFullyQualifiedFileName;

            var bitmap = Image.FromFile(filename) as Bitmap;

            var content = new MultipartFormDataContent
            {
                {new StringContent(picture.Name), AddQuotes("name")},
                {new StringContent(picture.Description), AddQuotes("description")},
                {new StringContent(picture.Tags), AddQuotes("tags")},
            };

            var image = new ByteArrayContent(ImageToByteArray(bitmap));
            image.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
            content.Add(image, "filename", "image.jpg");

            HttpResponseMessage response = await client.PostAsync("api/pictures", content);

            if (response.StatusCode == HttpStatusCode.OK)
            {
                String result = await response.Content.ReadAsStringAsync();
                var newpicture = JsonConvert.DeserializeObject<Picture>(result);
                TextBlockResponse += "\n" + newpicture.Url;
                ImageSource = newpicture.Url.ToString();
                QrCodeImage = QrCodeUtility.GetQrCodeImage(newpicture.Url.ToString());
            }
        }
        public void ShouldSaveAPicture()
        {
            var picture = new Picture() { Name = "picture1", Description = "desc of picture1", Tags = string.Empty };
            var image = new byte[] { 1, 2, 3 };

            var mockStorage = new Mock<IStorageContext>();
            mockStorage.Setup(s => s.SaveBlob(It.IsAny<string>(), It.IsAny<byte[]>(), It.IsAny<string>())).Returns(() =>
                        {
                            return "http://foo";
                        });

            var repository = new PictureRepository(mockStorage.Object);
            picture = repository.Save(picture, image);

            Assert.AreNotEqual(picture.Id, Guid.Empty);
            Assert.AreEqual(picture.Url, new Uri("http://foo"));

            mockStorage.Verify(s => s.SaveBlob(It.IsAny<string>(), It.IsAny<byte[]>(), It.IsAny<string>()), Times.Once());
            mockStorage.Verify(s => s.AddEntity(It.IsAny<Picture>(), StorageAccountConfiguration.PicturesTable), Times.Once());
            mockStorage.Verify(s => s.AddEntities(It.IsAny<IEnumerable<PictureTag>>(), StorageAccountConfiguration.PictureTagTable), Times.Once());
        }