public IActionResult UploadTheImage(AdminUploadImage model) { if (model == null) { return(FormJsonResult("empty model")); } if (string.IsNullOrEmpty(model.ImageForUserName) || string.IsNullOrWhiteSpace(model.ImageForUserName)) { return(FormJsonResult("empty user name")); } if (model.TheImageToUpload == null) { return(FormJsonResult("empty upload file")); } var blobData = new BlobDataDto() { FileName = model.TheImageToUpload.FileName, Stream = model.TheImageToUpload.OpenReadStream() }; // upload the image into blob storage _blobService.SaveUserProfilePicture(model.ImageForUserName, blobData); // cache the user var user = _userService.FindAll().FirstOrDefault(x => x.UserName == model.ImageForUserName); if (user != null) { _blobService.SetUserImageCached(user, true); } return(FormJsonResult("File " + blobData.FileName + " uploaded successfully.")); }
/// <summary> /// Returns the first blob search by passes pattern. /// </summary> /// <param name="path"></param> /// <returns></returns> public BlobDataDto FindBlob(string path) { if (string.IsNullOrEmpty(path) || string.IsNullOrWhiteSpace(path)) { throw new ServicesException("Null or blank path parameter FindBlob(path)"); } if (path.Contains("\\")) { throw new ServicesException("Path parameter contains slash FindBlob(path)"); } // Get a reference to our container. var container = GetContainer(); // Verified that ListBlobs does not return null if not found // There will be no null pointer exception here. var blob = container.ListBlobs(path, false).FirstOrDefault(); if (blob == null) { return(null); } // Convert to blob, read to stream and return. CloudBlockBlob blockBlob = (CloudBlockBlob)blob; var result = new BlobDataDto() { Stream = new MemoryStream(), FileName = blockBlob.Name }; blockBlob.DownloadToStream(result.Stream); return(result); }
public static void TestResizeUserFile() { var settings = Common.AppSettings.ReadSettings(); var currentDirectory = Directory.GetCurrentDirectory(); using (var fileStream = File.OpenRead(currentDirectory + "\\TestData\\mister.jpg")) { var blobService = new BlobService(settings, null); var blobData = new BlobDataDto() { FileName = "mister.jpg", Stream = fileStream }; var stream1 = blobService.ResizeToAvatar(fileStream, 100, 100); string path = @"C:\Temp\z1.jpg"; if (File.Exists(path)) { File.Delete(path); } using (var fileStream1 = File.OpenWrite(path)) { stream1.Position = 0; stream1.CopyTo(fileStream1); } } }
public IActionResult UploadAvatar(AdminUploadImage model) { // todo this method is currently not quite protected // however controller itself is marked as Authorize. if (model == null) { return(FormJsonResultError("empty model")); } if (string.IsNullOrEmpty(model.ImageForUserName) || string.IsNullOrWhiteSpace(model.ImageForUserName)) { return(FormJsonResultError("empty user name")); } if (model.TheImageToUpload == null) { return(FormJsonResultError("empty upload file")); } if (model.ImageForUserName != _userManager.GetUserName(User)) { return(FormJsonResultError("invalid user")); } var blobData = new BlobDataDto() { FileName = model.TheImageToUpload.FileName, Stream = model.TheImageToUpload.OpenReadStream() }; try { // Save image into the blob storage. _blobService.SaveUserProfilePicture(model.ImageForUserName, blobData); } catch (ServicesException ex) { return(FormJsonResultError(ex.Message)); } // cache the user var user = _userService.FindAll().FirstOrDefault(x => x.UserName == model.ImageForUserName); if (user != null) { _blobService.SetUserImageCached(user, true); } return(FormJsonResult("File " + blobData.FileName + " uploaded successfully.", "")); }
public static void TestUploadUserFile() { var settings = Common.AppSettings.ReadSettings(); var currentDirectory = Directory.GetCurrentDirectory(); using (var fileStream = File.OpenRead(currentDirectory + "\\TestData\\mister.jpg")) { var blobService = new BlobService(settings, null); var blobData = new BlobDataDto() { FileName = "mister.jpg", Stream = fileStream }; blobService.SaveUserProfilePicture("Anatoli1", blobData); } }
/// <summary> /// Upload user's avatar picture to blobs /// FileName will be taken only for extension. /// </summary> /// <param name="userName"></param> /// <param name="blobData"></param> public void SaveUserProfilePicture(string userName, BlobDataDto blobData) { // Todo check username for slashes // use image resizer to resize to a small avatar. // main avatar var path = USER_IMAGES_PATH + userName + USER_IMAGES_EXTENSION; var resizedImageStream = ResizeToAvatar(blobData.Stream, _avatarWidth, _avatarHeight); resizedImageStream.Position = 0; SaveFileToBlob(path, resizedImageStream); //blobData.Stream.Position = 0; // half avatar var pathSmall = USER_IMAGES_PATH + userName + USER_IMAGES_EXTENSION_SMALL; resizedImageStream.Position = 0; var resizedImageStreamSmall = ResizeToAvatar(resizedImageStream, _avatarWidth / 2, _avatarHeight / 2); resizedImageStreamSmall.Position = 0; SaveFileToBlob(pathSmall, resizedImageStreamSmall); }