/// <summary> /// Download the thumbnail for the specified video (only if it isn't already in our cache) /// </summary> public void DownloadThumbnail(IVideo video) { if (!_thumbnails.Contains(video)) { // if we can get a version of the thumbnail from the cache // then just save this version PluginHttpRequest pluginHttpRequest = new PluginHttpRequest(video.ThumbnailUrl, HttpRequestCacheLevel.CacheOnly); using (Stream cachedStream = pluginHttpRequest.GetResponse()) { if (cachedStream != null) { MemoryStream memoryStream = new MemoryStream(); StreamHelper.Transfer(cachedStream, memoryStream); _thumbnails[video] = new VideoThumbnail(memoryStream); } // otherwise mark it as 'downloading' and enque the download else { _thumbnails[video] = new DownloadingVideoThumbnail(); _workQueue.Enqueue(video); } } } }
/// <summary> /// Download the thumbnail -- return null if any error (connection, r/w, timeout, etc.) occurs /// </summary> private static Stream SafeDownloadThumbnail(IVideo ivideo) { try { // download the thumbnail PluginHttpRequest pluginHttpRequest = new PluginHttpRequest(ivideo.ThumbnailUrl, HttpRequestCacheLevel.CacheIfAvailable); using (Stream downloadedStream = pluginHttpRequest.GetResponse(5000)) { if (downloadedStream != null) { // transfer it to a memory stream so we don't hold open the network connection // (creating a Bitmap from a Stream requires that you hold open the Stream for // the lifetime of the Bitmap and we clearly don't want to do this for Streams // loaded from the network!) Stream memoryStream = new MemoryStream(); StreamHelper.Transfer(downloadedStream, memoryStream); return memoryStream; } else { return null; } } } catch { return null; } }
private bool ValidatePushpinUrl() { using (new WaitCursor()) { // empty url is ok, means no custom pushpin if (PushpinUrl == String.Empty) return true; // try to validate (take no more than 5 seconds) try { PluginHttpRequest request = new PluginHttpRequest(PushpinUrl, HttpRequestCacheLevel.CacheIfAvailable); using (Stream response = request.GetResponse(5000)) { if (response == null) return ShowPushpinValidationError(); else return true; } } catch (Exception) { return ShowPushpinValidationError(); } } }