Beispiel #1
0
        public async Task <IActionResult> SubmitUpdateProfile(UpdateProfileModel model, CancellationToken cancellationToken = default)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.WarningMessage = Messages.InvalidData;

                return(View("Profile", model));
            }

            try
            {
                var goblinIdentityUploadProfileUserModel = model.MapTo <GoblinIdentityUpdateProfileModel>();

                // Upload Avatar File if have

                if (model.AvatarFile != null)
                {
                    await using var memoryStream = new MemoryStream();

                    await model.AvatarFile.CopyToAsync(memoryStream, cancellationToken);

                    var fileBytes = memoryStream.ToArray();

                    var base64 = Convert.ToBase64String(fileBytes);

                    var fileName = model.AvatarFile.FileName ?? model.AvatarFile.Name ?? GoblinDateTimeHelper.SystemTimeNow.ToString("ddMMyyHHmmss");

                    var uploadResourceModel = new GoblinResourceUploadFileModel
                    {
                        LoggedInUserId        = LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id,
                        Folder                = "avatars",
                        Name                  = $"user-{LoggedInUser<GoblinIdentityUserModel>.Current.Data.Id}-avatar-{fileName}",
                        ContentBase64         = base64,
                        ImageMaxHeightPx      = 800,
                        ImageMaxWidthPx       = 800,
                        IsEnableCompressImage = true
                    };

                    var fileModel = await GoblinResourceHelper.UploadAsync(uploadResourceModel, cancellationToken).ConfigureAwait(true);

                    goblinIdentityUploadProfileUserModel.AvatarUrl = fileModel.Slug;

                    // Update Model

                    LoggedInUser <GoblinIdentityUserModel> .Current.Data.AvatarUrl = model.AvatarUrl = goblinIdentityUploadProfileUserModel.AvatarUrl;

                    model.AvatarFile = null;
                }

                // Update Profile

                await GoblinIdentityHelper.UpdateProfileAsync(LoggedInUser <GoblinIdentityUserModel> .Current.Data.Id, goblinIdentityUploadProfileUserModel, cancellationToken).ConfigureAwait(true);

                ViewBag.SuccessMessage = "Profile Updated Successfully.";
            }
            catch (GoblinException e)
            {
                ViewBag.ErrorMessage = e.ErrorModel.Message;
            }
            catch (Exception e)
            {
                ViewBag.ErrorMessage = e.Message;
            }

            return(View("Profile", model));
        }