public async Task <IHttpActionResult> InsertMomentsDetails() { if (!Request.Content.IsMimeMultipartContent()) { return(Content(HttpStatusCode.BadRequest, "Unsupported media type.")); } ImageResult imageResult = new ImageResult(); try { bool isAudio = false; bool isImage = false; string root = HttpContext.Current.Server.MapPath("~/MomentUploads"); var provider = new MultipartFormDataStreamProvider(root); await Request.Content.ReadAsMultipartAsync(provider); string StoragePath = string.Empty; Moments momentDetails = new Moments(); if (provider.FormData.Count == 0 && provider.FileData.Count == 0) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User Id is required."))); } else if (provider.FormData.Count == 0 && provider.FileData.Count > 0) { foreach (var item in provider.FileData) { File.Delete(item.LocalFileName); } return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User Id is required."))); } else if (provider.FormData.Count == 1 && provider.FileData.Count == 0) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Either an image or an audio or text and user id and parent id is required. "))); } long userId = Convert.ToInt64(provider.FormData.GetValues(0)[0]); long parentId = Convert.ToInt64(provider.FormData.GetValues(1)[0]); string message = string.Empty; if (provider.FormData.Count > 2) { message = provider.FormData.GetValues(2)[0]; } //string message = provider.FormData.GetValues(0)[1]; //moving the file to the required destination string temp = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string fileName1 = string.Empty; string path1 = string.Empty; ReturnMsg msg = new ReturnMsg(); foreach (MultipartFileData fileData in provider.FileData) { string fileName = fileData.Headers.ContentDisposition.FileName; fileName = fileName.Replace(" ", "_"); if (fileName.StartsWith("\"") && fileName.EndsWith("\"")) { fileName = fileName.Trim('"'); } if (fileName.Contains(@"/") || fileName.Contains(@"\")) { fileName = Path.GetFileName(fileName); } string extension = fileName.Split('.')[1].ToLower(); if (!validFormats.Any(a => extension.Contains(a))) { File.Delete(fileData.LocalFileName); if (provider.FileData.Count == 1) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Please upload an image or an audio file."))); } else { break; } } fileName1 = fileName.Split('.')[0] + "_" + temp + "." + extension; if (audioFormats.Any(a => extension.Contains(a))) { isImage = false; isAudio = true; StoragePath = HttpContext.Current.Server.MapPath("~/MomentUploads/Audio"); } else if (imageFormats.Any(a => extension.Contains(a))) { isImage = true; isAudio = false; StoragePath = HttpContext.Current.Server.MapPath("~/MomentUploads/Images/OriginalSize"); } path1 = Path.Combine(StoragePath, fileName1); File.Move(fileData.LocalFileName, path1); File.Delete(fileData.LocalFileName); if (isImage) { //saving the file in medium size; int width = 0; string folderName = string.Empty; width = 400; //Saving the medium size folderName = "MomentUploads/Images"; ImageUploadHelper imageUpload = new ImageUploadHelper { Width = width }; imageResult = imageUpload.RenameUploadFile(fileData, true, "", fileName1, folderName); //saving the file in thumbnail size width = 150; imageUpload = new ImageUploadHelper { Width = width }; imageResult = imageUpload.RenameUploadFile(fileData, false, "", fileName1, folderName); } UserUploads userUploads = new UserUploads(); userUploads.UserId = userId; if (isImage) { userUploads.UploadedImagePath = imageResult.ImageName; userUploads.UploadedAudioPath = string.Empty; } else if (isAudio) { userUploads.UploadedAudioPath = fileName1; userUploads.UploadedImagePath = string.Empty; } msg = _userUploadsService.InsertUserUploadDetails(userUploads); } //inserting moments in the database; long userUploadedId = 0; userUploadedId = Convert.ToInt64(msg.Message); momentDetails.Message = message; momentDetails.ParentId = parentId; momentDetails.PosterUserId = userId; momentDetails.UserUploadedId = userUploadedId; var dateTime = DateTime.UtcNow; momentDetails.PostingTime = dateTime.ToString(@"yyyy/MM/dd hh:mm tt", new CultureInfo("en-US")); _momentsService.InsertMomentDetails(momentDetails); } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, e.Message))); } return(Ok("Moment Details inserted successfully.")); }
public async Task <IHttpActionResult> UploadFile() { if (!Request.Content.IsMimeMultipartContent()) { return(Content(HttpStatusCode.BadRequest, "Unsupported media type.")); } string path = string.Empty; try { bool isVideo = false; bool isImage = false; string root = HttpContext.Current.Server.MapPath("~/ChatUploads"); var provider = new MultipartFormDataStreamProvider(root); await Request.Content.ReadAsMultipartAsync(provider); string temp = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string fileName1 = string.Empty; string StoragePath = string.Empty; foreach (MultipartFileData item in provider.FileData) { string fileName = item.Headers.ContentDisposition.FileName; fileName = fileName.Replace(" ", "_"); if (fileName.StartsWith("\"") && fileName.EndsWith("\"")) { fileName = fileName.Trim('"'); } if (fileName.Contains(@"/") || fileName.Contains(@"\")) { fileName = Path.GetFileName(fileName); } string extension = fileName.Split('.')[1].ToLower(); if (!validFormats.Any(a => extension.Contains(a))) { File.Delete(item.LocalFileName); if (provider.FileData.Count == 1) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Please upload an image or an audio file."))); } else { break; } } if (imageFormats.Any(a => extension.Contains(a))) { isImage = true; isVideo = false; StoragePath = HttpContext.Current.Server.MapPath("~/ChatUploads/Uploads/OriginalSize"); } else { isImage = false; isVideo = true; StoragePath = HttpContext.Current.Server.MapPath("~/ChatUploads/Uploads/Videos"); } fileName1 = fileName.Split('.')[0] + "_" + temp + "." + extension; string path1 = Path.Combine(StoragePath, fileName); File.Move(item.LocalFileName, path1); if (isImage) { path = imagePath + fileName; } else if (isVideo) { path = videoPath + fileName; } File.Delete(item.LocalFileName); if (isImage) { //saving the file in medium size; int width = 0; string folderName = string.Empty; width = 400; //Saving the medium size folderName = "ChatUploads/Uploads"; ImageUploadHelper imageUpload = new ImageUploadHelper { Width = width }; ImageResult imageResult = imageUpload.RenameUploadFile(item, true, "", fileName, folderName); //saving the file in thumbnail size width = 150; imageUpload = new ImageUploadHelper { Width = width }; imageResult = imageUpload.RenameUploadFile(item, false, "", fileName, folderName); path = imagePath + imageResult.ImageName; } } } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e.Message))); } return(Ok(path)); }
public async Task <IHttpActionResult> UploadUserProfileImage() { ReturnMsg msg = new ReturnMsg(); if (!Request.Content.IsMimeMultipartContent()) { return(Content(HttpStatusCode.BadRequest, "Unsupported media type.")); } try { string root = HttpContext.Current.Server.MapPath("~/ProfileImages"); var provider = new MultipartFormDataStreamProvider(root); await Request.Content.ReadAsMultipartAsync(provider); string StoragePath = HttpContext.Current.Server.MapPath("~/ProfileImages/OriginalSize"); TrevoUsers userDetails = new TrevoUsers(); if (provider.FormData.Count == 0) { File.Delete(provider.FileData[0].LocalFileName); return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "User Id is required."))); } long userId = Convert.ToInt64(provider.FormData.GetValues(0)[0]); //userDetails.UserID =provider.FormData.GetValues(0)[0]; //moving the file to the required destination string temp = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string fileName1 = string.Empty; string path1 = string.Empty; foreach (MultipartFileData fileData in provider.FileData) { string fileName = fileData.Headers.ContentDisposition.FileName; if (fileName.StartsWith("\"") && fileName.EndsWith("\"")) { fileName = fileName.Trim('"'); } if (fileName.Contains(@"/") || fileName.Contains(@"\")) { fileName = Path.GetFileName(fileName); } string extension = fileName.Split('.')[1]; if (extension.ToLower() != "jpg" && extension.ToLower() != "jpeg" && extension.ToLower() != "png") { File.Delete(fileData.LocalFileName); return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, "Please upload a file with extension in jpg or jpeg or png."))); } fileName1 = fileName.Split('.')[0] + "_" + temp + "." + extension; path1 = Path.Combine(StoragePath, fileName1); File.Move(fileData.LocalFileName, path1); File.Delete(fileData.LocalFileName); //saving the file in medium size; int width = 0; string folderName = string.Empty; width = 400; //Saving the medium size folderName = "ProfileImages"; ImageUploadHelper imageUpload = new ImageUploadHelper { Width = width }; ImageResult imageResult = imageUpload.RenameUploadFile(fileData, true, "", fileName1, folderName); //saving the file in thumbnail size width = 150; imageUpload = new ImageUploadHelper { Width = width }; imageResult = imageUpload.RenameUploadFile(fileData, false, "", fileName1, folderName); } var user = _UserService.GetUserDetailsById(userId); if (user != null) { msg = _UserService.UpdateUserProfileImage(fileName1, user.User_Id); if (!msg.IsSuccess) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "There is some error"))); } else { msg.IsSuccess = true; msg.Message = "Your profile image is successfully updated."; } } else { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "User not found."))); } } catch (Exception e) { return(ResponseMessage(Request.CreateErrorResponse(HttpStatusCode.Forbidden, e.Message))); } return(Ok(msg)); }