1 | 'use strict';
|
2 |
|
3 | var path = require('path');
|
4 | var fs = require('fs');
|
5 |
|
6 | var formstream = require('formstream');
|
7 |
|
8 | var util = require('./util');
|
9 | var wrapper = util.wrapper;
|
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 | exports.uploadMedia = function (filepath, type, callback) {
|
39 | this.preRequest(this._uploadMedia, arguments);
|
40 | };
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | exports._uploadMedia = function (filepath, type, callback) {
|
46 | var that = this;
|
47 | fs.stat(filepath, function (err, stat) {
|
48 | if (err) {
|
49 | return callback(err);
|
50 | }
|
51 | var form = formstream();
|
52 | form.file('media', filepath, path.basename(filepath), stat.size);
|
53 | var url = that.endpoint + '/cgi-bin/media/upload?access_token=' + that.token.accessToken + '&type=' + type;
|
54 | var opts = {
|
55 | dataType: 'json',
|
56 | type: 'POST',
|
57 | timeout: 60000,
|
58 | headers: form.headers(),
|
59 | stream: form
|
60 | };
|
61 | that.request(url, opts, wrapper(callback));
|
62 | });
|
63 | };
|
64 |
|
65 | ['image', 'voice', 'video', 'thumb'].forEach(function (type) {
|
66 | var method = 'upload' + type[0].toUpperCase() + type.substring(1);
|
67 | exports[method] = function (filepath, callback) {
|
68 | this.uploadMedia(filepath, type, callback);
|
69 | };
|
70 | });
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 | exports.getMedia = function (mediaId, callback) {
|
89 | this.preRequest(this._getMedia, arguments);
|
90 | };
|
91 |
|
92 |
|
93 |
|
94 |
|
95 | exports._getMedia = function (mediaId, callback) {
|
96 | var url = this.endpoint + '/cgi-bin/media/get?access_token=' + this.token.accessToken + '&media_id=' + mediaId;
|
97 | var opts = {
|
98 | timeout: 60000
|
99 | };
|
100 | this.request(url, opts, wrapper(function (err, data, res) {
|
101 |
|
102 | if (err) {
|
103 | return callback(err);
|
104 | }
|
105 | var contentType = res.headers['content-type'];
|
106 | if (contentType === 'application/json' || contentType === 'text/plain') {
|
107 | var ret;
|
108 | try {
|
109 | ret = JSON.parse(data);
|
110 | if (ret.errcode) {
|
111 | err = new Error(ret.errmsg);
|
112 | err.name = 'WeChatAPIError';
|
113 | }
|
114 | } catch (ex) {
|
115 | callback(ex, data, res);
|
116 | return;
|
117 | }
|
118 | return callback(err, ret, res);
|
119 | }
|
120 |
|
121 | callback(null, data, res);
|
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 | exports.uploadImage = function (filepath, callback) {
|
147 | this.preRequest(this._uploadImage, arguments);
|
148 | };
|
149 |
|
150 |
|
151 |
|
152 |
|
153 | exports._uploadImage = function (filepath, callback) {
|
154 | var that = this;
|
155 | fs.stat(filepath, function (err, stat) {
|
156 | if (err) {
|
157 | return callback(err);
|
158 | }
|
159 | var form = formstream();
|
160 | form.file('media', filepath, path.basename(filepath), stat.size);
|
161 | var url = that.endpoint + '/cgi-bin/media/uploadimg?access_token=' + that.token.accessToken;
|
162 | var opts = {
|
163 | dataType: 'json',
|
164 | type: 'POST',
|
165 | timeout: 60000,
|
166 | headers: form.headers(),
|
167 | stream: form
|
168 | };
|
169 | that.request(url, opts, wrapper(callback));
|
170 | });
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 | exports.uploadImageStream = function (req, callback) {
|
194 | this.preRequest(this._uploadImageStream, arguments);
|
195 | };
|
196 |
|
197 |
|
198 |
|
199 |
|
200 | exports._uploadImageStream = function (req, callback) {
|
201 | var that = this;
|
202 | var url = that.endpoint + '/cgi-bin/media/uploadimg?access_token=' + that.token.accessToken;
|
203 | var opts = {
|
204 | dataType: 'json',
|
205 | type: 'POST',
|
206 | timeout: 60000,
|
207 | headers: req.headers,
|
208 | stream: req
|
209 | };
|
210 | delete opts.headers.host;
|
211 | that.request(url, opts, callback);
|
212 | };
|