private async void DownloadImage(string itemPath, string widthStr, string heightStr) { if (string.IsNullOrEmpty(itemPath)) { DialogHelper.ShowSimpleDialog(this, Resource.String.text_error, Resource.String.text_empty_path); return; } try { IMediaOptionsBuilder builder = new MediaOptionsBuilder().Set; if (!string.IsNullOrEmpty(widthStr)) { var width = Int32.Parse(widthStr); builder.Width(width); } if (!string.IsNullOrEmpty(heightStr)) { var height = Int32.Parse(heightStr); builder.Height(height); } var requestBuilder = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(itemPath); IDownloadMediaOptions options = builder.Build(); if (!options.IsEmpty) { requestBuilder.DownloadOptions(options); } this.SetProgressBarIndeterminateVisibility(true); using (var session = Prefs.From(this).Session) { var response = await session.DownloadMediaResourceAsync(requestBuilder.Build()); this.SetProgressBarIndeterminateVisibility(false); using(var imageBitmap = await BitmapFactory.DecodeStreamAsync(response)) { this.targetImageView.SetImageBitmap(imageBitmap); } } } catch (Exception exception) { this.SetProgressBarIndeterminateVisibility(false); var title = this.GetString(Resource.String.text_item_received); DialogHelper.ShowSimpleDialog(this, title, exception.Message); } }
public void RequestBuilderTest() { IDownloadMediaOptions options = new MediaOptionsBuilder().Set .Width(100) .Height(200) .BackgroundColor("white") .DisplayAsThumbnail(true) .Build(); var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("/sitecore/media library/1 dot png") .DownloadOptions(options) .Database("master") .Build(); // request.ItemSource.Database = "web"; Assert.AreEqual("master", request.ItemSource.Database); }
public async void TestGetMediaWithScale() { var options = new MediaOptionsBuilder().Set .Scale(0.5f) .Build(); var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("/sitecore/media library/Images/testname222") .DownloadOptions(options) .Build(); using (var response = await this.session.DownloadMediaResourceAsync(request)) using (var ms = new MemoryStream()) { await response.CopyToAsync(ms); Assert.IsTrue(8000 > ms.Length); } }
public async void TestGetMediaAsThumbnail() { var options = new MediaOptionsBuilder().Set .DisplayAsThumbnail(true) .Build(); var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("/sitecore/media library/Images/butterfly2_large") .DownloadOptions(options) .Build(); using (var response = await this.session.DownloadMediaResourceAsync(request)) using (var ms = new MemoryStream()) { await response.CopyToAsync(ms); Assert.IsTrue(43000 > ms.Length); } }
private async void SendRequest () { try { using (ISitecoreWebApiSession session = this.instanceSettings.GetSession()) { IDownloadMediaOptions options = new MediaOptionsBuilder() .Set .Width(this.width) .Height(this.height) .BackgroundColor("white") .Build(); string path = this.MediaPathTextField.Text; var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(path) .DownloadOptions(options) .Build(); byte[] data = null; using (Stream response = await session.DownloadMediaResourceAsync(request)) using (MemoryStream responseInMemory = new MemoryStream()) { await response.CopyToAsync(responseInMemory); data = responseInMemory.ToArray(); } BeginInvokeOnMainThread(delegate { using ( UIImage image = new UIImage(NSData.FromArray(data)) ) { // no need disposing // since this.ImageView.Image creates a // new C# object on each call this.ImageView.Image = image; // Update Overlay this.HideLoader(); } }); } } catch(Exception e) { AlertHelper.ShowLocalizedAlertWithOkOption("Error", e.Message); } finally { BeginInvokeOnMainThread(delegate { this.HideLoader(); }); } }
private static async void __Main_Media(string[] args) { string instanceUrl = "http://mobiledev1ua1.dk.sitecore.net:7220"; using ( var session = SitecoreWebApiSessionBuilder.AnonymousSessionWithHost(instanceUrl) .Site("/sitecore/shell") .DefaultDatabase("web") .DefaultLanguage("en") .MediaLibraryRoot("/sitecore/media library") .MediaPrefix("~/media/") .DefaultMediaResourceExtension("ashx") .BuildReadonlySession() ) { string mediaPath = "/sitecore/media library/Images/butterfly2_large"; var options = new MediaOptionsBuilder().Set .MaxWidth(1920) .MaxHeight(1080) .Width(1024) .Height(768) .BackgroundColor("red") .DisableMediaCache(false) .AllowStrech(false) .Scale(1.0f) .DisplayAsThumbnail(false) .Build(); var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(mediaPath) .Database("master") .Language("fr") .Version(1) .DownloadOptions(options) .Build(); Stream response = await session.DownloadMediaResourceAsync(request); } }
public async void TestGetMediaWithHeightAndWidthAndAllowSrtech() { const int Height = 200; const int Width = 300; var options = new MediaOptionsBuilder().Set .Height(Height) .Width(Width) .AllowStrech(true) .Build(); var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("/sitecore/media library/Images/kirkorov") .DownloadOptions(options) .Build(); using (var response = await this.session.DownloadMediaResourceAsync(request)) using (var ms = new MemoryStream()) { await response.CopyToAsync(ms); Assert.IsTrue(14300 > ms.Length); } }
public async void TestGetMediaWithInternationalPath() { var options = new MediaOptionsBuilder().Set .Width(50) .Build(); var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("~/media/Images/files/では/flowers") .DownloadOptions(options) .Database("master") .Build(); using (var response = await this.session.DownloadMediaResourceAsync(request)) using (var ms = new MemoryStream()) { await response.CopyToAsync(ms); Assert.AreEqual(7654, ms.Length); } }
public async void TestGetMediaItemWithMp4Extension() { const string ItemPath = "/sitecore/media library/Images/Files/Video_01"; const string MediaPath = "~/media/Images/Files/Video_01.mp4"; const string Db = "master"; var options = new MediaOptionsBuilder().Set .Height(50) .Build(); var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(MediaPath) .DownloadOptions(options) .Database(Db) .Build(); using (var response = await this.session.DownloadMediaResourceAsync(request)) using (var ms = new MemoryStream()) { await response.CopyToAsync(ms); var expectedItem = await this.GetItemByPath(ItemPath, Db); Assert.AreEqual(expectedItem["size"].RawValue, ms.Length.ToString(CultureInfo.InvariantCulture)); } }
public async void TestGetItemWithTildaInPath() { var options = new MediaOptionsBuilder().Set .DisplayAsThumbnail(false) .AllowStrech(true) .Height(150) .Build(); var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("~/media/Images/green_mineraly1") .DownloadOptions(options) .Build(); using (var response = await this.session.DownloadMediaResourceAsync(request)) using (var ms = new MemoryStream()) { await response.CopyToAsync(ms); Assert.AreEqual(16284, ms.Length); } }
public async void TestMediaWithoutAccessToFolder() { const string MediaPath = "/sitecore/media library/Images/kirkorov"; var sessionNoReadAccess = SitecoreWebApiSessionBuilder.AuthenticatedSessionWithHost(this.testData.InstanceUrl) .Credentials(this.testData.Users.NoReadUserExtranet) .BuildReadonlySession(); var options = new MediaOptionsBuilder().Set .Scale(1) .Build(); var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath(MediaPath) .DownloadOptions(options) .Build(); using (var response = await sessionNoReadAccess.DownloadMediaResourceAsync(request)) using (var ms = new MemoryStream()) { await response.CopyToAsync(ms); var expectedItem = await this.GetItemByPath(MediaPath); Assert.AreEqual(expectedItem["size"].RawValue, ms.Length.ToString(CultureInfo.InvariantCulture)); } }
public void TestGetMediaFromInvalidImageReturnsError() { var options = new MediaOptionsBuilder().Set .Height(100) .Build(); var request = ItemWebApiRequestBuilder.DownloadResourceRequestWithMediaPath("/sitecore/media library/Images/nexus") .DownloadOptions(options) .Build(); TestDelegate testCode = async () => { var task = this.session.DownloadMediaResourceAsync(request); await task; }; Exception exception = Assert.Throws<LoadDataFromNetworkException>(testCode); Assert.IsTrue(exception.Message.Contains("Unable to download data from the internet")); Assert.AreEqual("System.Net.Http.HttpRequestException", exception.InnerException.GetType().ToString()); // Windows : "Response status code does not indicate success: 404 (Not Found)" // iOS : "404 (Not Found)" Assert.IsTrue(exception.InnerException.Message.Contains("Not Found")); }