All files / lib/backend-operations cloud-api-lambda-uploader.js

11.86% Statements 7/59
0% Branches 0/27
0% Functions 0/10
11.86% Lines 7/59
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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                          6x 6x 6x 6x   6x 6x                                                                                                                                                                                                                         6x      
/* 
 * Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
 * the License. A copy of the License is located at
 *
 *     http://aws.amazon.com/apache2.0/
 *
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
 * and limitations under the License.
*/
"use strict";
const fs = require('fs-extra')
const path = require('path')
const chalk = require('chalk')
const mime = require('mime-types')
 
const awsClient = require('../aws-operations/aws-client.js')
const pathManager = require('../utils/awsmobilejs-path-manager.js')
 
let _featureBuildDirPath
let _projectInfo
let _backendProjectDetails
let _callback
 
function uploadLambdaZipFiles(projectInfo, awsDetails, backendProjectDetails, featureName, callback){
    _projectInfo = projectInfo
    _backendProjectDetails = backendProjectDetails
    _callback = callback
 
    _featureBuildDirPath = pathManager.getBackendBuildFeatureDirPath(_projectInfo.ProjectPath, featureName)
    if(fs.existsSync(_featureBuildDirPath)){
 
        let deploymentBucket = getDeploymentBucket()
        let s3 = awsClient.S3(awsDetails, getDeploymentBucketRegion(deploymentBucket))
 
        let fileList = fs.readdirSync(_featureBuildDirPath)
        let uploadCount = 0
        let totalFileCount = fileList.length
        
        if(totalFileCount > 0){
            fileList.forEach(function(fileName) {
                let filePath = path.join(_featureBuildDirPath, fileName)
                let current = fs.lstatSync(filePath)
                if(current.isFile()) {
                    let key = 'uploads/' + fileName
                    
                    //first check if the file is already uploaded
                    let headRequestParams = {Bucket: getDeploymentBucketName(deploymentBucket), Key: key}
                    s3.headObject(headRequestParams, function(err, data){
                        if(err){ 
                            let fileStream = fs.createReadStream(filePath)
                            fileStream.on('error', function(err) {
                                console.log(fileName + ' read stream error', err)
                            })                    
                            
                            let contentType = mime.lookup(fileName)
                            let uploadParams = {Bucket: getDeploymentBucketName(deploymentBucket), Key: key, Body: fileStream, ContentType: contentType? contentType: "text/plain"}
                            
                            console.log('   uploading ' + fileName)
                            s3.upload (uploadParams, function (err, data) {
                                if (err) {
                                    console.log(chalk.red(fileName + ": upload error: ", err))
                                } if (data) {
                                    console.log("   upload Successful ", fileName)
                                }
                                uploadCount++ 
                                if(uploadCount == totalFileCount){
                                    onPreBackendUpdateComplete()
                                }
                            })
                        }else{
                            uploadCount++ 
                            if(uploadCount == totalFileCount){
                                onPreBackendUpdateComplete()
                            }
                        }
                    })
                }
            }, this)
        }else{
            onPreBackendUpdateComplete()
        }
    }else{
        onPreBackendUpdateComplete()
    }
}
 
function onPreBackendUpdateComplete(){
    if(_callback){
        _callback()
    }
}
 
 
function getDeploymentBucket(){
    return _backendProjectDetails.resources.find(isDeploymentBucket)
}
 
function getDeploymentBucketName(deploymentBucket){
    let deploymentBucketName 
    if(deploymentBucket){
        deploymentBucketName = deploymentBucket.name
    }
    return deploymentBucketName
}
 
function getDeploymentBucketRegion(deploymentBucket){
    let deploymentBucketRegion
    if(deploymentBucket){
        deploymentBucketRegion = deploymentBucket.attributes.region
    }
    return deploymentBucketRegion
}
 
function isDeploymentBucket(resource){
    let result = false
    try{
        result = (resource.type == "AWS::S3::Bucket" && 
                    resource.feature == 'common' &&
                    resource.name.includes('-deployments-'))
    }catch(e){
        result = false
    }
    return result
}
 
module.exports = {
    uploadLambdaZipFiles
}