Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Creates the correct directory structure and a config file in the user's drive;
- */
- function setupDrive(email, name) {
- // TODO create CSR folder and config file inside
- createFolder('CSR');
- uploadConfig(email, name);
- checkAuth();
- }
- /**
- * Creates a folder with the given name in the drive;
- */
- function createFolder(dirName) {
- var metadata = {
- 'name' : dirName,
- 'mimeType' : 'application/vnd.google-apps.folder'
- };
- var request = gapi.client.request({
- 'path': '/drive/v3/files',
- 'method': 'POST',
- 'body': JSON.stringify(metadata)});
- request.execute();
- }
- /**
- * Uploads a config file to the CSR folder with the given email and name;
- */
- function uploadConfig(email, name) {
- var request = gapi.client.request({
- 'path': '/drive/v3/files',
- 'method': 'GET',
- 'q': 'name="CSR", trashed="false", mimeType="application/vnd.google-apps.folder"',
- 'fields': "nextPageToken, files(id, name)"
- });
- request.execute(function (results) {
- var files = results.files;
- var csrID = '';
- if (files && files.length > 0) {
- csrID = files[0].id;
- }
- uploadFile('config', email + 'n' + name, 'plain', csrID);
- });
- }
- /**
- * Uploads either a plain text file or a CSV file to the user's Google Drive in the CSR folder;
- */
- function uploadFile(fileName, fileContent, mimeType, parentID) {
- console.log(parentID); //check that a parentID is being passed in
- var auth_token = gapi.auth.getToken().access_token;
- var metaType = '';
- var bodyType = '';
- if (mimeType == 'csv') {
- metaType = 'application/vnd.google-apps.spreadsheet';
- bodyType = 'text/csvrnrn';
- } else if (mimeType == 'plain') {
- metaType = 'text/plainrnrn';
- bodyType = 'text/plainrnrn';
- }
- const boundary = '-------314159265358979323846';
- const delimiter = "rn--" + boundary + "rn";
- const close_delim = "rn--" + boundary + "--";
- var metadata = {
- 'name': fileName,
- 'mimeType': metaType,
- 'parents':[{'id': parentID}]
- };
- var multipartRequestBody =
- delimiter + 'Content-Type: application/jsonrnrn' +
- JSON.stringify(metadata) +
- delimiter + 'Content-Type: ' + bodyType +
- fileContent +
- close_delim;
- var request = gapi.client.request({
- 'path': '/upload/drive/v3/files',
- 'method': 'POST',
- 'params': {'uploadType': 'multipart'},
- 'headers': { 'Content-Type': 'multipart/form-data; boundary="' + boundary + '"', 'Authorization': 'Bearer ' + auth_token, },
- 'body': multipartRequestBody
- })
- request.execute(function (file) {
- console.log("Wrote to file " + file.name + " id: " + file.id);
- });
- }
- function createFile(){
- const boundary = '-------314159265358979323846';
- const delimiter = "rn--" + boundary + "rn";
- const close_delim = "rn--" + boundary + "--";
- var fileContent = 'It works :)';
- var metadata = {
- 'name': 'myFile',
- 'mimeType': 'text/plainrnrn'
- };
- var multipartRequestBody = delimiter + 'Content-Type: application/jsonrnrn' + JSON.stringify(metadata) + delimiter + 'Content-Type: ' + 'text/plainrnrn' + fileContent + close_delim;
- gapi.client.request({
- 'path': '/upload/drive/v3/files',
- 'method': 'POST',
- 'params': {
- 'uploadType': 'multipart'
- },
- 'headers': {
- 'Content-Type': 'multipart/related; boundary="' + boundary + '"'
- },
- 'body': multipartRequestBody
- }).then(function(response){
- console.log(response);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement