1 var request
= require('request');
2 var Promise
= require('bluebird');
3 var rp
= require('request-promise');
4 var utils
= require('../../../framework/core/api_utils/utils.js');
5 var constants
= require('../../../framework/core/api_utils/constants.js');
6 var fs
= require('fs');
7 var _
= require('lodash');
9 var PackageFileHandler
= {};
11 function deleteFile(filename
) {
12 setTimeout(function() {
13 fs
.unlinkSync(constants
.BASE_PACKAGE_UPLOAD_DESTINATION
+ filename
);
14 }, constants
.PACKAGE_FILE_DELETE_DELAY_MILLISECONDS
);
17 function checkStatus(req
, transactionId
, isUpdate
) {
18 var upload_server
= req
.query
['upload_server'];
19 var headers
= _
.extend({},
21 'Authorization': req
.session
&& req
.session
.authorization
24 var type
= isUpdate
? 'update' : 'upload';
26 url
: upload_server
+ ':' + constants
.PACKAGE_MANAGER_SERVER_PORT
+ '/api/' + type
+ '/' + transactionId
+ '/state',
29 forever
: constants
.FOREVER_ON
,
30 rejectUnauthorized
: false
31 }, function(error
, response
, body
) {
33 console
.log('Error checking status for transaction', transactionId
, '. Will delete file', req
.file
.filename
, ' in ', constants
.PACKAGE_FILE_DELETE_DELAY_MILLISECONDS
,' milliseconds');
34 deleteFile(req
.file
.filename
);
36 var jsonStatus
= null;
37 if (typeof body
== 'string' || body
instanceof String
) {
38 jsonStatus
= JSON
.parse(body
);
43 if (jsonStatus
.status
&& (jsonStatus
.status
== 'success' || jsonStatus
.status
== 'failure')) {
44 console
.log('Transaction ', transactionId
, ' completed with status ', jsonStatus
.status
,'. Will delete file', req
.file
.filename
, ' in ', constants
.PACKAGE_FILE_DELETE_DELAY_MILLISECONDS
,' milliseconds');
45 deleteFile(req
.file
.filename
);
47 setTimeout(function() {
48 checkStatus(req
, transactionId
, isUpdate
);
49 }, constants
.PACKAGE_FILE_ONBOARD_TRANSACTION_STATUS_CHECK_DELAY_MILLISECONDS
);
55 PackageFileHandler
.checkCreatePackageStatusAndHandleFile = function(req
, transactionId
, isUpdate
) {
56 checkStatus(req
, transactionId
, isUpdate
);
60 module
.exports
= PackageFileHandler
;