UNPKG

@samepage/backend

Version:

Backend utilities for the apis of SamePage integrations

48 lines 1.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const client_s3_1 = require("@aws-sdk/client-s3"); const fs_1 = tslib_1.__importDefault(require("fs")); const stream_1 = require("stream"); const getDefault = () => { const r = new stream_1.Readable(); r.push(null); return r; }; const downloadFile = ({ Key = "", }) => { if (process.env.NODE_ENV === "production") { const s3 = new client_s3_1.S3({ region: "us-east-1", // TODO // endpoint: process.env.AWS_ENDPOINT, }); const Bucket = "samepage.network"; return s3.listObjectsV2({ Bucket, Prefix: Key }).then((r) => { if (r.KeyCount === 0) return getDefault(); return s3 .getObject({ Bucket, Key, }) .then((r) => r.Body); }); } else { const path = `public/${Key}`; if (!fs_1.default.existsSync(path)) return Promise.resolve(getDefault()); return Promise.resolve(fs_1.default.createReadStream(path)); } }; const downloadFileBuffer = (args) => downloadFile(args).then((fil) => { const chunks = []; return new Promise((resolve, reject) => { fil.on("data", (chunk) => chunks.push(Buffer.from(chunk))); fil.on("error", (err) => reject(err)); fil.on("end", () => resolve(Buffer.concat(chunks))); }); }); const downloadFileContent = (args) => downloadFileBuffer(args).then((fil) => fil.toString("utf8")); exports.default = downloadFileContent; //# sourceMappingURL=downloadFileContent.js.map