· 8 years ago · Dec 15, 2016, 12:12 PM
1function readFile(file_link, file_name, params, callback, header)
2{
3 var xhr = new XMLHttpRequest();
4 xhr.onreadystatechange = function(){
5 if (this.readyState == 4 && this.status == 200){
6 var blob=this.response;
7 var reader = new FileReader();
8 reader.onload = function(){
9 // here you'll call what to do with the base64 string result
10 params['file'] = this.result;
11 params['file_name'] = file_name;
12 $data.uploadApplicationDocuments(params).then(function(response) {
13 console.log("response in xmhttp", response);
14 callback(response);
15 }, function (err) {
16 //debugger;
17 callback(err);
18 });
19 };
20 reader.readAsDataURL(blob);
21 }
22 }
23 xhr.open('GET', file_link);
24 if (header) {
25 xhr.setRequestHeader('Authorization','Bearer '+header);
26 }
27 xhr.responseType = 'blob';
28 xhr.send();
29}
30
31function errorHandling(response) {
32 if ('error' in response) {
33
34 } else {
35
36 }
37 return response;
38}
39
40
41function chooseFromGoogleDrive(params) {
42 // The Browser API key obtained from the Google API Console.
43 // Replace with your own Browser API key, or your own key.
44 var developerKey = 'AIzaSyAPeVXB4-CMMzyMTdlhpXYKOMfRBH5vllQ';
45
46 // The Client ID obtained from the Google API Console. Replace with your own Client ID.
47 var clientId = "204377991864.apps.googleusercontent.com"
48
49 // Replace with your own App ID. (Its the first number in your Client ID)
50 var appId = "204377991864";
51
52 // Scope to use to access user's Drive items.
53 var scope = ['https://www.googleapis.com/auth/drive'];
54
55 var pickerApiLoaded = false;
56 var oauthToken;
57
58 // Use the Google API Loader script to load the google.picker script.
59 gapi.load('auth', {'callback': onAuthApiLoad});
60 gapi.load('picker', {'callback': onPickerApiLoad});
61
62 function onAuthApiLoad() {
63 window.gapi.auth.authorize(
64 {
65 'client_id': clientId,
66 'scope': scope,
67 'immediate': false
68 },
69 handleAuthResult);
70 }
71
72 function onPickerApiLoad() {
73 pickerApiLoaded = true;
74 createPicker();
75 }
76
77 function handleAuthResult(authResult) {
78 if (authResult && !authResult.error) {
79 oauthToken = authResult.access_token;
80 createPicker();
81 }
82 }
83
84 // Create and render a Picker object for searching images.
85 function createPicker() {
86 if (pickerApiLoaded && oauthToken) {
87 var view = new google.picker.View(google.picker.ViewId.DOCS);
88 view.setMimeTypes("image/png,image/jpeg,image/jpg");
89 var picker = new google.picker.PickerBuilder()
90 .enableFeature(google.picker.Feature.NAV_HIDDEN)
91 .enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
92 .setAppId(appId)
93 .setOAuthToken(oauthToken)
94 .addView(view)
95 .addView(new google.picker.DocsUploadView())
96 .setDeveloperKey(developerKey)
97 .setCallback(pickerCallback)
98 .build();
99 picker.setVisible(true);
100 }
101 }
102
103 // A simple callback implementation.
104 function pickerCallback(data) {
105 if (data.action == google.picker.Action.PICKED) {
106 var fileId = data.docs[0].id;
107 var accessToken = gapi.auth.getToken().access_token; //gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse().access_token;// or this: gapi.auth.getToken().access_token;
108 var header = accessToken;
109 return readFile("https://www.googleapis.com/drive/v3/files/"+fileId+'?alt=media',data.docs[0].name, params, errorHandling,header);
110 }
111 }
112}
113
114function chooseFromOneDrive(params) {
115 var sendFileIdToServer = function(link) {
116 var data = {'link': link};
117 $.ajax( {
118 url: "/one-drive-file-link/",
119 method: "POST",
120 data: data,
121 success: function( data ) {
122 console.log(data);
123 }
124 });
125 }
126
127
128 function launchOneDrivePicker(){
129 var odOptions = {
130 clientId: "09314769-4a16-43b7-b2ab-6871209ee396",
131 action: "share | download | query",
132 multiSelect: true,
133 openInNewWindow: true,
134 advanced: {},
135 success: function(files) { /* success handler */
136 console.log(files);
137 console.log(files.value[0].webUrl);
138 return readFile(files.value[0].webUrl, files.value[0].name, params);
139 },
140 cancel: function() { /* cancel handler */ },
141 error: function(e) { console.log(e);/* error handler */ }
142 }
143 OneDrive.open(odOptions);
144 }
145 launchOneDrivePicker();
146}
147
148function chooseFromDropBox(params) {
149 console.log(params);
150 var options = {
151 // Required. Called when a user selects an item in the Chooser.
152 success: function(files) {
153 return readFile(files[0].link,files[0].name, params);
154 },
155 // Optional. Called when the user closes the dialog without selecting a file
156 // and does not include any parameters.
157 cancel: function() {
158 },
159 // Optional. "preview" (default) is a preview link to the document for sharing,
160 // "direct" is an expiring link to download the contents of the file. For more
161 // information about link types, see Link types below.
162 linkType: "direct", // or "direct"
163
164 // Optional. A value of false (default) limits selection to a single file, while
165 // true enables multiple file selection.
166 multiselect: false, // or true
167
168 // Optional. This is a list of file extensions. If specified, the user will
169 // only be able to select files with these extensions. You may also specify
170 // file types, such as "video" or "images" in the list. For more information,
171 // see File types below. By default, all extensions are allowed.
172 extensions: ['.png', '.jpg'],
173};
174 Dropbox.appKey = 'edxmp5ki6qjawke';
175 Dropbox.choose(options);
176
177}
178return {
179 chooseFromGoogleDrive: chooseFromGoogleDrive,
180 chooseFromOneDrive: chooseFromOneDrive,
181 chooseFromDropBox: chooseFromDropBox
182}