Exemplo n.º 1
0
        public async Task <ActionResult <IEnumerable <Song> > > GetAllAsync()
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            var songs = await _songService.GetAllAsync(accountId);

            if (songs is null)
            {
                return(NotFound());
            }

            var result = new List <SongWeb>();

            foreach (var song in songs)
            {
                result.Add(new SongWeb()
                {
                    Id        = song.Id,
                    FileName  = song.FileName,
                    Name      = song.Name,
                    Author    = song.Author,
                    LengthSec = song.LengthSec,
                    Playlists = song.Playlists,
                    StorageID = song.StorageID
                });
            }

            return(Ok(result));
        }
Exemplo n.º 2
0
        public async Task <ActionResult> RegisterGoogleDrive(string gdCode)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            using var stream = new FileStream("googleDriveSecrets.json", FileMode.Open, FileAccess.Read);

            IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecretsStream = stream,
                Scopes    = _gdScopes,
                DataStore = _dataStore
            });

            var response = await flow.ExchangeCodeForTokenAsync(
                accountId.ToString(),
                gdCode,
                _config.GetValue <string>("GDRedirectURL"),
                CancellationToken.None
                );

            await _migService.GoogleDriveMigrationAsync(accountId);

            return(Ok());
        }
Exemplo n.º 3
0
        public async Task <ActionResult> GetFileByIdAsync(int id)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }
            var result = await _songService.GetFileByIdAsync(id, accountId);

            return(File(result, "audio/mpeg"));
        }
Exemplo n.º 4
0
        public async Task <ActionResult <string> > SignOutGoogleDrive()
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            await _authService.SignOutGoogleDrive(accountId);

            return(Ok());
        }
Exemplo n.º 5
0
        public async Task <ActionResult> Put([FromBody] Playlist playlist)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            await _playlistService.PutAsync(playlist);

            return(Ok());
        }
Exemplo n.º 6
0
        public async Task <ActionResult <IEnumerable <Song> > > GetSongsById(int id)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest(null));
            }

            var result = await _playlistService.GetSongsByPlIdAsync(id);

            return(Ok(result));
        }
Exemplo n.º 7
0
        public async Task <ActionResult <AccountWeb> > GetByIdAsync()
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            var result = await _accountService.GetByIdAsync(accountId);

            return(result is not null?Ok(result) : NotFound());
        }
Exemplo n.º 8
0
        public async Task <ActionResult <IEnumerable <Playlist> > > GetAllAsync()
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            var result = await _playlistService.GetAllAsync(accountId);

            return(Ok(result));
        }
Exemplo n.º 9
0
        public async Task <ActionResult> RegisterDropboxAsync(string dbxCode)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            using var client = new HttpClient();

            string dbxKeys = _authService.GetDropboxKeys();

            var dict = new Dictionary <string, string>();

            dict.Add("grant_type", "authorization_code");
            dict.Add("code", dbxCode);
            dict.Add("redirect_uri", _config.GetValue <string>("DropboxRedirectURL"));

            client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", dbxKeys);

            var req = new HttpRequestMessage(HttpMethod.Post, "https://api.dropbox.com/1/oauth2/token")
            {
                Content = new FormUrlEncodedContent(dict)
            };

            var response = await client.SendAsync(req);

            if (!response.IsSuccessStatusCode)
            {
                return(BadRequest());
            }

            var resBody = await response.Content.ReadAsStringAsync();

            var db = JsonConvert.DeserializeObject <DbxOAuthResponse>(resBody);

            var json = new DropboxJson()
            {
                Cursor    = string.Empty,
                DropboxID = db.Account_id,
                JwtToken  = db.Access_token
            };

            await _authService.RegisterDropboxAsync(accountId, json);

            await _migService.DropboxMigrationAsync(accountId);

            return(Ok());
        }
Exemplo n.º 10
0
        public async Task <ActionResult <Playlist> > GetByIdAsync(int plId)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            var pls = await _playlistService.GetAllAsync(accountId);

            var result = pls.FirstOrDefault(pls => pls.Id == plId);

            return(Ok(result));
        }
