-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub-upload.js
More file actions
144 lines (122 loc) · 3.05 KB
/
github-upload.js
File metadata and controls
144 lines (122 loc) · 3.05 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
var GitHubUpload = function(conf) {
this.conf = conf || {};
this._validate();
};
GitHubUpload.prototype = {
upload : function(files) {
var files = [].concat(files || []);
if (files.length === 0) {
console.log("No files to upload.");
}
for (var i = 0, l = files.length; i < l; i++) {
this.uploadFile(files[i]);
}
},
uploadFile : function(file) {
console.log(file);
},
_validate : function() {
if (!this.conf.token) {
console.log("GitHub token is missing!");
}
if (!this.conf.user) {
console.log("GitHub user is missing!");
}
if (!this.conf.repo) {
console.log("GitHub repo is missing!");
}
},
_create : function() {
// create on github
},
_save : function() {
// save to s3
}
};
exports.GitHubUpload = GitHubUpload;
var fs = require("fs");
var http = require("https");
var post = require("http-post");
var path = require("path");
var xml2js = require("xml2js");
var githubHost = "api.github.com";
var githubS3Host = "github.s3.amazonaws.com";
var conf = {};
exports.upload = function(_conf) {
checkArgs(_conf);
conf = _conf;
createDownload(uploadDownload);
};
function createDownload(callback) {
var filename = path.basename(conf.filepath);
var filesize = fs.statSync(conf.filepath).size;
var data = JSON.stringify({
"name" : filename,
"size" : filesize,
"description" : conf.filedesc,
"content_type" : "text/javascript"
});
var req = http.request({
host : githubHost,
path : "/repos/" + conf.user + "/" + conf.repo + "/downloads?access_token=" + conf.token,
method : "POST",
headers : { "Content-length" : data.length }
}, function(response) {
response.on("data", callback || function(responseData) {});
});
req.write(data + "\n");
req.end();
}
function uploadDownload(responseData) {
var data = JSON.parse(responseData.toString());
post({
host : githubS3Host
}, {
"key" : data.path,
"acl" : data.acl,
"success_action_status" : 201,
"Filename" : data.name,
"AWSAccessKeyId" : data.accesskeyid,
"Policy" : data.policy,
"Signature" : data.signature,
"Content-Type" : data.mime_type
}, [
{
"param" : "file",
"path" : conf.filepath
}
], function(response) {
response.on("data", uploadFinished);
});
};
function uploadFinished(responseData) {
var parser = new xml2js.Parser();
parser.parseString(responseData.toString(), function(error, data) {
conf.onUpload && conf.onUpload(data.PostResponse);
});
}
function checkArgs(conf) {
conf = conf || {};
if (!conf.token) {
console.log("GitHub token is missing!");
process.exit();
}
if (!conf.user) {
console.log("GitHub user is missing!");
process.exit();
}
if (!conf.repo) {
console.log("GitHub repo is missing!");
process.exit();
}
if (!conf.filepath) {
console.log("File to upload is missing!");
process.exit();
}
else {
if (!fs.existsSync(conf.filepath)) {
console.log("File to upload does not exist!");
process.exit();
}
}
}