npe_gerber/index.js
2025-06-14 22:14:45 +01:00

257 lines
7.4 KiB
JavaScript

//Modules
const AdmZip = require('adm-zip');
const { emptyDirSync } = require('fs-extra');
const path = require('path');
const pcbStackup = require('pcb-stackup');
const sharp = require('sharp');
const { Readable } = require('node:stream');
const { Buffer } = require('node:buffer');
const {
existsSync,
accessSync,
createReadStream,
mkdirSync,
chmodSync,
constants,
} = require('node:fs');
//ensureDirSync method
function ensureDirSync(directory) {
if (!existsSync(directory)) {
mkdirSync(directory, { recursive: true });
chmodSync(directory, 0o644);
}
}
//Class definition
class ImageGenerator {
constructor(folderConfig, imgConfig, layerNames) {
this.tmpDir = folderConfig.tmpDir;
this.imgDir = folderConfig.imgDir;
this.imgConfig = imgConfig;
this.layerNames = layerNames;
//Ensure folders exist
try {
if (!existsSync(this.tmpDir)) throw 'Temp dir does not exist';
if (!existsSync(this.imgDir)) throw 'Image dir does not exist';
} catch (error) {
throw new Error(error);
}
//Check folder permissions
try {
accessSync(this.tmpDir, constants.R_OK | constants.W_OK);
accessSync(this.imgDir, constants.R_OK | constants.W_OK);
} catch (error) {
throw new Error(error);
}
}
/**
* Extracts the passed in zip file
* @param {string} fileName Name of the file to be extracted
* @param {string} tmpDir Temporary directory to extract to
* @returns {number} Number of objects contained in the archive
*/
static extractArchive(fileName, tmpDir) {
// Check archive exists
try {
if (!existsSync(fileName)) {
throw Error('Archive does not exist.');
}
if (!existsSync(tmpDir)) {
throw Error('Temporary folder does not exist.');
}
} catch (e) {
throw new Error(e);
}
const zip = new AdmZip(fileName);
zip.extractAllTo(path.join(tmpDir, 'archive'));
return zip.getEntries().length;
}
/**
* Temporary test method zip file
* @param {string} fileName Name of the file to be extracted
* @param {string} tmpDir Temporary directory to extract to
* @returns {number} Number of objects contained in the archive
*/
static testArchive(fileName, tmpDir) {
// Check archive exists
try {
if (!existsSync(fileName)) {
throw Error('Archive does not exist.');
}
if (!existsSync(tmpDir)) {
throw Error('Temporary folder does not exist.');
}
} catch (e) {
throw new Error(e);
}
try {
const zip = new AdmZip(fileName);
return zip.getEntries().length;
} catch (error) {
throw new Error(error);
}
}
/**
* Take in a directory of layer files and return an array of the layers files
* @param {string} dir Directory containing layer files
* @param {Array} layerNames Array of filenames for the desired layers
* @returns {Array} Array of paths to the layers files
*/
static getLayers(dir, layerNames) {
return new Promise((resolve, reject) => {
// Make sure the directory exists
if (!existsSync(dir)) {
return reject(new Error('Layers folder does not exist.'));
}
// Check that the required layer files exist in source dir
let layersValid = true;
layerNames.forEach((layer) => {
if (!existsSync(path.join(dir, layer))) layersValid = false;
});
if (!layersValid) return reject(new Error('Layer not found.'));
// Construct array of layers that match the supplied filenames array
const layers = layerNames.map((layerName) => ({
filename: layerName,
gerber: createReadStream(path.join(dir, layerName)),
}));
return resolve(layers);
});
}
/**
* Clean up the archive folder in the specified directory
* @param {string} dir Path to a directory to clean up
*/
static cleanupFiles(dir) {
try {
const folder = path.join(dir, 'archive');
emptyDirSync(folder);
} catch (err) {
throw new Error(err);
}
}
/**
* Take an archive containing gerber files, config object, temporary dir
* and output dir and create a PNG image from the gerber in the output dir
* @param {string} gerber Path to an archive file containing gerber
* @returns {Promise.<string>} Promise to return path to image
*/
gerberToImage(gerber) {
// Create output dir if it doesn't exist
try {
// fs.ensureDirSync(this.imgDir, 0o644);
ensureDirSync(this.imgDir);
} catch (e) {
throw new Error(e);
}
// Check temp and output dirs exist
try {
if (!existsSync(gerber)) {
throw Error('Archive does not exist.');
}
if (!existsSync(this.tmpDir)) {
throw Error('Temporary folder does not exist.');
}
if (!existsSync(this.imgDir)) {
throw Error('Output folder does not exist.');
}
} catch (e) {
throw new Error(e);
}
// Set filenames
const imageName = path.basename(gerber, '.zip');
const destFile = `${path.join(this.imgDir, imageName)}.png`;
return new Promise((resolve, reject) => {
ImageGenerator.extractArchive(gerber, this.tmpDir);
ImageGenerator.getLayers(
path.join(this.tmpDir, 'archive'),
this.layerNames,
)
.then(pcbStackup)
.then((stackup) => {
sharp(Buffer.from(stackup.top.svg), {
density: this.imgConfig.density,
})
.resize({ width: this.imgConfig.resizeWidth })
.png({ compressionLevel: this.imgConfig.compLevel })
.toFile(destFile);
})
.then(() => {
ImageGenerator.cleanupFiles(this.tmpDir);
resolve(destFile);
})
.catch((e) => {
ImageGenerator.cleanupFiles(this.tmpDir);
reject(new Error(e));
});
});
}
/**
* Take an archive containing gerber files and return a stream containing
* a PNG image from the gerber
* @param {string} gerber Path to an archive file containing gerber
* @returns {Promise.<stream.Readable>} Promise that resolves to a PNG stream
*/
gerberToStream(gerber) {
// Check temp and output dirs exist
try {
if (!existsSync(gerber)) {
throw Error('Archive does not exist.');
}
if (!existsSync(this.tmpDir)) {
throw Error('Temporary folder does not exist.');
}
if (!existsSync(this.imgDir)) {
throw Error('Output folder does not exist.');
}
} catch (e) {
throw new Error(e);
}
return new Promise((resolve, reject) => {
ImageGenerator.extractArchive(gerber, this.tmpDir);
ImageGenerator.getLayers(
path.join(this.tmpDir, 'archive'),
this.layerNames,
)
.then(pcbStackup)
.then((stackup) => {
sharp(Buffer.from(stackup.top.svg), {
density: this.imgConfig.density,
})
.resize({ width: this.imgConfig.resizeWidth })
.png({ compressionLevel: this.imgConfig.compLevel })
.toBuffer()
.then((buffer) => {
ImageGenerator.cleanupFiles(this.tmpDir);
const stream = new Readable();
stream.push(buffer);
stream.push(null);
resolve(stream);
});
})
.catch((e) => {
ImageGenerator.cleanupFiles(this.tmpDir);
reject(new Error(e));
});
});
}
}
module.exports = {
ImageGenerator,
};