Skip to content

Commit dd27596

Browse files
committed
added README.md
1 parent d9fa102 commit dd27596

1 file changed

Lines changed: 193 additions & 0 deletions

File tree

README.md

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
Code Your Cloud
2+
=============
3+
<h3>Goal</h3>
4+
To allow people around the globe to collaborate on coding projects via Google Drive
5+
<h3>Features</h3>
6+
<ul>
7+
<li>Syntax highlighting</li>
8+
<li>Collaboration</li>
9+
<li>Built-in color picker</li>
10+
<li>HTML preview</li>
11+
<li>Autocomplete</li>
12+
<li>Javascript console</li>
13+
<li>A notepad to jot down ideas</li>
14+
<li>Built-in todo list</li>
15+
<li>Browser</li>
16+
<li>Keyboard shortcuts</li>
17+
<li>Vim modes</li>
18+
<li>Preferences</li>
19+
<li>Google Drive integration</li>
20+
</ul>
21+
<h3>Useful Code Snippets</h3>
22+
<h6>Getting the contents of a Google Drive file by id</h6>
23+
```javascript
24+
function getContentOfFile(theID){ //gets the content of the file
25+
gapi.client.request({'path': '/drive/v2/files/'+theID,'method': 'GET',callback: function ( theResponseJS, theResponseTXT ) {
26+
var myToken = gapi.auth.getToken();
27+
var myXHR = new XMLHttpRequest();
28+
myXHR.open('GET', theResponseJS.downloadUrl, true );
29+
myXHR.setRequestHeader('Authorization', 'Bearer ' + myToken.access_token );
30+
myXHR.onreadystatechange = function( theProgressEvent ) {
31+
if (myXHR.readyState == 4) {
32+
if ( myXHR.status == 200 ) {
33+
var content = myXHR.response;
34+
}
35+
}
36+
}
37+
myXHR.send();
38+
}
39+
});
40+
}
41+
```
42+
<h6>Get the title of a file</h6>
43+
```javascript
44+
function getTitle(fileId){
45+
var request = gapi.client.drive.files.get({
46+
'fileId': fileId
47+
});
48+
request.execute(function(resp) {
49+
title = resp.title;
50+
});
51+
}
52+
```
53+
<h6>Inserting a new file into a folder</h6>
54+
```javascript
55+
function insertNewFile(folderId) {
56+
var content = " ";
57+
var FolderId = "";
58+
var contentArray = new Array(content.length);
59+
for (var i = 0; i < contentArray.length; i++) {
60+
contentArray[i] = content.charCodeAt(i);
61+
}
62+
var byteArray = new Uint8Array(contentArray);
63+
var blob = new Blob([byteArray], {type: 'text/plain'}); //this is the only way I could get this to work
64+
insertFile(blob, fileInserted, folderId);
65+
}
66+
function fileInserted(d) {
67+
setPercent("100");
68+
var FI = FolderId;
69+
if(FI !== myRootFolderId){
70+
insertFileIntoFolder(FI, d.id);
71+
removeFileFromFolder(d.parents[0].id,d.id);
72+
}
73+
openFile(d.id);
74+
}
75+
function insertFileIntoFolder(folderId, fileId) {
76+
var body = {'id': folderId};
77+
var request = gapi.client.drive.parents.insert({
78+
'fileId': fileId,
79+
'resource': body
80+
});
81+
request.execute(function(resp) { });
82+
}
83+
function removeFileFromFolder(folderId, fileId) {
84+
var request = gapi.client.drive.parents.delete({
85+
'parentId': folderId,
86+
'fileId': fileId
87+
});
88+
request.execute(function(resp) { });
89+
}
90+
function insertFile(fileData, callback, folderId) {
91+
setPercent("90");
92+
const boundary = '-------314159265358979323846';
93+
const delimiter = "\r\n--" + boundary + "\r\n";
94+
const close_delim = "\r\n--" + boundary + "--";
95+
96+
var reader = new FileReader();
97+
reader.readAsBinaryString(fileData);
98+
reader.onload = function(e) {
99+
var contentType = fileData.type || 'application/octet-stream';
100+
var metadata = {
101+
'title': "untitled.txt",
102+
'mimeType': contentType
103+
};
104+
105+
var base64Data = btoa(reader.result);
106+
var multipartRequestBody =
107+
delimiter +
108+
'Content-Type: application/json\r\n\r\n' +
109+
JSON.stringify(metadata) +
110+
delimiter +
111+
'Content-Type: ' + contentType + '\r\n' +
112+
'Content-Transfer-Encoding: base64\r\n' +
113+
'\r\n' +
114+
base64Data +
115+
close_delim;
116+
117+
var request = gapi.client.request({
118+
'path': '/upload/drive/v2/files',
119+
'method': 'POST',
120+
'params': {'uploadType': 'multipart'},
121+
'headers': {
122+
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
123+
},
124+
'body': multipartRequestBody});
125+
if (!callback) {
126+
callback = function(file) {
127+
};
128+
}
129+
request.execute(callback);
130+
}
131+
}
132+
```
133+
<h6>Saving a file</h6>
134+
```javascript
135+
function saveFile(fileId, content){
136+
//console.log(content);
137+
if(typeof content !== "undefined"){ //if nothing is "null"
138+
var contentArray = new Array(content.length);
139+
for (var i = 0; i < contentArray.length; i++) {
140+
contentArray[i] = content.charCodeAt(i);
141+
}
142+
var byteArray = new Uint8Array(contentArray);
143+
var blob = new Blob([byteArray], {type: 'text/plain'}); //this is the only way I could get this to work
144+
var request = gapi.client.drive.files.get({'fileId': fileId});//gets the metadata, which is left alone
145+
request.execute(function(resp) {
146+
//console.log(content);
147+
updateFile(fileId,resp,blob,changesSaved);
148+
});
149+
}
150+
}
151+
function updateFile(fileId, fileMetadata, fileData, callback) { //is the callback necessary?
152+
if(ok){
153+
const boundary = '-------314159265358979323846';
154+
const delimiter = "\r\n--" + boundary + "\r\n";
155+
const close_delim = "\r\n--" + boundary + "--";
156+
157+
var reader = new FileReader();
158+
reader.readAsBinaryString(fileData);
159+
reader.onload = function(e) {
160+
var contentType = fileData.type || 'application/octet-stream';
161+
var base64Data = btoa(reader.result);
162+
//console.log(base64Data);
163+
var multipartRequestBody =
164+
delimiter +
165+
'Content-Type: application/json\r\n\r\n' +
166+
JSON.stringify(fileMetadata) +
167+
delimiter +
168+
'Content-Type: ' + contentType + '\r\n' +
169+
'Content-Transfer-Encoding: base64\r\n' +
170+
'\r\n' +
171+
base64Data +
172+
close_delim;
173+
174+
var request = gapi.client.request({
175+
'path': '/upload/drive/v2/files/' + fileId,
176+
'method': 'PUT',
177+
'params': {'uploadType': 'multipart', 'alt': 'json'},
178+
'headers': {
179+
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
180+
},
181+
'body': multipartRequestBody});
182+
if (!callback) {//this isn't necessary
183+
callback = function(file) {
184+
//console.log(file) //for some reason, this is important
185+
};
186+
}
187+
request.execute(callback);//not needed
188+
}
189+
}
190+
}
191+
```
192+
<h4>See the project so far <a href="https://codeyourcloud.com">here</h4>
193+
<h4>See the about page <a href="https://codeyourcloud.com/about">here</h4>

0 commit comments

Comments
 (0)