-
Notifications
You must be signed in to change notification settings - Fork 0
/
do.js
41 lines (34 loc) · 1.32 KB
/
do.js
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
/**
* Created by HENG on 2015/5/22.
*/
var https = require("https");
var fs = require("fs");
var path = require("path");
var key = "CR25kPWD2NV8sViEjfgY7JEPuhWt_TmF";
module.exports = function (filePath, callback) {
var input = fs.createReadStream(filePath);
var fileExt = path.extname(filePath);
var fileName = path.basename(filePath, fileExt);
var output = fs.createWriteStream("test/" + fileName + ".mini" + fileExt);
/* Uncomment below if you have trouble validating our SSL certificate.
Download cacert.pem from: http://curl.haxx.se/ca/cacert.pem */
// var boundaries = /-----BEGIN CERTIFICATE-----[\s\S]+?-----END CERTIFICATE-----\n/g
// var certs = fs.readFileSync(__dirname + "/cacert.pem").toString()
// https.globalAgent.options.ca = certs.match(boundaries);
var options = require("url").parse("https://api.tinify.com/shrink");
options.auth = "api:" + key;
options.method = "POST";
var request = https.request(options, function(response) {
if (response.statusCode === 201) {
/* Compression was successful, retrieve output from Location header. */
https.get(response.headers.location, function(response) {
response.pipe(output);
callback();
});
} else {
/* Something went wrong! You can parse the JSON body for details. */
console.log("Compression failed");
}
});
input.pipe(request);
};