public async Task <ActionResult> FindTracks([FromQuery] string query, [FromQuery] int?offset) { var user = await GetLoginUser(); //if (user == null) return GetErrorResult("no_login"); if (query == null) { return(GetErrorResult("no_query")); } var uid = user?.id ?? 0; query = query.ToLower(); var result = Track.Includes(_context.Tracks).Where(t => (t.owner == uid || t.visibility == Visibility.Public) && // visible by user ( t.name.ToLower().Contains(query) || t.artist.ToLower().Contains(query) || t.album.ToLower().Contains(query) || t.albumArtist.ToLower().Contains(query) ) ).Skip(offset ?? 0).Take(200); return(new JsonResult(new { tracks = result.Select(x => TrackVM.FromTrack(x, _app, false)) })); }
public async Task <ActionResult> GetList(int id) { var user = await GetLoginUser(); //if (user == null) return GetErrorResult("no_login"); var list = await _context.Lists.FindAsync(id); if (list?.IsVisibleToUser(user) != true) { return(GetErrorResult("list_not_found")); } var tracks = _context.GetTracks(list.trackids) .Where(x => x.IsVisibleToUser(user)) .Select(x => TrackVM.FromTrack(x, _app, false)) .ToList(); return(new JsonResult(new { id = list.id, owner = list.owner, name = list.name, visibility = list.visibility, tracks = tracks, version = list.version })); }
public async Task <ActionResult> GetStat([FromRoute] string id) { var login = await GetLoginUser(); User user = await _context.GetUserFromIdOrName(id); if (user == null) { return(GetErrorResult("user_not_found")); } var plays = _context.Plays.Where(p => p.uid == user.id); var lastplay = await plays.OrderBy(p => p.time) .Include(p => p.Track.fileRecord) .Include(p => p.Track.files).ThenInclude(f => f.File) .LastOrDefaultAsync(); var lastPlayTime = lastplay?.time; var lastPlayTrack = lastplay?.Track; if (lastplay?.Track != null && !lastPlayTrack.IsVisibleToUser(login)) { lastPlayTrack = null; } return(new JsonResult(new { playCount = await plays.CountAsync(), lastPlayTime = lastPlayTime?.ToUniversalTime(), lastPlayTrack = lastPlayTrack == null ? null : TrackVM.FromTrack(lastPlayTrack, _app) })); }
public async Task <ActionResult> GetTrack(int id) { var user = await GetLoginUser(); //if (user == null) return GetErrorResult("no_login"); var track = await _context.GetTrack(id); if (track?.IsVisibleToUser(user) != true) { return(GetErrorResult("track_not_found")); } return(new JsonResult(TrackVM.FromTrack(track, _app, true))); }
public async Task <ActionResult> GetRecentPlays() { var plays = _context.Plays.Include(p => p.Track).Where(p => p.Track.visibility == Visibility.Public) .OrderByDescending(p => p.id).Take(20); return(new JsonResult(new { recentplays = await plays.Select(p => new { uid = p.uid, track = TrackVM.FromTrack(p.Track, _app, false), time = p.time }).ToListAsync() })); }
public async Task <ActionResult> GetMeUploads() { var user = await GetLoginUser(); if (user == null) { return(GetErrorResult("no_login")); } return(new JsonResult(new { tracks = await Track.Includes(_context.Tracks).Where(t => t.owner == user.id) .Select(x => TrackVM.FromTrack(x, _app, false)) .ToListAsync() })); }
public async Task <ActionResult> GetGroup(int id) { var user = await GetLoginUser(); var uid = user?.id ?? 0; //if (user == null) return GetErrorResult("no_login"); var result = _context.Tracks.Where(t => t.groupId == id && (t.owner == uid || t.visibility == Visibility.Public) // visible by user) ); return(new JsonResult(new { tracks = result.Select(x => TrackVM.FromTrack(x, _app, false)) })); }
private async Task <object> GetUserPlaying(User user) { var location = TrackLocation.Parse(user.last_playing); Track track = null; if (location.trackid != 0) { track = await _context.GetTrack(location.trackid); if (track?.IsVisibleToUser(user) != true) { track = null; } } return(new { location.listid, location.position, location.trackid, track = track == null ? null : TrackVM.FromTrack(track, _app) }); }
public async Task <ActionResult> PostNewFile() { if (Request.ContentType != "application/x-mcloud-upload") { return(GetErrorResult("bad_content_type")); } if (_app.StorageService.Mode != StorageMode.Direct) { return(GetErrorResult("direct_upload_disabled")); } var user = await GetLoginUser(); if (user == null) { return(GetErrorResult("no_login")); } var stream = Request.Body; // Read the Track json var jsonBytesLen = await ReadBlockLength(stream); if (jsonBytesLen < 0 || jsonBytesLen > 1000) { return(GetErrorResult("json_len_out_of_range")); } var jsonStr = await ReadString(stream, jsonBytesLen); var track = JsonSerializer.Deserialize <Track>(jsonStr); var extNamePos = track.name.LastIndexOf('.'); var extName = extNamePos >= 0 ? track.name.Substring(extNamePos + 1).ToLower() : "mp3"; if (!SupportedFileFormats.Contains(extName) && _context.User.role != UserRole.SuperAdmin) { return(GetErrorResult("unsupported_file_format")); } // Now start reading the file var fileLength = await ReadBlockLength(stream); if (fileLength < 0 || !user.AllowFileUploadSize(fileLength)) { return(GetErrorResult("file_len_out_of_range")); } // Read the stream into a temp file var tmpdir = Path.Combine(_context.MCloudConfig.StorageDir, "tracks-inprogress"); Directory.CreateDirectory(tmpdir); var filename = Guid.NewGuid().ToString("D") + "." + extName; var tmpfile = Path.Combine(tmpdir, filename); try { using (var fs = System.IO.File.Create(tmpfile)) { await stream.CopyToAsync(fs, 64 * 1024); } } catch (Exception) { System.IO.File.Delete(tmpfile); throw; } // Move the temp file to storage "tracks" directory var tracksdir = Path.Combine(_context.MCloudConfig.StorageDir, "tracks"); Directory.CreateDirectory(tracksdir); System.IO.File.Move(tmpfile, Path.Combine(tracksdir, filename)); // Fill the track info, and complete. track.owner = user.id; track.fileRecord = new StoredFile { path = "storage/tracks/" + filename, size = fileLength }; await Task.Run(() => { try { track.ReadTrackInfoFromFile(_app); } catch { } }); AddTrackWithFile(track, extName); await _context.SaveChangesAsync(); return(new JsonResult(TrackVM.FromTrack(track, _app, false)) { StatusCode = 201 }); }
public async Task <ActionResult> UploadResult([FromBody] UploadResultArg arg) { if (!_context.IsLogged) { return(GetErrorResult("no_login")); } var tagSplits = arg.Tag.Split('|'); var filepath = tagSplits[0]; var size = long.Parse(tagSplits[1]); if (tagSplits[2] != _app.SignTag(arg.Url + "|" + filepath + "|" + size)) { return(GetErrorResult("invalid_tag")); } var track = new Track { name = arg.Filename, artist = "Unknown", owner = _context.User.id, fileRecord = new StoredFile { path = "storage/" + filepath, size = size } }; var extNamePos = arg.Filename.LastIndexOf('.'); var extName = extNamePos >= 0 ? arg.Filename.Substring(extNamePos + 1).ToLower() : null; if (extName == null && _context.User.role == UserRole.SuperAdmin) { track.artist = ""; } else { Directory.CreateDirectory(Path.Combine(_context.MCloudConfig.StorageDir, "tracks")); var destFile = Path.Combine(_context.MCloudConfig.StorageDir, filepath); await _app.StorageService.GetFile(arg.Url, destFile); if (new FileInfo(destFile).Length != size) { track.DeleteFile(_app); return(GetErrorResult("wrong_size")); } await Task.Run(() => { try { track.ReadTrackInfoFromFile(_app); } catch { } }); } AddTrackWithFile(track, extName); await _context.SaveChangesAsync(); return(new JsonResult(TrackVM.FromTrack(track, _app, false)) { StatusCode = 201 }); }
public async Task <ActionResult> PutTrack(int id, TrackVM vm) { if (id != vm.id) { return(GetErrorResult("bad_request")); } var user = await GetLoginUser(); if (user == null) { return(GetErrorResult("no_login")); } var track = await _context.GetTrack(id); if (track?.IsWritableByUser(user) != true) { return(GetErrorResult("track_not_found")); } if (vm.version != null && vm.version.Value != track.version) { goto TRACK_CHANGED; } RETRY: track.name = vm.name; track.artist = vm.artist; if (vm.album != null) { track.album = vm.album; } if (vm.albumArtist != null) { track.albumArtist = vm.albumArtist; } if (vm.lyrics != null) { track.lyrics = vm.lyrics; } if (vm.visibility != null) { track.visibility = vm.visibility.Value; } if (vm.groupId != null) { track.groupId = vm.groupId.Value; } track.version++; // _context.Entry(track).State = EntityState.Modified; if (await _context.FailedSavingChanges()) { if (vm.version != null) { goto TRACK_CHANGED; } goto RETRY; } return(new JsonResult(TrackVM.FromTrack(track, _app, vm.lyrics != null))); TRACK_CHANGED: return(new JsonResult(new { error = "track_changed", track = TrackVM.FromTrack(track, _app, vm.lyrics != null) })); }