public async Task <object> UploadProfile(
            [Required] IFormFile file,
            [FromServices] HashFileProvider fileProvider,
            [FromServices] AppDbContext context,
            [FromServices] MediaFileProcessor processor)
        {
            if (!processor.TryProcessImage(file.OpenReadStream(), out var jpg,
                                           maxsize: 200 * 1024))
            {
                return(StatusCode(400));
            }
            var user = await userManager.GetUserAsync(User);

            var hash = await fileProvider.SaveFileAsync(jpg);

            if (user.ProfileFileHash != null)
            {
                fileProvider.DeleteFile(user.ProfileFileHash);
            }
            user.ProfileFileHash = hash;
            await context.SaveChangesAsync();

            return(Ok());
        }
Beispiel #2
0
        public async Task <object> Modify(
            [FromForm] long id,
            [FromForm] long?musicId,
            [FromForm][MaxLength(100)] string mapName,
            [FromForm][Range(0, 100)] int?difficulty,
            [FromForm][MaxLength(400)] string description,
            [FromForm] string content,
            IFormFile image,
            [FromServices] HashFileProvider fileProvider,
            [FromServices] AppDbContext context)
        {
            var user = await userManager.GetUserAsync(User);

            var map = await context.Maps.FindAsync(id);

            if (map is null || map.Deleted)
            {
                return(StatusCode(404));
            }
            if (map.Uploader != user)
            {
                return(StatusCode(403));
            }

            if (mapName != null && mapName.Any(c => c > 127))
            {
                return(StatusCode(400));
            }

            if (musicId != null)
            {
                var music = await context.Musics.FindAsync(musicId);

                if (music is null || music.Deleted)
                {
                    return(StatusCode(404));
                }
                map.Music = music;
            }
            if (mapName != null)
            {
                map.MapName = mapName;
            }
            if (difficulty != null)
            {
                map.Difficulty = difficulty ?? 20;
            }
            if (description != null)
            {
                map.Description = description;
            }
            if (content != null)
            {
                map.MapContent = content;
            }
            if (image != null)
            {
                var type = image.ContentType;
                if (!type.StartsWith("image") || image.Length > 1024 * 1024 * 5)
                {
                    return(StatusCode(400));
                }
                var hash = await fileProvider.SaveFileAsync(image.OpenReadStream());

                fileProvider.DeleteFile(map.ImageFileHashAndType.Split(':')[0]);
                map.ImageFileHashAndType = hash + ":" + type;
            }

            map.LastModified = DateTime.Now;

            await context.SaveChangesAsync();

            return(Ok());
        }