PromucFlow_constructor/app/client/download-assets.js
Sangeeth Sivan 82280cfde9
feat: util to serve images locally or via remote url (#22080)
## Description

- On air-gapped instances we can't fetch appsmith assets from S3, that
will result in broken which is not desirable.
- So this adds a script and util function which searches the client and
server codebase for the assets url and downloads the image and puts it
in the `public` folder so that the browser can access those even in an
airgapped instance since the assets are being served locally.

> Way to serve assets locally.

Fixes #22004 
> if no issue exists, please create an issue and ask the maintainers
about this first

Media
https://vdqm24wed6.vmaker.com/record/IquS90WbWgS1I0bz - blocked certain
api routes from getting called on airgap
2023-04-10 12:32:31 +05:30

62 lines
2.0 KiB
JavaScript

const fs = require("fs");
const path = require("path");
const https = require("https");
const regex = /(?:\${ASSETS_CDN_URL}|https:\/\/assets\.appsmith\.com)[^`"]+/g;
const rootDir = [
path.resolve(__dirname, "src"),
path.join(path.resolve(__dirname, "../"), "server", "appsmith-server"),
];
function searchFiles(dir) {
fs.readdirSync(dir).forEach((file) => {
const filePath = path.join(dir, file);
const stat = fs.statSync(filePath);
if (stat.isDirectory()) {
searchFiles(filePath);
} else if (stat.isFile() && path.extname(filePath) !== ".class") {
// Skip .class files - server code
const contents = fs.readFileSync(filePath, "utf8");
const matches = contents.match(regex);
if (matches) {
console.log(`Found ${matches.length} matches in ${filePath}:`);
const replacedMatches = matches.map((match) => {
return match.replace(
/\${ASSETS_CDN_URL}/,
"https://assets.appsmith.com",
);
});
replacedMatches.forEach((match) => {
const filename = path.basename(match);
const destPath = path.join(__dirname, "public", filename);
if (fs.existsSync(destPath)) {
console.log(`File already exists: ${filename}`);
return;
}
console.log(`Downloading ${match} to ${destPath}...`);
https.get(match, (response) => {
if (response.statusCode === 200) {
const fileStream = fs.createWriteStream(destPath);
response.pipe(fileStream);
fileStream.on("finish", () => {
fileStream.close();
console.log(`Downloaded ${match} to ${destPath}`);
});
} else {
console.error(
`Failed to download ${match}:`,
response.statusCode,
);
}
});
});
}
}
});
}
for (const dir of rootDir) {
searchFiles(dir);
}