Advertisement
Guest User

Untitled

a guest
Feb 1st, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Client = require('ftp');
  2. var fs = require('fs');
  3. var mailer = require('mail');
  4. var stream = require('stream');
  5.  
  6. var qv = {
  7.   host: process.env.QV_HOST,
  8.   user: process.env.QV_USER,
  9.   password: process.env.QV_PASS
  10. };
  11.  
  12. var brick = {
  13.   host: process.env.BRICK_HOST,
  14.   user: process.env.BRICK_USER,
  15.   password: process.env.BRICK_PASS,
  16. };
  17.  
  18. var transporter = mailer.Mail({
  19.   host: process.env.SMTP_HOST,
  20.   port: process.env.SMTP_PORT,
  21.   secure: true,
  22.   auth: {
  23.     user: process.env.SMTP_USER,
  24.     pass: process.env.SMTP_PASS
  25.   }
  26. })
  27.  
  28. var data = '/data';
  29. var sale = '/sale';
  30. var property = '/rates-valuations'
  31. var downloadList = []
  32.  
  33. var emailText = ''
  34.  
  35. var taCodeMap = {
  36.   11: "/11_thames-coromandel",
  37.   12: "/12_hauraki",
  38.   13: "/13_waikato",
  39.   15: "/15_matamata-piako",
  40.   16: "/16_hamilton-city",
  41.   17: "/17_waipa",
  42.   18: "/18_otorohanga",
  43.   19: "/19_south-waikato",
  44.   20: "/20_waitomo",
  45.   21: "/21_taupo",
  46.   22: "/22_western-bay-of-plenty",
  47.   23: "/23_tauranga-city",
  48.   24: "/24_rotorua",
  49.   25: "/25_whakatane",
  50.   26: "/26_kawerau",
  51.   27: "/27_opotiki",
  52.   28: "/28_gisborne",
  53.   29: "/29_wairoa",
  54.   30: "/30_hastings",
  55.   31: "/31_napier-city",
  56.   32: "/32_central-hawkes-bay",
  57.   33: "/33_new-plymouth",
  58.   34: "/34_stratford",
  59.   35: "/35_south-taranaki",
  60.   36: "/36_ruapehu",
  61.   37: "/37_wanganui",
  62.   38: "/38_rangitikei",
  63.   39: "/39_manawatu",
  64.   40: "/40_palmerston-north-city",
  65.   41: "/41_tararua",
  66.   42: "/42_horowhenua",
  67.   43: "/43_kapiti-coast",
  68.   44: "/44_porirua-city",
  69.   45: "/45_upper-hutt-city",
  70.   46: "/46_hutt-city",
  71.   47: "/47_wellington-city",
  72.   48: "/48_masterton",
  73.   49: "/49_carterton",
  74.   50: "/50_south-wairarapa",
  75.   51: "/51_tasman",
  76.   52: "/52_nelson-city",
  77.   53: "/53_marlborough",
  78.   56: "/56_grey",
  79.   57: "/57_westland",
  80.   58: "/58_hurunui",
  81.   59: "/59_waimakariri",
  82.   60: "/60_christchurch-city",
  83.   62: "/62_selwyn",
  84.   63: "/63_ashburton",
  85.   64: "/64_timaru",
  86.   65: "/65_mackenzie",
  87.   66: "/66_waimate",
  88.   67: "/67_chatham-islands",
  89.   68: "/68_waitaki",
  90.   69: "/69_central-otago",
  91.   70: "/70_queenstown-lakes",
  92.   71: "/71_dunedin-city",
  93.   72: "/72_clutha",
  94.   74: "/74_gore",
  95.   75: "/75_invercargill-city",
  96.   76: "/76_auckland",
  97. }
  98. //TaCodeMap.GetTA handles JS' expectations for numbers with a leading 0
  99. taCodeMap.GetTA = function(taCode) {
  100.   if (taCode == '01') {
  101.     return "/1_far-north"
  102.   } else if (taCode == '02') {
  103.     return "/2_whangarei"
  104.   } else if (taCode == '03') {
  105.     return "/3_kaipara"
  106.   } else {
  107.     return taCodeMap[taCode]
  108.   }
  109. }
  110.  
  111. var salePath = function(taCode) {
  112.   return data + taCodeMap.GetTA(taCode) + sale
  113. }
  114.  
  115. var propertyPath = function(taCode) {
  116.   return data + taCodeMap.GetTA(taCode) + property
  117. }
  118.  
  119. var createBrickPath = function(path) {
  120.   if (path.includes('Sales')) {
  121.     return salePath(path.slice(5, 7)) + "/" + path
  122.   } else if (path.includes('Property')) {
  123.     return propertyPath(path.slice(8, 10)) + "/" + path
  124.   }
  125. }
  126.  
  127. var list = new Client();
  128. list.on('ready', function() {
  129.  
  130.   list.list(function(err, fileList) {
  131.     if (err)
  132.       throw err;
  133.     fileList.map(function(entry) {
  134.       downloadList.push(entry.name)
  135.     })
  136.     list.end()
  137.  
  138.   })
  139. });
  140.  
  141. var download = new Client()
  142. list.on('end', function() {
  143.   emailText += downloadList.length + ' files found in QV.<br /> <br />'
  144.   download.on('ready', function() {
  145.  
  146.     downloadList.map(function(file) {
  147.       download.get(file, false, function(err, stream) {
  148.         if (err)
  149.           throw err
  150.         stream.once('close', function() {
  151.           download.end();
  152.         });
  153.  
  154.         stream.pipe(fs.createWriteStream(file))
  155.       })
  156.  
  157.     });
  158.   })
  159.   download.connect(qv)
  160. })
  161.  
  162. var upload = new Client();
  163. download.on('end', function() {
  164.   if (downloadList.length > 0) {
  165.     upload.on('ready', function() {
  166.  
  167.       downloadList.map(function(filename) {
  168.         var brickPath = createBrickPath(filename)
  169.         //Check if the file exists
  170.         upload.size(brickPath, function(err, size) {
  171.           if (err) {
  172.             // file doesn't exist, upload
  173.             if (err.message.includes('No such file or directory')) {
  174.  
  175.               upload.put(filename, brickPath, function(err) {
  176.                 if (err)
  177.                   throw err;
  178.                 upload.end();
  179.                 emailText += filename + ' sucessfully uploaded to ' + brickPath + '<br />'
  180.               });
  181.               return
  182.             }
  183.  
  184.             throw err
  185.           }
  186.           // file exists, upload it
  187.           emailText += filename + ' skipped because file already exists <br />'
  188.         })
  189.  
  190.       });
  191.     });
  192.     upload.connect(brick)
  193.   } else {
  194.     emailText = 'No new files found'
  195.   }
  196. })
  197.  
  198. upload.on('end', function() {
  199.   transporter.message({
  200.     from: 'ftpLambda@aws.co.nz',
  201.     to: ['data@homes.co.nz'],
  202.     subject: 'test sendmail',
  203.     html: emailText,
  204.   }, function(err, reply) {
  205.     console.log(err && err.stack);
  206.     console.dir(reply);
  207.   });
  208. })
  209. list.connect(qv)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement