Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. /**
  2. * Creates the correct directory structure and a config file in the user's drive;
  3. */
  4. function setupDrive(email, name) {
  5. // TODO create CSR folder and config file inside
  6. createFolder('CSR');
  7. uploadConfig(email, name);
  8. checkAuth();
  9. }
  10.  
  11. /**
  12. * Creates a folder with the given name in the drive;
  13. */
  14. function createFolder(dirName) {
  15. var metadata = {
  16. 'name' : dirName,
  17. 'mimeType' : 'application/vnd.google-apps.folder'
  18. };
  19.  
  20. var request = gapi.client.request({
  21. 'path': '/drive/v3/files',
  22. 'method': 'POST',
  23. 'body': JSON.stringify(metadata)});
  24. request.execute();
  25. }
  26.  
  27. /**
  28. * Uploads a config file to the CSR folder with the given email and name;
  29. */
  30. function uploadConfig(email, name) {
  31. var request = gapi.client.request({
  32. 'path': '/drive/v3/files',
  33. 'method': 'GET',
  34. 'q': 'name="CSR", trashed="false", mimeType="application/vnd.google-apps.folder"',
  35. 'fields': "nextPageToken, files(id, name)"
  36. });
  37.  
  38. request.execute(function (results) {
  39. var files = results.files;
  40. var csrID = '';
  41. if (files && files.length > 0) {
  42. csrID = files[0].id;
  43. }
  44. uploadFile('config', email + 'n' + name, 'plain', csrID);
  45. });
  46. }
  47.  
  48. /**
  49. * Uploads either a plain text file or a CSV file to the user's Google Drive in the CSR folder;
  50. */
  51. function uploadFile(fileName, fileContent, mimeType, parentID) {
  52. console.log(parentID); //check that a parentID is being passed in
  53. var auth_token = gapi.auth.getToken().access_token;
  54.  
  55. var metaType = '';
  56. var bodyType = '';
  57. if (mimeType == 'csv') {
  58. metaType = 'application/vnd.google-apps.spreadsheet';
  59. bodyType = 'text/csvrnrn';
  60. } else if (mimeType == 'plain') {
  61. metaType = 'text/plainrnrn';
  62. bodyType = 'text/plainrnrn';
  63. }
  64.  
  65. const boundary = '-------314159265358979323846';
  66. const delimiter = "rn--" + boundary + "rn";
  67. const close_delim = "rn--" + boundary + "--";
  68.  
  69. var metadata = {
  70. 'name': fileName,
  71. 'mimeType': metaType,
  72. 'parents':[{'id': parentID}]
  73. };
  74.  
  75. var multipartRequestBody =
  76. delimiter + 'Content-Type: application/jsonrnrn' +
  77. JSON.stringify(metadata) +
  78. delimiter + 'Content-Type: ' + bodyType +
  79. fileContent +
  80. close_delim;
  81.  
  82. var request = gapi.client.request({
  83. 'path': '/upload/drive/v3/files',
  84. 'method': 'POST',
  85. 'params': {'uploadType': 'multipart'},
  86. 'headers': { 'Content-Type': 'multipart/form-data; boundary="' + boundary + '"', 'Authorization': 'Bearer ' + auth_token, },
  87. 'body': multipartRequestBody
  88. })
  89.  
  90. request.execute(function (file) {
  91. console.log("Wrote to file " + file.name + " id: " + file.id);
  92. });
  93. }
  94.  
  95. function createFile(){
  96. const boundary = '-------314159265358979323846';
  97. const delimiter = "rn--" + boundary + "rn";
  98. const close_delim = "rn--" + boundary + "--";
  99.  
  100. var fileContent = 'It works :)';
  101.  
  102. var metadata = {
  103. 'name': 'myFile',
  104. 'mimeType': 'text/plainrnrn'
  105. };
  106.  
  107. var multipartRequestBody = delimiter + 'Content-Type: application/jsonrnrn' + JSON.stringify(metadata) + delimiter + 'Content-Type: ' + 'text/plainrnrn' + fileContent + close_delim;
  108.  
  109. gapi.client.request({
  110. 'path': '/upload/drive/v3/files',
  111. 'method': 'POST',
  112. 'params': {
  113. 'uploadType': 'multipart'
  114. },
  115. 'headers': {
  116. 'Content-Type': 'multipart/related; boundary="' + boundary + '"'
  117. },
  118. 'body': multipartRequestBody
  119. }).then(function(response){
  120. console.log(response);
  121. });
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement