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> File( [Required] long id, [FromServices] HashFileProvider fileProvider, [FromServices] AppDbContext context) { var music = await context.Musics.FindAsync(id); if (music is null || music.Deleted) { return(StatusCode(404)); } if (music.Locked) { return(StatusCode(403)); } var fs = fileProvider.GetFileByHash(music.FileHash); return(File(fs, "audio/mp3", music.Title + ".mp3", null, EntityTagHeaderValue.Parse(new StringSegment('"' + music.FileHash + '"')), true)); }
public async Task <object> Image( [Required] long id, [FromServices] AppDbContext context, [FromServices] HashFileProvider fileProvider) { var map = await context.Maps.FindAsync(id); if (map is null || map.Deleted) { return(StatusCode(404)); } if (map.Locked) { return(StatusCode(403)); } var hashAndType = map.ImageFileHashAndType.Split(':'); var file = fileProvider.GetFileByHash(hashAndType[0]); return(File(file, hashAndType[1], null, EntityTagHeaderValue.Parse(new StringSegment('"' + hashAndType[0] + '"')), true)); }
public async Task <object> Profile( [Required] string username, [FromServices] HashFileProvider fileProvider) { var user = await userManager.FindByNameAsync(username); var hash = user?.ProfileFileHash; if (hash == null) { return(StatusCode(404)); } try { var fs = fileProvider.GetFileByHash(hash); return(File(fs, "image/jpeg", null, EntityTagHeaderValue.Parse(new StringSegment('"' + hash + '"')), true)); } catch (FileNotFoundException) { return(StatusCode(404)); } }
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)); }
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()); }