public static string GetDropboxFolder() { if (ApplicationInstalled("Dropbox")) { string infoPath = @"Dropbox\info.json"; string jsonPath = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), infoPath); if (!File.Exists(jsonPath)) { jsonPath = Path.Combine(Environment.GetEnvironmentVariable("AppData"), infoPath); } if (!File.Exists(jsonPath)) { return(""); } string jsonContent = File.ReadAllText(jsonPath).Replace("{\"personal\":", ""); jsonContent = jsonContent.Remove(jsonContent.Length - 1, 1); DropboxJson latestBeta = JsonConvert.DeserializeObject <DropboxJson>(jsonContent); string dropboxPath = latestBeta.Path; return(dropboxPath); } else { DoDebug("Dropbox not installed"); return(""); } }
public async Task DropboxMigrationAsync(int accountId) { AccountStorage entity = await _dbContext.AccountStorages.FindAsync(accountId, (int)StorageType.Dropbox); if (entity is null) { return; } DropboxJson dbxJson = JsonConvert.DeserializeObject <DropboxJson>(entity.JsonData); using var dbx = new DropboxClient(dbxJson.JwtToken); var files = await dbx.Files.ListFolderAsync(string.Empty); dbxJson.Cursor = files.Cursor; await UpdateDbxJsonData(accountId, dbxJson); await _dbContext.SaveChangesAsync(); if (_env.IsDevelopment()) { _backgroundTaskQueue.EnqueueAsync(ct => UpdateSongsDBX(accountId, files.Entries, dbxJson.JwtToken)); } else { await UpdateSongsDBX(accountId, files.Entries, dbxJson.JwtToken); } }
public async Task <byte[]> GetFileByIdAsync(int sID, int accountID) { var entity = await _dbContext.Songs.FindAsync(sID); byte[] bytes = null; switch (entity.StorageID) { case (int)StorageType.Dropbox: { var accountStorage = await _dbContext.AccountStorages.FindAsync(accountID, (int)StorageType.Dropbox); DropboxJson dbxJson = JsonConvert.DeserializeObject <DropboxJson>(accountStorage.JsonData); var song = await _dbContext.Songs.FindAsync(sID); using var dbx = new DropboxClient(dbxJson.JwtToken); var file = await dbx.Files.DownloadAsync(song.StorageSongID); bytes = await file.GetContentAsByteArrayAsync(); break; } case (int)StorageType.GoogleDrive: { using var stream = new FileStream("googleDriveSecrets.json", FileMode.Open, FileAccess.Read); UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, _gdScopes, accountID.ToString(), CancellationToken.None, _GDdataStore); using var gdService = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Garmusic", }); var mStream = new MemoryStream(); await gdService.Files.Get(entity.StorageSongID).DownloadAsync(mStream); bytes = mStream.ToArray(); break; } } return(bytes); }
public async Task DeleteAsync(int sID, int accountID) { var entity = await _dbContext.Songs.FindAsync(sID); _dbContext.Songs.Remove(entity); await SaveAsync(); switch (entity.StorageID) { case (int)StorageType.Dropbox: { var accountStorage = _dbContext.AccountStorages.Find(accountID, entity.StorageID); DropboxJson dbxJson = JsonConvert.DeserializeObject <DropboxJson>(accountStorage.JsonData); using var dbx = new DropboxClient(dbxJson.JwtToken); await dbx.Files.DeleteV2Async(entity.StorageSongID); var files = await dbx.Files.ListFolderAsync(string.Empty); dbxJson.Cursor = files.Cursor; accountStorage.JsonData = JsonConvert.SerializeObject(dbxJson); break; } case (int)StorageType.GoogleDrive: { using var stream = new FileStream("googleDriveSecrets.json", FileMode.Open, FileAccess.Read); UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, _gdScopes, accountID.ToString(), CancellationToken.None, _GDdataStore); using var gdService = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Garmusic", }); await gdService.Files.Delete(entity.StorageSongID).ExecuteAsync(); break; } } await SaveAsync(); }
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()); }
public async Task RegisterDropboxAsync(int accountId, DropboxJson json) { AccountStorage entity = new AccountStorage() { AccountID = accountId, StorageID = (int)StorageType.Dropbox }; string entityJson = JsonConvert.SerializeObject(json); entity.JsonData = entityJson; await _dbContext.AccountStorages.AddAsync(entity); await _dbContext.SaveChangesAsync(); }
public static string GetDropboxFolder() { if (MainProgram.ApplicationInstalled("Dropbox")) { string infoPath = @"Dropbox\info.json"; string jsonPath = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), infoPath); if (!Directory.Exists(Directory.GetDirectoryRoot(jsonPath))) { return(""); } if (!File.Exists(jsonPath)) { jsonPath = Path.Combine(Environment.GetEnvironmentVariable("AppData"), infoPath); } if (!File.Exists(jsonPath)) { return(""); } string jsonContent = File.ReadAllText(jsonPath); try { DropboxJson dropboxJson = JsonConvert.DeserializeObject <DropboxJson>(jsonContent); if (dropboxJson != null) { if (dropboxJson.personal != null) { return(dropboxJson.personal.Path); } else if (dropboxJson.business != null) { return(dropboxJson.business.Path); } } } catch { MainProgram.DoDebug("Failed to deserialize Dropbox Json"); MainProgram.DoDebug(jsonContent); } } else { MainProgram.DoDebug("Dropbox not installed"); return(""); } return(""); }
public async Task <string> GetDropboxJwtAsync(int accountId) { var entity = await _dbContext.AccountStorages.FindAsync(accountId, (int)StorageType.Dropbox); if (entity is null) { return(string.Empty); } DropboxJson json = JsonConvert.DeserializeObject <DropboxJson>(entity.JsonData); if (string.IsNullOrEmpty(json.JwtToken)) { return(string.Empty); } return(json.JwtToken); }
public static string GetDropboxFolder() { if (ApplicationInstalled("Dropbox")) { string infoPath = @"Dropbox\info.json"; string jsonPath = Path.Combine(Environment.GetEnvironmentVariable("LocalAppData"), infoPath); if (!Directory.Exists(Directory.GetDirectoryRoot(jsonPath))) { return(""); } if (!File.Exists(jsonPath)) { jsonPath = Path.Combine(Environment.GetEnvironmentVariable("AppData"), infoPath); } if (!File.Exists(jsonPath)) { return(""); } string jsonContent = File.ReadAllText(jsonPath); //if (jsonContent != String.Empty) jsonContent = jsonContent.Replace("{\"personal\":", ""); else return ""; //jsonContent = jsonContent.Remove(jsonContent.Length - 1, 1); try { DropboxJson dropboxJson = JsonConvert.DeserializeObject <DropboxJson>(jsonContent); if (dropboxJson != null) { if (dropboxJson.personal != null) { return(dropboxJson.personal.Path); } } } catch { DoDebug("Failed to deserialize Dropbox Json"); DoDebug(jsonContent); } } else { DoDebug("Dropbox not installed"); return(""); } return(""); }
public async Task DropboxWebhookMigrationAsync(IEnumerable <string> storageAccountsIDs) { var accounts = await _dbContext.Accounts.Include(a => a.AccountStorages.Where(acs => acs.StorageID == (int)StorageType.Dropbox)).ToListAsync(); foreach (var acc in accounts) { if (acc.AccountStorages.Count == 0) { continue; } DropboxJson json = JsonConvert.DeserializeObject <DropboxJson>(acc.AccountStorages[0].JsonData); if (json is null) { continue; } if (!storageAccountsIDs.Contains(json.DropboxID)) { continue; } using var dbx = new DropboxClient(json.JwtToken); var files = await dbx.Files.ListFolderContinueAsync(json.Cursor); json.Cursor = files.Cursor; await UpdateDbxJsonData(acc.AccountID, json); await _dbContext.SaveChangesAsync(); if (_env.IsDevelopment()) { _backgroundTaskQueue.EnqueueAsync(ct => UpdateSongsDBX(acc.AccountID, files.Entries, json.JwtToken, true)); } else { await UpdateSongsDBX(acc.AccountID, files.Entries, json.JwtToken); } } }
private async Task UpdateDbxJsonData(int accountId, DropboxJson json) { var entity = await _dbContext.AccountStorages.FindAsync(accountId, (int)StorageType.Dropbox); entity.JsonData = JsonConvert.SerializeObject(json); }
public async Task <Song> PostAsync(IFormFile file, int accountID, int storageID) { Song song = null; switch (storageID) { case (int)StorageType.Dropbox: { var accountStorage = _dbContext.AccountStorages.Find(accountID, (int)StorageType.Dropbox); DropboxJson dbxJson = JsonConvert.DeserializeObject <DropboxJson>(accountStorage.JsonData); using var dbx = new DropboxClient(dbxJson.JwtToken); var uploaded = await dbx.Files.UploadAsync( "/" + file.FileName, WriteMode.Add.Instance, strictConflict : true, body : file.OpenReadStream()); song = new Song() { AccountID = accountID, FileName = file.FileName, StorageSongID = uploaded.Id, StorageID = storageID, Playlists = new List <Playlist>() }; break; } case (int)StorageType.GoogleDrive: { using var s = new FileStream("googleDriveSecrets.json", FileMode.Open, FileAccess.Read); UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(s).Secrets, _gdScopes, accountID.ToString(), CancellationToken.None, _GDdataStore); using var gdService = new DriveService(new BaseClientService.Initializer() { HttpClientInitializer = credential, ApplicationName = "Garmusic", }); var ids = gdService.Files.GenerateIds(); ids.Count = 1; Google.Apis.Drive.v3.Data.File f = new Google.Apis.Drive.v3.Data.File(); f.Id = (await ids.ExecuteAsync()).Ids[0]; f.Name = file.FileName; await gdService.Files.Create(f, file.OpenReadStream(), "audio/mpeg").UploadAsync(); song = new Song() { AccountID = accountID, FileName = file.FileName, StorageSongID = f.Id, StorageID = storageID, Playlists = new List <Playlist>() }; break; } } using Stream stream = file.OpenReadStream(); MetadataUtility.FillMetadata(song, stream); await PostAsync(song); await SaveAsync(); return(song); }
public async Task RegisterDropboxAsync(int accountId, DropboxJson json) { await _authRepository.RegisterDropboxAsync(accountId, json); }