Ejemplo n.º 1
0
        private async Task <IdentityResult> UpdatePictureAsync(List <IFormFile> file, IUser user)
        {
            if (file.Count != 1)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = "Please upload a single file."
                }));
            }

            var thumbnailStream = new MemoryStream();

            try
            {
                await assetThumbnailGenerator.CreateThumbnailAsync(file[0].OpenReadStream(), thumbnailStream, 128, 128, "Crop");

                thumbnailStream.Position = 0;
            }
            catch
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = "Picture is not a valid image."
                }));
            }

            await userPictureStore.UploadAsync(user.Id, thumbnailStream);

            user.SetPictureUrlToStore();

            return(await userManager.UpdateAsync(user));
        }
Ejemplo n.º 2
0
        public Task <IActionResult> UploadPicture(List <IFormFile> file)
        {
            return(MakeChangeAsync(async user =>
            {
                if (file.Count != 1)
                {
                    return IdentityResult.Failed(new IdentityError {
                        Description = "Please upload a single file."
                    });
                }

                var thumbnailStream = new MemoryStream();
                try
                {
                    await assetThumbnailGenerator.CreateThumbnailAsync(file[0].OpenReadStream(), thumbnailStream, 128, 128, "Crop");

                    thumbnailStream.Position = 0;
                }
                catch
                {
                    return IdentityResult.Failed(new IdentityError {
                        Description = "Picture is not a valid image."
                    });
                }

                await userPictureStore.UploadAsync(user.Id, thumbnailStream);

                user.SetPictureUrlToStore();

                return await userManager.UpdateAsync(user);
            }, "Picture uploaded successfully."));
        }
Ejemplo n.º 3
0
        private async Task UpdatePictureAsync(List <IFormFile> file, string id)
        {
            if (file.Count != 1)
            {
                throw new ValidationException(T.Get("validation.onlyOneFile"));
            }

            using (var thumbnailStream = new MemoryStream())
            {
                try
                {
                    await assetThumbnailGenerator.CreateThumbnailAsync(file[0].OpenReadStream(), thumbnailStream, ResizeOptions);

                    thumbnailStream.Position = 0;
                }
                catch
                {
                    throw new ValidationException(T.Get("validation.notAnImage"));
                }

                await userPictureStore.UploadAsync(id, thumbnailStream);
            }

            var update = new UserValues {
                PictureUrl = SquidexClaimTypes.PictureUrlStore
            };

            await userService.UpdateAsync(id, update);
        }
Ejemplo n.º 4
0
        private async Task <IdentityResult> UpdatePictureAsync(List <IFormFile> file, UserWithClaims user)
        {
            if (file.Count != 1)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Description = "Please upload a single file."
                }));
            }

            using (var thumbnailStream = new MemoryStream())
            {
                try
                {
                    await assetThumbnailGenerator.CreateThumbnailAsync(file[0].OpenReadStream(), thumbnailStream, 128, 128, "Crop");

                    thumbnailStream.Position = 0;
                }
                catch
                {
                    return(IdentityResult.Failed(new IdentityError {
                        Description = "Picture is not a valid image."
                    }));
                }

                await userPictureStore.UploadAsync(user.Id, thumbnailStream);
            }

            return(await userManager.UpdateSafeAsync(user.Identity, new UserValues { PictureUrl = SquidexClaimTypes.PictureUrlStore }));
        }
Ejemplo n.º 5
0
        private async Task UploadResizedAsync(IFormFile file, string id,
                                              CancellationToken ct)
        {
            await using var assetResized = TempAssetFile.Create(file.ToAssetFile());

            var resizeOptions = new ResizeOptions
            {
                TargetWidth  = 128,
                TargetHeight = 128
            };

            try
            {
                await using (var originalStream = file.OpenReadStream())
                {
                    await using (var resizeStream = assetResized.OpenWrite())
                    {
                        await assetThumbnailGenerator.CreateThumbnailAsync(originalStream, file.ContentType, resizeStream, resizeOptions, ct);
                    }
                }
            }
            catch
            {
                throw new ValidationException(T.Get("validation.notAnImage"));
            }

            await using (var resizeStream = assetResized.OpenWrite())
            {
                await userPictureStore.UploadAsync(id, resizeStream, ct);
            }
        }
Ejemplo n.º 6
0
        private async Task <IdentityResult> UpdatePictureAsync(List <IFormFile> file, IdentityUser user)
        {
            if (file.Count != 1)
            {
                var description = T.Get("validation.onlyOneFile");

                return(IdentityResult.Failed(new IdentityError {
                    Code = "PictureNotOneFile", Description = description
                }));
            }

            using (var thumbnailStream = new MemoryStream())
            {
                try
                {
                    await assetThumbnailGenerator.CreateThumbnailAsync(file[0].OpenReadStream(), thumbnailStream, ResizeOptions);

                    thumbnailStream.Position = 0;
                }
                catch
                {
                    var description = T.Get("validation.notAnImage");

                    return(IdentityResult.Failed(new IdentityError {
                        Code = "PictureNotAnImage", Description = description
                    }));
                }

                await userPictureStore.UploadAsync(user.Id, thumbnailStream);
            }

            return(await userManager.UpdateSafeAsync(user, new UserValues { PictureUrl = SquidexClaimTypes.PictureUrlStore }));
        }