1var folderPath = 'learnmusiconline'; 2const msalRequest = { 3 scopes: [ 4 'user.read' 5 ] 6} 7 8/* 9const msalRequest = { 10 scopes: [ 11 'user.read', 12 'files.readwrite' 13 ] 14} 15*/ 16/* 17const msalConfigTest = { 18 auth: { 19 clientId: '12a10271-c3a5-4ed6-8a54-1a100ad4bb25', 20 validateAuthority: false, 21 authority: "https://login.microsoftonline.com/common", 22 knownAuthorities: [], 23 redirectUri: 'https://server8.datasol.net.au', 24 postLogoutRedirectUri: "https://server8.datasol.net.au/logout", 25 navigateToLoginRequestUrl: false 26 }, 27 cache: { 28 cacheLocation: "localStorage", // This configures where your cache will be stored 29 storeAuthStateInCookie: true, // Set this to "true" if you are having issues on IE11 or Edge 30 } 31}; 32 33const msalConfigTest = { 34 auth: { 35 clientId: '12a10271-c3a5-4ed6-8a54-1a100ad4bb25', 36 redirectUri: 'https://server8.datasol.net.au', 37 validateAuthority: false 38 } 39}; 40*/ 41//const msalClient = new msal.PublicClientApplication(msalConfigTest); 42const msalClient = new msal.PublicClientApplication(msalConfig); 43 44async function MicrosoftSignIn() { 45 // Login 46 try { 47 // Use MSAL to login 48 const authResult = await msalClient.loginPopup(msalRequest); 49 console.log('id_token acquired at: ' + new Date().toString()); 50 // Save the account username, needed for token acquisition 51 sessionStorage.setItem('msalInfo', JSON.stringify(authResult)); 52 sessionStorage.setItem('msalAccount', authResult.account.username); 53 54 console.log(authResult); 55 56 // Get the user's profile from Graph 57 user = await getUser(); 58 // Save the profile in session 59 sessionStorage.setItem('graphUser', JSON.stringify(user)); 60 sessionStorage.setItem('displayName', user.displayName); 61console.log(sessionStorage); 62 63 $.event.trigger('microsoftsignin'); 64 65 } catch (error) { 66 console.log(error); 67alert('Error '+error); 68 } 69} 70 71 72function MicrosoftSignOut() { 73 account = null; 74 sessionStorage.removeItem('graphUser'); 75 msalClient.logout(); 76} 77 78async function getToken() { 79 let account = sessionStorage.getItem('msalAccount'); 80 if (!account){ 81 82console.log('NO Microsoft Account'); 83console.log(sessionStorage); 84 85 document.location.href='login'+window.location.search; 86 } 87 88console.log('msalAccount', account); 89console.log('msalClient', msalClient); 90 91 92 try { 93 // First, attempt to get the token silently 94 const silentRequest = { 95 scopes: msalRequest.scopes, 96 account: msalClient.getAccountByUsername(account) 97 }; 98 99 const silentResult = await msalClient.acquireTokenSilent(silentRequest); 100 101console.log('silentResult', silentResult); 102 return silentResult.accessToken; 103 } catch (silentError) { 104console.log(silentError); 105 // If silent requests fails with InteractionRequiredAuthError, 106 // attempt to get the token interactively 107 if (silentError instanceof msal.InteractionRequiredAuthError) { 108 109console.log('se'); 110 const interactiveResult = await msalClient.acquireTokenPopup(msalRequest); 111 return interactiveResult.accessToken; 112 } else { 113console.log('throw'); 114 115 document.location.href='login'+window.location.search; 116 117// throw silentError; 118 } 119 120 } 121} 122 123const authProvider = { 124 getAccessToken: async () => { 125 // Call getToken in auth.js 126 return await getToken(); 127 } 128}; 129 130// Initialize the Graph client 131const graphClient = MicrosoftGraph.Client.initWithMiddleware({authProvider}); 132//givenName 133// surname 134async function getUser() { 135 return await graphClient 136 .api('/me') 137 // Only get the fields used by the app 138 .select('id,displayName,mail,userPrincipalName,givenName,surname') 139 .get(); 140} 141// '/drives/{drive-id}/items/{item-id}/children' 142async function getFiles() { 143 return await graphClient 144 .api('/me/drive/root/children') 145 // Only get the fields used by the app 146 .get(); 147} 148 149async function getFolder(folder) { 150 return await graphClient 151 .api('/me/drive/items/'+folder+'/children') 152 .get(); 153} 154 155 156async function SaveOneDriveFile(folderPath, fileName, file) { 157console.log('SaveOneDriveFile',folderPath, fileName); 158// return await graphClient 159let ret = await graphClient 160 .api('/me/drive/root:/'+folderPath+'/'+fileName+':/content') 161 .put(file); 162 163console.log('ret', ret); 164return ret; 165} 166 167 168async function GetOnedriveFile(file_id, returnFunction) { 169 return await graphClient 170 .api('/me/drive/items/'+file_id) 171 .get() 172 .then((response) => { 173 let downloadURL = response["@microsoft.graph.downloadUrl"]; 174 $.get(downloadURL, function(data, status){ 175 returnFunction(data, file_id); 176 }); 177 });
178} 179 180 181async function GetOnedriveFileFromShareId(file_shareid, returnFunction) { 182 return await graphClient 183 .api('/shares/'+file_shareid+'/driveItem') 184 .get() 185 .then((response) => { 186//console.log(response); 187 let downloadURL = response["@microsoft.graph.downloadUrl"]; 188 $.get(downloadURL, function(data, status){ 189 returnFunction(data, file_shareid); 190 }); 191 }); 192} 193 194 195async function SaveAppFile(fileName, file) { 196 return await graphClient 197 .api('/me/drive/special/approot:/'+fileName+':/content') 198 .put(file); 199} 200 201 202async function FindOneDriveFile(folderPath) { 203 return await graphClient 204 .api("/me/drive/items/root:/"+folderPath+":/children?orderby=lastModifiedDateTime%20desc") 205 .get(); 206} 207 208function SetPreferences(data) 209{ 210console.log('SetPreferences', data); 211 212 if (data.defaultFolder != undefined) 213 { 214 folderPath = data.defaultFolder; 215 } 216} 217 218 219 220async function GetAppPrefFile() 221{ 222 let files = await GetAppFileChildren(); 223//console.log(files); 224 if (files.value && files.value.length > 0) 225 { 226 files.value.forEach(function (file) { 227 if (file.name == 'mmpro_preferences.json') 228 { 229 GetOnedriveFile(file.id, SetPreferences) 230 } 231 }); 232 } 233 else 234 { 235console.log('No App Files Found'); 236 } 237} 238 239 240async function GetAppFileChildren() { 241 return await graphClient 242 .api('/me/drive/special/approot/children') 243 .get(); 244} 245 246async function GetSequencerAppPrefFile() 247{ 248 let files = await FindOneDriveFile('Apps/Sequencer Mastering Music Online'); 249//console.log(files); 250 if (files.value && files.value.length > 0) 251 { 252 files.value.forEach(function (file) { 253 if (file.name == 'mmpro_preferences.json') 254 { 255 GetOnedriveFile(file.id, SetPreferences) 256 } 257 }); 258 } 259 else 260 { 261console.log('No App Files Found'); 262 } 263} 264 265 266async function SaveOnedriveAppFile(fileName, file) 267{ 268 return await graphClient 269 .api('/me/drive/special/approot:/'+fileName+':/content') 270 .put(file); 271} 272 273async function CreatLinkToFile(file_id) 274{ 275 let permission = { 276 "type": "view", 277 "scope": "anonymous" 278 }; 279 280 return await graphClient 281 .api('/me/drive/items/'+file_id+'/createLink') 282 .post(permission); 283} 284 285 286 287 288
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.