Exemple #1
0
        public async Task <object> Upload(
            [FromForm][Required] long musicId,
            [FromForm][Required][MaxLength(100)] string mapName,
            [FromForm][Required][Range(0, 100)] int difficulty,
            [FromForm][Required][MaxLength(400)] string description,
            [FromForm][Required] string content,
            [Required] IFormFile image,
            [FromServices] HashFileProvider fileProvider,
            [FromServices] AppDbContext context)
        {
            var user = await userManager.GetUserAsync(User);

            var music = await context.Musics.FindAsync(musicId);

            if (music is null || music.Deleted)
            {
                return(StatusCode(404));
            }

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

            var type = image.ContentType;

            if (!type.StartsWith("image") || image.Length > 1024 * 1024 * 5)
            {
                return(StatusCode(400));
            }
            var hash = await fileProvider.SaveFileAsync(image.OpenReadStream());

            var map = new Map()
            {
                Uploader             = user,
                Music                = music,
                MapName              = mapName,
                Difficulty           = difficulty,
                Description          = description,
                MapContent           = content,
                ImageFileHashAndType = hash + ":" + type
            };
            await context.Maps.AddAsync(map);

            await context.SaveChangesAsync();

            return(Ok(map.Id));
        }
        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());
        }
        public async Task <object> UpLoad(
            [Required] IFormFile file,
            [FromForm][Required][MaxLength(100)] string title,
            [FromForm][Required][MaxLength(100)] string artist,
            [FromForm][Required][MaxLength(100)] string titleUnicode,
            [FromForm][Required][MaxLength(100)] string artistUnicode,
            [FromForm][Required][MaxLength(400)] string description,
            [FromServices] HashFileProvider fileProvider,
            [FromServices] AppDbContext context)
        {
            var user = await userManager.GetUserAsync(User);

            if (file.ContentType != "audio/mp3" ||
                file.Length > 1024 * 1024 * 10 ||
                title.Any(c => c > 127) ||
                artist.Any(c => c > 127))
            {
                return(StatusCode(400));
            }

            var music = new Music
            {
                Uploader      = user,
                Title         = title,
                TitleUnicode  = titleUnicode,
                Artist        = artist,
                ArtistUnicode = artistUnicode,
                Description   = description,
                FileHash      = await fileProvider.SaveFileAsync(file.OpenReadStream())
            };

            context.Musics.Add(music);
            await context.SaveChangesAsync();

            return(Ok(music.Id));
        }
Exemple #4
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());
        }