Skip to content

Commit 76af12f

Browse files
committed
Initial rewrite for v2
1 parent cd8b98b commit 76af12f

5 files changed

Lines changed: 140 additions & 112 deletions

File tree

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(The MIT License)
22

3-
Copyright (c) 2014 Blimp LLC - Giovanni Collazo
3+
Copyright (c) 2015 Blimp LLC - Giovanni Collazo
44

55
Permission is hereby granted, free of charge, to any person obtaining
66
a copy of this software and associated documentation files (the

README.md

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,78 @@
11
# FilePreviews.io
2-
This is a client library for the **Demo API** of [FilePreviews.io](http://filepreviews.io) service. A lot more to come very soon.
3-
4-
[Sign up to beta](http://eepurl.com/To0U1)
2+
Node.js client library and CLI tool for the [FilePreviews.io](http://filepreviews.io) service. Generate image previews and metadata from almost any kind of file.
53

64
## Installation
5+
76
```
87
npm install filepreviews
98
```
109

1110
### Example code
11+
1212
```js
13+
var FilePreviews = require('filepreviews');
1314

14-
var FilePreviews = require('filepreviews'),
15-
previews = new FilePreviews({debug: true});
15+
var previews = new FilePreviews({
16+
debug: true,
17+
apiKey: 'API_KEY_HERE',
18+
apiSecret: 'API_SECRET_HERE'
19+
});
1620

1721
previews.generate(url, function(err, result) {
18-
if (err) console.error(err);
22+
console.log(err);
23+
console.log(result.id);
24+
console.log(result.status);
1925

20-
console.log(result.previewURL);
21-
console.log(result.metadata);
26+
previews.retrieve(result.id, function(err, result) {
27+
console.log(result);
28+
});
2229
});
2330
```
2431

2532
#### Options
26-
You can optinally send an options object.
33+
You can optionally send an options object.
34+
2735
```js
28-
var previews = new FilePreviews({debug: true});
36+
var FilePreviews = require('filepreviews');
37+
38+
var previews = new FilePreviews({
39+
debug: true,
40+
apiKey: 'API_KEY_HERE',
41+
apiSecret: 'API_SECRET_HERE'
42+
});
43+
2944
var options = {
3045
size: {
31-
width: 100,
32-
height: 999,
46+
width: 250,
47+
height: 250,
3348
},
34-
// supported:
35-
// 'exif', 'ocr', 'psd', 'checksum', 'multimedia',
36-
// and 'all' which means everything
3749
metadata: ['exif', 'ocr', 'psd'],
38-
39-
//supported:
40-
// 'jpg', 'png'
4150
format: 'jpg'
4251
}
4352

4453
previews.generate(url, options, function(err, result) {
45-
console.log(result.previewURL);
46-
console.log(result.metadata);
54+
console.log(result.id);
55+
console.log(result.status);
4756
});
4857
```
4958

5059
## CLI
5160
We made a very simple CLI tool that comes bundled with this module and allows you to very easily test the FilePreviews.io API.
5261

53-
5462
#### Install globally
63+
5564
```
5665
npm install -g filepreviews
5766
```
5867

5968
#### CLI Usage
69+
6070
```
6171
filepreviews [options] [url]
6272
```
6373

6474
#### Example
75+
6576
```
6677
filepreviews https://www.filepicker.io/api/file/mbsbe85FTIW6DzYlkav2
6778
```

bin/filepreviews

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
#!/usr/bin/env node
2-
'use strict';
3-
42
var FilePreviews = require('../lib/'),
53
program = require('commander'),
64
url = require('valid-url'),
@@ -13,13 +11,14 @@ program
1311
.option('-D, --debug', 'Enable debug logging.')
1412
.option('-V, --version', 'Show version.')
1513
.option('-K, --apiKey [key]', 'Use FilePreviews.io application API Key')
14+
.option('-S, --apiSecret [key]', 'Use FilePreviews.io application API Secret Key');
15+
16+
program
17+
.command('generate <url>')
1618
.option('-m, --metadata [values]', 'Eg: exif,ocr,psd,all')
1719
.option('-f, --format [values]', 'Eg: png, jpg')
1820
.option('-h, --height [value]', 'Height value')
19-
.option('-w, --width [value]', 'Width value');
20-
21-
program
22-
.command('*')
21+
.option('-w, --width [value]', 'Width value')
2322
.action(function(fileURL) {
2423
var options = {};
2524

@@ -44,7 +43,8 @@ program
4443
if (url.isWebUri(fileURL)) {
4544
var preview = new FilePreviews({
4645
debug: program.debug,
47-
apiKey: program.apiKey
46+
apiKey: program.apiKey,
47+
apiSecret: program.apiSecret
4848
});
4949

5050
preview.generate(fileURL, options, function(err, result) {
@@ -55,4 +55,18 @@ program
5555
}
5656
});
5757

58+
program
59+
.command('retrieve <preview_id>')
60+
.action(function(previewId) {
61+
var preview = new FilePreviews({
62+
debug: program.debug,
63+
apiKey: program.apiKey,
64+
apiSecret: program.apiSecret
65+
});
66+
67+
preview.retrieve(previewId, function(err, result) {
68+
console.log(JSON.stringify(result, null, ' '));
69+
});
70+
});
71+
5872
program.parse(process.argv);

lib/index.js

Lines changed: 85 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1-
'use strict';
2-
31
var request = require('request');
42

5-
var API_URL = 'https://api.filepreviews.io/v1/',
6-
FilePreviews;
3+
var API_URL = 'https://api.filepreviews.io/v2';
4+
var FilePreviews;
75

86
FilePreviews = function(options) {
97
options = options || {};
108

119
this.debug = options.debug || false;
1210
this.apiKey = options.apiKey;
11+
this.apiSecret = options.apiSecret;
12+
13+
if (!this.apiKey) {
14+
throw new Error('Missing required apiKey.');
15+
}
16+
17+
if (!this.apiSecret) {
18+
throw new Error('Missing required apiSecret.');
19+
}
20+
};
21+
22+
FilePreviews.prototype.log = function(msg) {
23+
if (this.debug) {
24+
console.log(msg);
25+
}
26+
27+
return this;
1328
};
1429

1530
FilePreviews.prototype.generate = function(url, options, callback) {
@@ -25,112 +40,100 @@ FilePreviews.prototype.generate = function(url, options, callback) {
2540
}.bind(this));
2641
};
2742

28-
FilePreviews.prototype.submitJobToAPI = function(url, options, callback) {
43+
FilePreviews.prototype.generate = function(url, options, callback) {
2944
if (arguments.length === 2) {
3045
if (Object.prototype.toString.call(options) === '[object Function]') {
3146
callback = options;
3247
}
48+
} else if (arguments.length === 1) {
49+
options = {};
3350
}
3451

35-
var error = 'API request error: ';
36-
37-
this._log('API request to: ' + API_URL);
52+
this.request(API_URL + '/previews/', {
53+
method: 'POST',
54+
data: this.getAPIRequestData(url, options)
55+
},
56+
function(err, result) {
57+
if (callback) {
58+
callback(err, result);
59+
}
60+
});
61+
};
3862

39-
var requestOptions = {
40-
url: API_URL,
41-
json: this.getAPIRequestData(url, options)
42-
};
63+
FilePreviews.prototype.retrieve = function(previewId, callback) {
64+
this.request(API_URL + '/previews/' + previewId + '/', {
65+
method: 'GET'
66+
},
67+
function(err, result) {
68+
if (callback) {
69+
callback(err, result);
70+
}
71+
});
72+
};
4373

44-
if (this.apiKey) {
45-
requestOptions.headers = {
46-
'X-API-Key': this.apiKey
47-
};
48-
}
74+
FilePreviews.prototype.request = function(url, options, callback) {
75+
var data;
4976

50-
request.post(requestOptions, function(err, result) {
51-
if (err) console.error(err);
52-
53-
if (result.statusCode === 200 || result.statusCode === 403) {
54-
this._log('Got response ' + result.statusCode);
55-
var data = result.body;
56-
this._pollForMetadata(data.metadata_url, options, function(err, metadata) {
57-
if (err) {
58-
callback(err);
59-
} else {
60-
callback(null, {
61-
metadata: metadata,
62-
previewURL: data.preview_url
63-
});
64-
}
65-
}.bind(this));
66-
67-
} else if (result.statusCode === 429) {
68-
error = new Error(error + 'Throttling error, try later');
69-
console.error(error);
70-
callback(error);
77+
var onSuccess = function(error, response, body) {
78+
if (!error && response.statusCode >= 200 && response.statusCode <= 299) {
79+
this.log('API request success: ' + response.statusCode);
80+
this.log('API request response:', body);
7181

82+
callback(null, body);
7283
} else {
73-
error = new Error(error + result.statusCode);
74-
console.error(error);
75-
callback(error);
84+
onError(error, response, body);
7685
}
77-
}.bind(this));
78-
};
86+
}.bind(this);
7987

80-
FilePreviews.prototype._pollForMetadata = function(url, options, callback) {
81-
if (arguments.length === 2) {
82-
if (Object.prototype.toString.call(options) === '[object Function]') {
83-
callback = options;
84-
}
85-
}
88+
var onError = function(error, response, body) {
89+
this.log('API request error: ' + error);
90+
this.log('API request response:', body);
91+
this.log('API request error: ' + response.statusCode);
8692

87-
var tries = 1,
88-
pause = 1000;
89-
90-
var _getter = function() {
91-
this._log('Polling for metadata, tries: ' + tries);
92-
this._log('Polling url: ' + url);
93-
94-
request.get(url, function(err, result) {
95-
if (result.statusCode === 200) {
96-
this._log('Metadata found');
97-
var body = JSON.parse(result.body);
98-
if (body.error) {
99-
callback(body.error, body.error);
100-
} else {
101-
callback(null, body);
102-
}
103-
} else {
104-
pause = pause + (tries * 1000);
105-
tries++;
106-
107-
this._log('Metadata not found next try in: ' + pause / 1000 + 's');
108-
setTimeout(_getter, pause);
109-
}
110-
}.bind(this));
93+
callback(body);
11194
}.bind(this);
11295

113-
return _getter();
114-
};
96+
var requestOptions = {
97+
url: url,
98+
method: options.method,
99+
json: true,
100+
auth: {
101+
username: this.apiKey,
102+
password: this.apiSecret
103+
}
104+
};
115105

116-
FilePreviews.prototype._log = function(msg) {
117-
if (this.debug) console.log(msg);
118-
return this;
106+
if (options.data) {
107+
requestOptions.body = options.data;
108+
}
109+
110+
this.log('API request to: ' + url);
111+
112+
request(requestOptions, function (error, response, body) {
113+
if (error) {
114+
onError(error, response, body);
115+
} else {
116+
onSuccess(error, response, body);
117+
}
118+
});
119119
};
120120

121121
FilePreviews.prototype.getAPIRequestData = function(url, options) {
122-
if (arguments.length === 1) {
122+
var size;
123+
124+
if (arguments.length === 2) {
123125
if (Object.prototype.toString.call(options) === '[object Function]') {
124-
options = false;
126+
options = {};
125127
}
128+
} else if (arguments.length === 1) {
129+
options = {};
126130
}
127131

128132
if (options) {
129-
130133
options.url = url;
131134

132135
if (options.size) {
133-
var size = '';
136+
size = '';
134137

135138
if (options.size.width) {
136139
size = options.size.width;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "filepreviews",
3-
"version": "1.5.0",
3+
"version": "2.0.0",
44
"description": "Client library for FilePreviews.io - Generate image previews and metadata from almost any kind of file",
55
"main": "./lib/index.js",
66
"repository": {

0 commit comments

Comments
 (0)