temu-labels-crawler/send-shipping-labels-s3.js

50 lines
1.4 KiB
JavaScript

const axios = require("axios");
const fs = require("fs");
const path = require("path");
const dotenv = require("dotenv").config({ path: __dirname + "/.env" });
const { exit } = require("process");
const s3Utils = require("./s3-service");
(async function () {
/**
* load config data
*/
const config = JSON.parse(fs.readFileSync(__dirname + "/config.json"));
const environment = process.env["ENVIRONMENT"];
/**
* directory path
*/
let shippingLabelsPath = config[environment].temu_order_shipping_labels_path;
let processedPath = shippingLabelsPath + "/processed";
let syncedPath = shippingLabelsPath + "/synced";
if (!fs.existsSync(syncedPath)) {
fs.mkdirSync(syncedPath);
}
/**
* read all files in directory, send data to s3 then move to synced
*/
const pdfFiles = fs
.readdirSync(processedPath)
.filter((file) => path.extname(file).toLocaleLowerCase() === ".pdf");
if (pdfFiles.length === 0) {
console.log(`No Files Present at ${processedPath}`);
}
for (const file of pdfFiles) {
try {
const filePath = path.join(processedPath, file);
console.log(`Processing: ${filePath}`);
// send pdf files to s3
const result = await s3Utils.downloadAndUploadToS3(filePath, file );
if (result['$metadata']['httpStatusCode'] == 200) {
fs.renameSync(filePath, path.join(syncedPath, file));
}
} catch (e) {
console.log(e);
}
}
})();