+/**
+ * These methods retrieves the status of package operations. It takes an optional
+ * transaction id (id) this if present will return only that status otherwise
+ * an array of status' will be response.
+ */
+PackageManager.getJobStatus = function(req, jobType) {
+ var api_server = req.query["api_server"];
+ var uri = utils.confdPort(api_server);
+ var id = req.params['id'];
+ var url = utils.projectContextUrl(req, uri + '/api/operational/' + jobType + (id ? '/job/' + id : ''));
+ return new Promise(function(resolve, reject) {
+ request({
+ url: url,
+ method: 'GET',
+ headers: _.extend({}, constants.HTTP_HEADERS.accept.data, {
+ 'Authorization': req.session && req.session.authorization
+ }),
+ forever: constants.FOREVER_ON,
+ rejectUnauthorized: false
+ }, function(error, response, body) {
+ if (utils.validateResponse('PackageManager.getJobStatus', error, response, body, resolve, reject)) {
+ var returnData;
+ var jsonResponse = JSON.parse(response.body);
+ if (id) {
+ returnData = jsonResponse['rw-pkg-mgmt:job'];
+ if (!returnData){
+ returnData = {
+ status: 'failure',
+ errors: [{value: "Internal error"}]
+ }
+ }
+ } else {
+ var data = jsonResponse['rw-pkg-mgmt:' + jobType];
+ returnData = (data && data.job) || [];
+ }
+ resolve({
+ statusCode: response.statusCode,
+ data: returnData
+ });
+ }
+ })
+ })
+}
+PackageManager.getCopyJobStatus = function(req) {
+ return PackageManager.getJobStatus(req, 'copy-jobs');
+}
+PackageManager.getImportJobStatus = function(req) {
+ return PackageManager.getJobStatus(req, 'create-jobs');
+}
+PackageManager.getUpdateJobStatus = function(req) {
+ return PackageManager.getJobStatus(req, 'update-jobs');
+}
+
+function makeAssetTypeParamName (type) {
+ return type.toLowerCase() + '-file-type';
+}
+FileManager.addFile = function(req) {
+ console.log(' Uploading file', req.file.originalname, 'as', req.file.filename);
+ var api_server = req.query['api_server'];
+ var download_host = req.query['dev_download_server'];
+ var package_id = req.query['package_id'];
+ var package_type = req.query['package_type'].toUpperCase();
+ var package_path = req.query['package_path'];
+ if (!download_host) {
+ download_host = req.protocol + '://' + req.get('host');//api_server + ':' + utils.getPortForProtocol(req.protocol);
+ }
+ var filename = req.file.filename;
+ var assetName = req.file.originalname;
+ var input = {
+ 'external-url': download_host + '/composer/' + constants.BASE_PACKAGE_UPLOAD_DESTINATION + '/' + filename,
+ 'package-type': package_type,
+ 'package-id': package_id,
+ 'package-path': package_path ? package_path + '/' + assetName : assetName
+ };
+
+ var uri = utils.projectContextUrl(req, utils.confdPort(api_server) + '/api/operations/package-file-add');
+
+ input = utils.addProjectContextToRPCPayload(req, uri, input);
+
+ var assetType = req.query['asset_type'].toUpperCase();
+ input[makeAssetTypeParamName(package_type)] = assetType;
+ return new Promise(function(resolve, reject) {
+ Promise.all([
+ rp({
+ uri: uri,
+ method: 'POST',
+ headers: _.extend({}, constants.HTTP_HEADERS.accept.collection, {
+ 'Authorization': req.session && req.session.authorization
+ }),
+ forever: constants.FOREVER_ON,
+ rejectUnauthorized: false,
+ resolveWithFullResponse: true,
+ json: true,
+ body: {
+ input: input
+ }
+ })
+ ]).then(function(result) {
+ var data = {};
+ data['transaction_id'] = result[0].body['output']['task-id'];
+ // Add a status checker on the transaction and then to delete the file later
+ PackageFileHandler.checkCreatePackageStatusAndHandleFile(req, data['transaction_id'], 'download');
+ resolve({
+ statusCode: constants.HTTP_RESPONSE_CODES.SUCCESS.OK,
+ data: data
+ });
+ }).catch(function(error) {
+ var res = {};
+ console.log('Problem with Composer.upload', error);
+ res.statusCode = error.statusCode || 500;
+ res.errorMessage = {
+ error: 'Failed to upload package ' + req.file.originalname + '. Error: ' + error
+ };
+ reject(res);
+ });
+ });
+}
+