1- 'use strict' ;
2-
31var 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
86FilePreviews = 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
1530FilePreviews . 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
121121FilePreviews . 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 ;
0 commit comments