forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplunk.js
More file actions
executable file
·187 lines (147 loc) · 4.2 KB
/
Copy pathplunk.js
File metadata and controls
executable file
·187 lines (147 loc) · 4.2 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
var mongoose = require('mongoose');
var assert = require('assert');
var request = require('co-request');
var config = require('config');
var Schema = mongoose.Schema;
var _ = require('lodash');
var log = require('log')();
var zip = require('node-zip');
var schema = new Schema({
description: {
type: String,
default: ""
},
webPath: {
type: String,
unique: true,
required: true
},
plunkId: {
type: String,
required: true
},
files: [{
filename: String,
content: String
}]
});
schema.methods.getUrl = function() {
return 'http://plnkr.co/edit/' + this.plunkId + '?p=preview';
};
schema.methods.getZip = function() {
var archive = new zip();
for (var i = 0; i < this.files.length; i++) {
var file = this.files[i];
archive.file(file.filename, file.content);
}
var buffer = archive.generate({type: 'nodebuffer'});
return buffer;
};
/**
* Merges files into the current plunk
* @param files
* @returns {boolean} new files list to post w/ nulls where files are deleted
*/
schema.methods.mergeAndSyncRemote = function*(files) {
var changes = {};
log.debug("mergeAndSyncRemote " + this.plunkId);
log.debug("OLD files", this.files);
log.debug("NEW files", files);
/* delete this.files which are absent in files */
for (var i = 0; i < this.files.length; i++) {
var file = this.files[i];
if (!files[file.filename]) {
this.files.splice(i--, 1);
changes[file.filename] = null; // for submitting to plnkr
}
}
for (var name in files) {
var existingFile = null;
for (var i = 0; i < this.files.length; i++) {
var item = this.files[i];
if (item.filename == name) {
existingFile = item;
break;
}
}
if (existingFile) {
if (existingFile.content == files[name].content) continue;
existingFile.content = files[name].content;
} else {
this.files.push(files[name]);
}
delete files[name].filename;
changes[name] = files[name];
}
log.debug("UPDATED files", this.files);
if (_.isEmpty(changes)) {
log.debug("no changes, skip updating");
return;
} else {
log.debug("plunk " + this.plunkId + " changes", changes);
}
if (this.plunkId) {
log.debug("update remotely", this.webPath, this.plunkId);
yield* Plunk.updateRemote(this.plunkId, changes);
} else {
log.debug("create plunk remotely", this.webPath);
this.plunkId = yield* Plunk.createRemote(this.description, this.files);
}
yield this.persist();
};
schema.statics.createRemote = function*(description, files) {
if (process.env.PLUNK_REMOTE_OFF) {
return Math.random().toString(36).slice(2);
}
var filesObj = {};
files.forEach(function(file) {
filesObj[file.filename] = {
filename: file.filename,
content: file.content
}; // no _id
});
var form = {
description: description,
tags: [],
files: filesObj,
private: true
};
var data = {
method: 'POST',
headers: {'Content-Type': 'application/json;charset=utf-8'},
json: true,
url: "http://api.plnkr.co/plunks/?sessid=" + config.plnkrAuthId,
body: form
};
var result = yield Plunk.request(data);
assert.equal(result.statusCode, 201);
return result.body.id;
};
schema.statics.request = function*(data) {
console.log(data);
var result = yield request(data);
if (result.statusCode == 404) {
throw new Error("result status code 404, probably (plnkrAuthId is too old OR this plunk doesn't belong to plunk@javascript.ru (javascript-plunk) user)");
}
if (result.statusCode == 400) {
throw new Error("invalid json, probably you don't need to stringify body (request will do it)");
}
return result;
};
schema.statics.updateRemote = function* (plunkId, changes) {
if (process.env.PLUNK_REMOTE_OFF) {
return;
}
var form = {
files: changes
};
var result = yield Plunk.request({
method: 'POST',
headers: {'Content-Type': 'application/json'},
json: true,
url: "http://api.plnkr.co/plunks/" + plunkId + "?sessid=" + config.plnkrAuthId,
body: form
});
assert.equal(result.statusCode, 200);
};
var Plunk = module.exports = mongoose.model('Plunk', schema);