public async Task <HttpResponseMessage> Get([FromUri] AvatarRequest avatarRequest) { if (!ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest, "Bad Request")); } try { return(await Task <HttpResponseMessage> .Factory.StartNew(() => { NeeoFileInfo fileInfo = null; ulong avatarUpdatedTimeStamp = 0; int dimension = Convert.ToInt32(avatarRequest.Dimension); NeeoUser user = new NeeoUser(avatarRequest.Uid); switch (user.GetAvatarState(avatarRequest.Timestamp, false, out avatarUpdatedTimeStamp, out fileInfo)) { case AvatarState.Modified: var response = Request.CreateResponse(HttpStatusCode.OK); response.Headers.Add("ts", avatarUpdatedTimeStamp.ToString()); response.Content = new ByteArrayContent(MediaUtility.ResizeImage(fileInfo.FullPath, dimension, dimension)); //response.Content = new StreamContent(new FileStream(fileInfo.FullPath,FileMode.Open)); response.Content.Headers.ContentType = new MediaTypeHeaderValue( MimeTypeMapping.GetMimeType(fileInfo.Extension).GetDescription()); return response; case AvatarState.NotModified: return Request.CreateResponse(HttpStatusCode.NotModified); default: return Request.CreateResponse(HttpStatusCode.BadRequest); } })); } catch (AggregateException aggregateException) { Logger.LogManager.CurrentInstance.ErrorLogger.LogError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, aggregateException.Message, aggregateException, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name); } catch (Exception exception) { Logger.LogManager.CurrentInstance.ErrorLogger.LogError(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, exception.Message, exception, System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Name); } return(Request.CreateResponse(HttpStatusCode.InternalServerError)); }
/// <summary> /// Gets the user's avatar base on the previous time stamp of the avatar. /// </summary> /// <param name="userID">A string containing the user id.</param> /// <param name="timeStamp">An integer containing the time stamp that has to be matched with the existing image. It is optional.</param> /// <param name="requiredDimension">An integer specifying the dimension of the image required. It is optional.</param> public void GetUserAvatar(string userID, ulong timeStamp, uint requiredDimension) { #region log user request and response /*********************************************** * To log user request and response ***********************************************/ if (_logRequestResponse) { LogManager.CurrentInstance.InfoLogger.LogInfo( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Request ===> userID : " + userID + ", timeStamp : " + timeStamp + ", requiredDimension : " + requiredDimension); } #endregion if (!NeeoUtility.IsNullOrEmpty(userID)) { userID = HttpUtility.UrlEncode(userID); NeeoUser user = new NeeoUser(userID); string filePath = ""; ulong avatarTimeStamp; switch (user.GetAvatarState(timeStamp, out avatarTimeStamp, out filePath)) { case AvatarState.NotExist: NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.NotFound); break; case AvatarState.Modified: string url = NeeoUtility.GenerateAvatarUrl(userID, timeStamp, requiredDimension); RedirectServiceToUrl(url, avatarTimeStamp); break; case AvatarState.NotModified: NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.NotModified); break; } } else { NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.BadRequest); } }
/// <summary> /// Processes the http request to send the required response. /// </summary> /// <remarks>It is used to send image data to the requester if the request </remarks> /// <param name="context">An object holding http context object for setting up response header on sending response.</param> public void ProcessRequest(HttpContext context) { _httpContext = context; NeeoFileInfo fileInfo = null; ulong avatarTimeStamp = 0; ulong avatarUpdatedTimeStamp = 0; uint requiredDimension = 0; if (!NeeoUtility.IsNullOrEmpty(context.Request.QueryString["uid"])) { string uID = HttpUtility.UrlEncode(context.Request.QueryString["uid"].ToString()); NeeoUser user = new NeeoUser(uID); if (!NeeoUtility.IsNullOrEmpty(context.Request.QueryString["ts"])) { if (!ulong.TryParse(context.Request.QueryString["ts"], out avatarTimeStamp)) { SetResponseHeaders((int)HttpStatusCode.BadRequest); } } switch (user.GetAvatarState(avatarTimeStamp, out avatarUpdatedTimeStamp, out fileInfo)) { case AvatarState.NotExist: SetResponseHeaders((int)HttpStatusCode.BadRequest); break; case AvatarState.Modified: if (!NeeoUtility.IsNullOrEmpty(context.Request.QueryString["dim"])) { UInt32.TryParse(context.Request.QueryString["dim"], out requiredDimension); } SetResponseWithFileData(fileInfo.FullPath, avatarUpdatedTimeStamp, requiredDimension); break; case AvatarState.NotModified: SetResponseHeaders((int)HttpStatusCode.NotModified); break; } } else { SetResponseHeaders((int)HttpStatusCode.BadRequest); } }
public HttpResponseMessage GetUserAvatar([FromUri] GetUserAvatarRequest request) { if (request == null || !ModelState.IsValid) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } LogRequest(request); ulong temp = 0; //uID = HttpUtility.UrlEncode(uID); request.Uid = request.Uid.Trim(); NeeoUser user = new NeeoUser(request.Uid); NeeoFileInfo filePath = null; ulong avatarTimeStamp; switch (user.GetAvatarState(request.Ts, out avatarTimeStamp, out filePath)) { case AvatarState.NotExist: return(Request.CreateResponse(HttpStatusCode.NotFound)); case AvatarState.Modified: string url = NeeoUrlBuilder.BuildAvatarUrl(request.Uid, request.Ts, request.Dim); return(RedirectServiceToUrl(url, avatarTimeStamp)); case AvatarState.NotModified: return(Request.CreateResponse(HttpStatusCode.NotModified)); default: return(Request.CreateResponse(HttpStatusCode.InternalServerError)); } //} //else //{ // NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.Unauthorized); //} }
/// <summary> /// Gets the user's avatar base on the previous time stamp of the avatar. /// </summary> /// <param name="userID">A string containing the user id.</param> /// <param name="timeStamp">An integer containing the time stamp that has to be matched with the existing image. It is optional.</param> /// <param name="requiredDimension">An integer specifying the dimension of the image required. It is optional.</param> public void GetUserAvatar(string userID, ulong timeStamp, uint requiredDimension) { userID = (userID != null) ? userID.Trim() : userID; #region log user request and response /*********************************************** * To log user request and response ***********************************************/ if (_logRequestResponse) { LogManager.CurrentInstance.InfoLogger.LogInfo( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, System.Reflection.MethodBase.GetCurrentMethod().Name + "===>" + "Request ===> userID : " + userID + ", timeStamp : " + timeStamp + ", requiredDimension : " + requiredDimension); } #endregion // #region Verify User //var request = OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty; //string keyFromClient = request.Headers["key"]; //if (NeeoUtility.AuthenticateUserRequest(userID, keyFromClient)) //{ // #endregion ulong temp = 0; if (NeeoUtility.IsNullOrEmpty(userID) || !ulong.TryParse(userID, out temp)) { NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.BadRequest); } else { userID = HttpUtility.UrlEncode(userID); NeeoUser user = new NeeoUser(userID); NeeoFileInfo fileInfo = null; ulong avatarTimeStamp; switch (user.GetAvatarState(timeStamp, out avatarTimeStamp, out fileInfo)) { case AvatarState.NotExist: NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.NotFound); break; case AvatarState.Modified: string url = NeeoUrlBuilder.BuildAvatarUrl(userID, timeStamp, requiredDimension); RedirectServiceToUrl(url, avatarTimeStamp); break; case AvatarState.NotModified: NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.NotModified); break; } } //} //else //{ // NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.Unauthorized); //} }
///// <summary> ///// Gets profile picture of the user specified with user id. ///// </summary> ///// <param name="userID">A string containing the user id.</param> //public void GetAvatar(string userID) //{ // User user = new User(userID); // ulong avatarTimeStamp = 0; // switch (user.GetAvatarState(avatarTimeStamp)) // { // case AvatarState.NotExist: // NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.NotFound); // break; // case AvatarState.Modified: // string url = NeeoUtility.GenerateFileLink(RequestType.Profile, userID, userID); // RedirectServiceToUrl(url); // break; // case AvatarState.NotModified: // NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.NotModified); // break; // } //} ///// <summary> ///// ///// </summary> ///// <param name="userID"></param> ///// <param name="timeStamp"></param> //public void GetAvatar(string userID, ulong timeStamp) //{ // if (timeStamp.ToString().Length > 7) // { // User user = new User(userID); // ulong avatarTimeStamp; // switch (user.GetAvatarState(timeStamp)) // { // case AvatarState.NotExist: // NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.NotFound); // break; // case AvatarState.Modified: // string url = NeeoUtility.GenerateFileLink(RequestType.Profile, userID, userID); // RedirectServiceToUrl(url); // break; // case AvatarState.NotModified: // NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.NotModified); // break; // } // } // else // { // NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.BadRequest); // } //} ///// <summary> ///// ///// </summary> ///// <param name="userID"></param> ///// <param name="width"></param> ///// <param name="height"></param> //public void GetAvatar(string userID, ushort width, ushort height) //{ // //if (width > 128 && height > 128 && width == height) // //{ // // User user = new User(userID); // // if (AvatarState.Exists == user.GetAvatarState()) // // { // // string url = NeeoUtility.GenerateFileLink(RequestType.Profile, userID, userID); // // RedirectServiceToUrl(url); // // } // // else // // { // // NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode) HttpStatusCode.BadRequest); // // } // //} // //else // //{ // // NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.BadRequest); // //} //} /// <summary> /// Gets the user's avatar info base on the previous time stamp of the avatar. /// </summary> /// <param name="userID">A string containing the user id.</param> /// <param name="timeStamp">An integer containing the time stamp that has to be matched with the existing image. It is optional.</param> /// <param name="requiredDimension">An integer specifying the dimension of the image required. It is optional.</param> /// <returns>A object containing the avatar details if the provided time-stamp does not match; otherwise, not modified http status code.</returns> public ImageInfo GetAvatar(string userID, ulong timeStamp, uint requiredDimension) { #region log user request and response /*********************************************** * To log user request ***********************************************/ if (_logRequestResponse) { LogManager.CurrentInstance.InfoLogger.LogInfo( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Request ===> userID : " + userID + ", timeStamp : " + timeStamp + ", requiredDimension : " + requiredDimension); } #endregion if (!NeeoUtility.IsNullOrEmpty(userID)) { userID = HttpUtility.UrlEncode(userID); NeeoUser user = new NeeoUser(userID); string filePath = ""; ulong avatarTimeStamp; switch (user.GetAvatarState(timeStamp, out avatarTimeStamp, out filePath)) { case AvatarState.NotExist: NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.NotFound); return(null); case AvatarState.Modified: ImageInfo imageInfo = new ImageInfo(); imageInfo.ImageUrl = NeeoUtility.GenerateAvatarUrl(userID, timeStamp, requiredDimension); imageInfo.TimeStamp = avatarTimeStamp; #region log user request and response /*********************************************** * To log user response ***********************************************/ if (_logRequestResponse) { LogManager.CurrentInstance.InfoLogger.LogInfo( System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "Response ===> imageInfo : " + JsonConvert.SerializeObject(imageInfo)); } #endregion return(imageInfo); case AvatarState.NotModified: NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.NotModified); return(null); default: return(null); } } else { NeeoUtility.SetServiceResponseHeaders((CustomHttpStatusCode)HttpStatusCode.BadRequest); return(null); } }