public async void DeleteProfilePictureFromCloud(string url) { if (string.IsNullOrEmpty(url) || string.IsNullOrWhiteSpace(url)) { return; } await DropboxApi.DeleteFromDropbox(url); }
public async Task <Tuple <bool, string> > UploadUserProfilePictureAsync(IWebHostEnvironment env, IFormFile imagefile, int userID) { if (imagefile == null || imagefile.Length < 0 || imagefile.Length > ConfigContex.GetProfileImageMaxSizeInBytes()) { return(Tuple.Create(false, "File is to large or none existing")); } //store old image url, if it exist string oldImageUrl = GetUsersData(userID).profileImageUrl; // Start image upload // Create Directory if (!Directory.Exists(env.WebRootPath)) { Console.WriteLine("wwwRoot does not exist"); } // Create file uniq name var fileName = ImageProcessing.GenerateUniqFileNameFromOldName(imagefile.FileName); string wwrootPath = env.WebRootPath; wwrootPath = wwrootPath + "/"; string fullPath = wwrootPath + fileName; try { // Upload file to local storage (wwwroot) using (var fileStream = Image.FromStream(imagefile.OpenReadStream())) { // Copy image, resize it, make new image int newSize = ConfigContex.GetProfileImagePixelSize(); // Get size from appsettings Bitmap resultImage = ImageProcessing.Resize(fileStream, newSize, newSize); resultImage.Save(fullPath); } // Upload file to dropbox, then get shared link string sharedUrl = await DropboxApi.Upload(fullPath, fileName, true); // Upload url to database AddProfilePictureByUrl(sharedUrl, userID); // Delete local stored image File.Delete(fullPath); // Succes DeleteProfilePictureFromCloud(oldImageUrl); return(Tuple.Create(true, "Profile image successfully created.")); } catch (Exception e) { Console.WriteLine(e); return(Tuple.Create(false, "Error in uploading file, check if its the correct extenstion (PNG, JPG).")); } }