/// <summary> /// Gets the cache. /// </summary> /// <returns>The cache.</returns> /// <param name="id">Identifier.</param> /// <param name="url">URL.</param> /// <param name="mediaType">Media type.</param> /// <param name="overrideCache">If set to <c>true</c> override cache.</param> public async Task <CachedMedia> GetCache(string url, string mediaType = "Image", bool overrideCache = false) { //Get cached data from current request CachedMedia cachedMedia = await this.Get(url); //Valid cache if (await _cacheValidatorService.IsCacheValid(cachedMedia, overrideCache)) { return(cachedMedia); } if (cachedMedia == null) { cachedMedia = Create(Guid.NewGuid(), url, mediaType); } try { //cachedMedia.MediaData = await _restService.GetAsync<Byte[]>(await GeneratRestUrl(cachedMedia.Url), CancellationToken.None); cachedMedia.MediaData = await _sitecoreService.GetMediaByUrl(cachedMedia.Url); await this.Save(cachedMedia); } catch (MethodFailedException ex) { _loggingService.Log("KnownError in GetCache, Url {0} . Error: {1}", url, ex.Message); } catch (System.Exception ex) { _loggingService.Log("Error in GetCache, Url {0} . Error: {1}", url, ex.Message); } return(cachedMedia); }
/// <summary> /// Process the Request. /// </summary> /// <param name="context">The HTTP context for the request.</param> /// <returns></returns> /// <remarks>Documented by Dev03, 2009-05-24</remarks> public void ProcessRequest(HttpContext context) { HttpSessionState session = context.Session; int mediaId = Convert.ToInt32(context.Request.Params["id"]); if (mediaId < 0) { int uid = FileHandlerHelpers.Login(context.Request.Params["user"], context.Request.Params["password"]); context.Response.StatusCode = 200; context.Response.Write(uid < 0 ? uid.ToString() : "TRUE"); session["uid"] = uid; } else { object uid = session["uid"]; if (uid == null || (int)uid < 0) { context.Response.StatusCode = 403; return; } CachedMedia cachedMedia; if ((cachedMedia = HttpContext.Current.Cache[mediaId.ToString()] as CachedMedia) == null) { cachedMedia = new CachedMedia(mediaId, GetMediaMimeType(mediaId)); WriteMedia(mediaId, cachedMedia.Data); HttpContext.Current.Cache.Insert(cachedMedia.Id.ToString(), cachedMedia, null, Cache.NoAbsoluteExpiration, cacheTimeSpan); } context.Response.StatusCode = 200; context.Response.ContentType = cachedMedia.MimeType; cachedMedia.Data.WriteTo(context.Response.OutputStream); } }
/// <summary> /// Delete media by id. /// </summary> /// <param name="id">Identifier.</param> public async Task <bool> Delete(Guid id) { CachedMedia current = await Get(id); if (current == null) { return(false); } return(await Delete(current.Url)); }
/// <summary> /// Get media by id. /// </summary> /// <param name="id">Media identifier.</param> public async Task <CachedMedia> Get(Guid id) { CachedMedia cachedMedia = null; try { cachedMedia = await this._asyncConnection.GetAsync <CachedMedia> (cached => cached.Id == id); } catch (System.Exception ex) { _loggingService.Log("Error in reading media from DB, id {0} . Error: {1}", id, ex.Message); throw ex; } return(cachedMedia); }
/// <summary> /// Get media by media url. /// </summary> /// <param name="url">Media URL.</param> public async Task <CachedMedia> Get(string url) { CachedMedia cachedMedia = null; try { cachedMedia = await this._asyncConnection.FindAsync <CachedMedia> (url); } catch (System.Exception ex) { _loggingService.Log("Error in reading image from DB, url {0} . Error: {1}", url, ex.Message); throw ex; } return(cachedMedia); }
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { CachedMedia cachedMedia = (CachedMedia)value; if (cachedMedia == null) { return(null); } if (cachedMedia.MediaData != null) { return(ImageFromBytes(cachedMedia)); } return(ImageFromUrl(cachedMedia.Url)); }
/// <summary> /// Save CachedMedia /// </summary> /// <param name="newMediaRequest">New media request.</param> private async Task Save(CachedMedia newMediaRequest) { newMediaRequest.UpdatedAt = DateTime.Now; CachedMedia currentImageRequest = await Get(newMediaRequest.Url); try { if (currentImageRequest == null) { await _asyncConnection.InsertAsync(newMediaRequest); } else { await _asyncConnection.UpdateAsync(newMediaRequest); } } catch (Exception ex) { this._loggingService.Log("Error in Save, Url {0} . Error: {1}", newMediaRequest.Url, ex.Message); throw ex; } }
private ImageSource ImageFromBytes(CachedMedia cachedMedia) { return(ImageSource.FromStream(() => new MemoryStream(cachedMedia.MediaData))); }
/// <summary> /// Process the Request. /// </summary> /// <param name="context">The HTTP context for the request.</param> /// <returns></returns> /// <remarks>Documented by Dev03, 2009-05-24</remarks> public void ProcessRequest(HttpContext context) { HttpSessionState session = context.Session; int mediaId = Convert.ToInt32(context.Request.Params["id"]); if (mediaId < 0) { int uid = FileHandlerHelpers.Login(context.Request.Params["user"], context.Request.Params["password"]); context.Response.StatusCode = 200; context.Response.Write(uid < 0 ? uid.ToString() : "TRUE"); session["uid"] = uid; } else { object uid = session["uid"]; if (uid == null || (int)uid < 0) { context.Response.StatusCode = 403; return; } CachedMedia cachedMedia; if ((cachedMedia = HttpContext.Current.Cache[mediaId.ToString()] as CachedMedia) == null) { cachedMedia = new CachedMedia(mediaId, GetMediaMimeType(mediaId)); WriteMedia(mediaId, cachedMedia.Data); HttpContext.Current.Cache.Insert(cachedMedia.Id.ToString(), cachedMedia, null, Cache.NoAbsoluteExpiration, cacheTimeSpan); } context.Response.StatusCode = 200; context.Response.ContentType = cachedMedia.MimeType; cachedMedia.Data.WriteTo(context.Response.OutputStream); } }