Exemplo n.º 11
0
        public async Task <ActionResult> DeleteAsync(int pID)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            if (!await _playlistService.CanModifyAsync(accountId, pID))
            {
                return(Unauthorized());
            }

            await _playlistService.RemoveAsync(pID);

            return(Ok());
        }
Exemplo n.º 12
0
        public async Task <ActionResult> DeleteAsync(int sID)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            if (!await _songService.CanModifyAsync(accountId, sID))
            {
                return(Unauthorized());
            }

            await _songService.DeleteAsync(sID, accountId);

            return(Ok());
        }
Exemplo n.º 13
0
        public async Task <ActionResult> Put()
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            // Method cant handle parameter
            // This is a replacement
            using StreamReader reader = new StreamReader(Request.Body, Encoding.UTF8);

            string json = await reader.ReadToEndAsync();

            if (json.Length <= 2)
            {
                return(BadRequest());
            }

            var parsedJson = json.Remove(json.Length - 1).Substring(json.IndexOf(':') + 1);

            var playlistsWatch = JsonConvert.DeserializeObject <List <PlaylistWatch> >(parsedJson);

            var playlists = new List <Playlist>();

            foreach (var plw in playlistsWatch)
            {
                playlists.Add(new Playlist()
                {
                    Id   = plw.Id,
                    Sync = plw.Sync
                });
            }

            if (playlists.Count == 0)
            {
                return(BadRequest());
            }

            await _playlistService.UpdateSyncAsync(playlists);

            return(Ok());
        }
Exemplo n.º 14
0
        public async Task <ActionResult> DeleteRangeAsync([FromBody] List <int> sIDs)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            foreach (var id in sIDs)
            {
                if (!await _songService.CanModifyAsync(accountId, id))
                {
                    return(Unauthorized());
                }
            }

            await _songService.DeleteRangeAsync(sIDs, accountId);

            return(Ok());
        }
Exemplo n.º 15
0
        public async Task <ActionResult <Song> > PostAsync([FromForm] IFormFile file, [FromForm] int storageID)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            Song song = null;

            try
            {
                song = await _songService.PostAsync(file, accountId, storageID);
            }
            catch (Exception ex)
            {
                return(BadRequest("Oops! Something went wrong, please try again later"));
            }

            return(Ok(song));
        }
Exemplo n.º 16
0
        public async Task <IActionResult> GetAllAsync()
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            var songs = await _songService.GetAllWatchAsync(accountId);

            var playlists = await _playlistService.GetAllWatchAsync(accountId);

            if (songs is null || playlists is null)
            {
                return(NotFound());
            }

            var result = new { songs = songs, playlists = playlists };

            return(Ok(result));
        }
Exemplo n.º 17
0
        public async Task <ActionResult> ChangePassword([FromForm] string oldPass, [FromForm] string newPass)
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            try
            {
                await _authService.ChangePassword(accountId, oldPass, newPass);
            }
            catch (InvalidPasswordException)
            {
                return(BadRequest("Invalid password"));
            }
            catch (Exception)
            {
                return(BadRequest("Oops! Something went wrong, please try again later"));
            }

            return(Ok());
        }
Exemplo n.º 18
0
        public ActionResult <string> GetSignInGDUrl()
        {
            int accountId = JWTUtility.GetIdFromRequestHeaders(Request.Headers);

            if (accountId == -1)
            {
                return(BadRequest());
            }

            using var stream = new FileStream("googleDriveSecrets.json", FileMode.Open, FileAccess.Read);

            IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
            {
                ClientSecretsStream = stream,
                Scopes    = _gdScopes,
                DataStore = _dataStore
            });

            var request = flow.CreateAuthorizationCodeRequest(_config.GetValue <string>("GDRedirectURL"));

            string url = request.Build().ToString();

            return(Ok(url));
        }