Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var Client = require('ftp');
- var fs = require('fs');
- var mailer = require('mail');
- var stream = require('stream');
- var qv = {
- host: process.env.QV_HOST,
- user: process.env.QV_USER,
- password: process.env.QV_PASS
- };
- var brick = {
- host: process.env.BRICK_HOST,
- user: process.env.BRICK_USER,
- password: process.env.BRICK_PASS,
- };
- var transporter = mailer.Mail({
- host: process.env.SMTP_HOST,
- port: process.env.SMTP_PORT,
- secure: true,
- auth: {
- user: process.env.SMTP_USER,
- pass: process.env.SMTP_PASS
- }
- })
- var data = '/data';
- var sale = '/sale';
- var property = '/rates-valuations'
- var downloadList = []
- var emailText = ''
- var taCodeMap = {
- 11: "/11_thames-coromandel",
- 12: "/12_hauraki",
- 13: "/13_waikato",
- 15: "/15_matamata-piako",
- 16: "/16_hamilton-city",
- 17: "/17_waipa",
- 18: "/18_otorohanga",
- 19: "/19_south-waikato",
- 20: "/20_waitomo",
- 21: "/21_taupo",
- 22: "/22_western-bay-of-plenty",
- 23: "/23_tauranga-city",
- 24: "/24_rotorua",
- 25: "/25_whakatane",
- 26: "/26_kawerau",
- 27: "/27_opotiki",
- 28: "/28_gisborne",
- 29: "/29_wairoa",
- 30: "/30_hastings",
- 31: "/31_napier-city",
- 32: "/32_central-hawkes-bay",
- 33: "/33_new-plymouth",
- 34: "/34_stratford",
- 35: "/35_south-taranaki",
- 36: "/36_ruapehu",
- 37: "/37_wanganui",
- 38: "/38_rangitikei",
- 39: "/39_manawatu",
- 40: "/40_palmerston-north-city",
- 41: "/41_tararua",
- 42: "/42_horowhenua",
- 43: "/43_kapiti-coast",
- 44: "/44_porirua-city",
- 45: "/45_upper-hutt-city",
- 46: "/46_hutt-city",
- 47: "/47_wellington-city",
- 48: "/48_masterton",
- 49: "/49_carterton",
- 50: "/50_south-wairarapa",
- 51: "/51_tasman",
- 52: "/52_nelson-city",
- 53: "/53_marlborough",
- 56: "/56_grey",
- 57: "/57_westland",
- 58: "/58_hurunui",
- 59: "/59_waimakariri",
- 60: "/60_christchurch-city",
- 62: "/62_selwyn",
- 63: "/63_ashburton",
- 64: "/64_timaru",
- 65: "/65_mackenzie",
- 66: "/66_waimate",
- 67: "/67_chatham-islands",
- 68: "/68_waitaki",
- 69: "/69_central-otago",
- 70: "/70_queenstown-lakes",
- 71: "/71_dunedin-city",
- 72: "/72_clutha",
- 74: "/74_gore",
- 75: "/75_invercargill-city",
- 76: "/76_auckland",
- }
- //TaCodeMap.GetTA handles JS' expectations for numbers with a leading 0
- taCodeMap.GetTA = function(taCode) {
- if (taCode == '01') {
- return "/1_far-north"
- } else if (taCode == '02') {
- return "/2_whangarei"
- } else if (taCode == '03') {
- return "/3_kaipara"
- } else {
- return taCodeMap[taCode]
- }
- }
- var salePath = function(taCode) {
- return data + taCodeMap.GetTA(taCode) + sale
- }
- var propertyPath = function(taCode) {
- return data + taCodeMap.GetTA(taCode) + property
- }
- var createBrickPath = function(path) {
- if (path.includes('Sales')) {
- return salePath(path.slice(5, 7)) + "/" + path
- } else if (path.includes('Property')) {
- return propertyPath(path.slice(8, 10)) + "/" + path
- }
- }
- var list = new Client();
- list.on('ready', function() {
- list.list(function(err, fileList) {
- if (err)
- throw err;
- fileList.map(function(entry) {
- downloadList.push(entry.name)
- })
- list.end()
- })
- });
- var download = new Client()
- list.on('end', function() {
- emailText += downloadList.length + ' files found in QV.<br /> <br />'
- download.on('ready', function() {
- downloadList.map(function(file) {
- download.get(file, false, function(err, stream) {
- if (err)
- throw err
- stream.once('close', function() {
- download.end();
- });
- stream.pipe(fs.createWriteStream(file))
- })
- });
- })
- download.connect(qv)
- })
- var upload = new Client();
- download.on('end', function() {
- if (downloadList.length > 0) {
- upload.on('ready', function() {
- downloadList.map(function(filename) {
- var brickPath = createBrickPath(filename)
- //Check if the file exists
- upload.size(brickPath, function(err, size) {
- if (err) {
- // file doesn't exist, upload
- if (err.message.includes('No such file or directory')) {
- upload.put(filename, brickPath, function(err) {
- if (err)
- throw err;
- upload.end();
- emailText += filename + ' sucessfully uploaded to ' + brickPath + '<br />'
- });
- return
- }
- throw err
- }
- // file exists, upload it
- emailText += filename + ' skipped because file already exists <br />'
- })
- });
- });
- upload.connect(brick)
- } else {
- emailText = 'No new files found'
- }
- })
- upload.on('end', function() {
- transporter.message({
- from: 'ftpLambda@aws.co.nz',
- to: ['data@homes.co.nz'],
- subject: 'test sendmail',
- html: emailText,
- }, function(err, reply) {
- console.log(err && err.stack);
- console.dir(reply);
- });
- })
- list.connect(qv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement