-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuploadFileFromDesignStudio.gs
128 lines (111 loc) · 4.59 KB
/
uploadFileFromDesignStudio.gs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
var SPREADSHEET_FILE_ID = '1ijFYl9VO6isNDL1jgh_XS-mMABe_Od8NSi1MRnOlGUw';
var SHEET_TO_WRITE_DATA = "Sheet1" ;
var ADMIN_EMAIL = "[email protected]";
var PR_EMAIL = '[email protected]';
//var PR_SUPPORT_EMAIL = '[email protected]';
var TECH_EMAIL = '[email protected]';
var SRD_EMAIL = '[email protected]';
function doGet(e){
var params = JSON.stringify(e);
Logger.log("params " + params.toString());
var data = params["data"];
//var filename =...;
//var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob();
uploadFile(data);
}
function doPost(e) {
try{
Logger.log("e: " + JSON.stringify(e) );
var fileName = e.parameter.fileName || "hello";
Logger.log("Filename is: " + fileName);
var file = e.parameter.file;
//var section = e.parameter.section || "other";
var uploader = "unknown";
if(e.parameter.uploader){
uploader = e.parameter.uploader;
};
var userEmail = "unknown";
if(e.parameter.userEmail){
userEmail = e.parameter.userEmail;
}
Logger.log("Uploader: " + uploader);
Logger.log("Email: " + userEmail);
var newFileName = fileName + " - " + uploader;
uploadFile(file, newFileName, uploader, userEmail, fileName);
return ContentService.createTextOutput("OK").setMimeType(ContentService.MimeType.JAVASCRIPT);
}
catch(error){
email();
return ContentService.createTextOutput("NOT OK").setMimeType(ContentService.MimeType.JAVASCRIPT);
}
}
function uploadFile(data, fileName, uploader, userEmail, orginalFileName) {
try {
//https://drive.google.com/open?id=
//var mFolderId = "0B-Hig070yW4KczFiNExjcnVqVlE";
var mFolderId = "0B-Hig070yW4KeVZ4b2lwc0dDU1U";
var folder = DriveApp.getFolderById(mFolderId);
var contentType = data.substring(5,data.indexOf(';')),
bytes = Utilities.base64Decode(data.substr(data.indexOf('base64,')+7), Utilities.Charset.UTF_8),
blob = Utilities.newBlob(bytes, contentType, fileName),
file = folder.createFile(blob);
var fileLink = file.getUrl();
Logger.log("File link: " + fileLink);
Logger.log("Type of file " + contentType);
//var blobNew = Utilities.newBlob(data);
//var translatedData = Blob.setDataFromString(data)
//var file = folder.createFile(fileName, blobNew);
//file.setDescription("Uploaded by " + form.myName);
Logger.log("Successfully uploaded this file");
writeToFile(fileLink, uploader, userEmail, orginalFileName);
//return "File uploaded successfully " + file.getUrl();
} catch (error) {
Logger.log("Could not upload the file: " + error.toString());
email();
return ContentService.createTextOutput("NOT OK").setMimeType(ContentService.MimeType.JAVASCRIPT);
}
try{
emailSuccess(uploader, userEmail, fileLink, PR_EMAIL);
//emailSuccess(uploader, userEmail, fileLink, PR_SUPPORT_EMAIL);
emailSuccess(uploader, userEmail, fileLink, TECH_EMAIL);
emailSuccess(uploader, userEmail, fileLink, ADMIN_EMAIL);
if(userEmail.indexOf("srdivinetouch") > -1){
Logger.log("Going to email the SRD EMAIL");
emailSuccess(uploader, userEmail, fileLink, SRD_EMAIL);
};
return ContentService.createTextOutput("OK").setMimeType(ContentService.MimeType.JAVASCRIPT);
}
catch(error){
Logger.log("Could not send email: " + error.toString());
email();
return ContentService.createTextOutput("NOT OK").setMimeType(ContentService.MimeType.JAVASCRIPT);
}
}
function writeToFile(fileLink, uploader, userEmail, fileName){
var innerArray = [];
var timeStamp = new Date();
innerArray.push(timeStamp);
innerArray.push(uploader);
innerArray.push(userEmail);
innerArray.push(fileLink);
innerArray.push(fileName);
var ss = SpreadsheetApp.openById(SPREADSHEET_FILE_ID);
var sheet = ss.getSheetByName(SHEET_TO_WRITE_DATA);//Set name in Code.gs file
sheet.appendRow(innerArray);
}
function email(){
var subject = 'Google Drive Error Log';
var body = Logger.getLog();
MailApp.sendEmail(ADMIN_EMAIL, subject, body);
}
function emailSuccess(uploader, userEmail, fileLink, toEmail){
var subject = 'New Design Studio Asset created by ' + uploader;
var body = "Hello, <br /> You can view the new uploaded image here: " + fileLink;
body = body + "<br /> In case there are any issues with the image, please email the creator here: " + userEmail;
body = body + "<br /> Thank you!"
MailApp.sendEmail({
to: toEmail,
subject: subject,
htmlBody: body,
});
}