Skip to content

Commit

Permalink
Merge branch 'main' into ss-resource-updates-on-ai-topic
Browse files Browse the repository at this point in the history
  • Loading branch information
ToniBonittoGSA authored Jul 10, 2024
2 parents f117efe + 38c867c commit 710f66a
Show file tree
Hide file tree
Showing 79 changed files with 1,780 additions and 3,557 deletions.
18 changes: 11 additions & 7 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -111,25 +111,29 @@ security:

menu:
primary:
- Name: "News"
Identifier: "news"
- Name: "Blogs"
Identifier: "blogs"
URL: "/news/"
Weight: 1
- Name: "Events"
Identifier: "events"
URL: "/events/"
Weight: 2
- Name: "Resources"
Identifier: "resources"
URL: "/resources/"
Weight: 3
- Name: "Events"
Identifier: "events"
URL: "/events/"
Weight: 2
- Name: "Communities"
Identifier: "communities"
URL: "/communities/"
Weight: 4
- Name: "Guides"
Identifier: "guides"
URL: "/guides/"
Weight: 4
- Name: "Tools"
Identifier: "tools"
URL: "/services/"
URL: "/services/directory/"
Weight: 5

contribute:
Expand Down
89 changes: 67 additions & 22 deletions config/gulp/file-prep.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { src, series } = require("gulp");
const sharp = require("sharp");
const del = require("del");
const tap = require("gulp-tap");
const sizeOf = require("image-size");
Expand Down Expand Up @@ -26,6 +27,29 @@ const extensionsString = allExtensions
const imageRegex = /(png|jpg|jpeg)/;
const fileRegex = /(doc|docx|pdf|ppt|pptx|pptm|xls|xlsx)/;


/**
* Converts JPG images to PNG format
* @param {string} imagePath - path of the image file
*/
async function convertJpgToPng(imagePath) {
console.log(`Converting image ${imagePath} to PNG`);
const outputPath = imagePath.replace(/\.jpe?g$/i, ".png");

await sharp(imagePath)
.toFormat("png")
.toFile(outputPath);

// Check if the original JPG file exists before unlinking
if (fs.existsSync(imagePath)) {
fs.unlinkSync(imagePath); // Remove the original JPG file
}

return path.basename(outputPath);
}



/**
* Object containing working folder paths used for lifecycle steps of uploading
* to-process contains the normalized filename, static files are upload to s3 from here
Expand All @@ -43,6 +67,7 @@ const filePaths = {
},
};


/**
* Creates directories for each step of the file uploading process
* These directories are removed when a file has been uploaded
Expand All @@ -53,34 +78,54 @@ function fileTidy(done) {
let filetype = "";
let paths = filePaths;

fs.readdir(paths.uploads, (err, files) => {
// process.stdout.write(files.length.toString() + "\n");
fs.readdir(paths.uploads, async (err, files) => {
if (err) {
console.error(`Failed to read directory ${paths.uploads}: ${err.message}`);
done(err);
return;
}

for (let file of files) {
// checks for .pdf, .png
if (allExtensions.includes(path.extname(file))) {
// creates new normalized file name
newfileName = cleanFileName(file);
const fileExt = path.extname(file);
if (allExtensions.includes(fileExt)) {
filetype = fileType(file);
const dirToProcess = paths[filetype].toProcess;
// create working directories if they do not exist
createDir(paths[filetype].toProcess, 3);
if (filetype === "image") createDir(paths[filetype].processed, 3);
// copies uploaded file to /to-process with new normalized name
fs.renameSync(
`${paths.uploads}/${file}`,
`${paths[filetype].toProcess}/${newfileName}`
);
// copies uploaded file to /to-process with new normalized name
// convert jpg to png
if (filetype === "image" && fileExt === ".jpg" || fileExt === ".jpeg") {
let convertedPng = await convertJpgToPng(path.join(paths.uploads, file)).catch
((err) => {
console.error(`Error converting image ${file} to PNG: ${err.message}`);
return;
});
// ensure the folder for the process image exists.
if (convertedPng != file) {
file = convertedPng;
}

}
newfileName = cleanFileName(file);
const newFilePath = path.join(dirToProcess, newfileName);

// Rename and move the file to the new path
try {
console.log(`Moving file from ${path.join(paths.uploads, file)} to ${newFilePath}`);
fs.renameSync(path.join(paths.uploads, file), newFilePath);
} catch (renameError) {
console.error(`Error moving file from ${path.join(paths.uploads, file)} to ${newFilePath}: ${renameError.message}`);
continue;
}
}
}
if (err) {
process.output.write(
`Error cleaning and copying file [${file}]
Error message: ${err.message}`
);
}
done();
});
done();
}



/**
* creates the originals and to-process directories for both files and images
* ./content/uploads/_working-images/to-process";
Expand Down Expand Up @@ -117,8 +162,8 @@ function createDir(directoryPath, foldersDeep) {

/**
* Checks the file extension and returns a string value of file or image
* @param {String} extension - file name extension (.pdf, .png, etc...)
* @returns a string value of image or file
* @param {string} extension - file name extension (.pdf, .png, etc...)
* @returns {string} a string value of image or file
*/
function fileType(extension) {
if (fileRegex.test(extension)) return "file";
Expand All @@ -133,7 +178,7 @@ function fileType(extension) {
* @returns filename in string format
*/
function cleanFileName(origfilename) {
return origfilename
return origfilename
.toLowerCase()
.replace(/[ &$_#!?.]/g, "-")
.replace(/-+/g, "-") // multiple dashes to a single dash
Expand All @@ -143,7 +188,7 @@ function cleanFileName(origfilename) {
.replace(/^\d{2,4}-*x-*\d{2,4}-*/g, "") // strip leading dimensions
.replace(/-\./g, ".") // remove leading dashes
.replace(/^-/g, "") // removes dashes from start of filename
.toLowerCase();
.toLowerCase();
}

/**
Expand Down
51 changes: 37 additions & 14 deletions config/gulp/file-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,29 +120,52 @@ async function processImageVariants(image) {
* Read image(s) in the upload directory
*/
async function processImages() {
fs.readdir(`${processImagesDirectory}`, (err, images) => {
// images.length returns undefined when no images exist
if (images === undefined) {
console.error("No images to process");
}
if (!err) {
images.forEach((image) => {
return new Promise((resolve, reject) => {
fs.readdir(`${processImagesDirectory}`, async (err, images) => {
if (images === undefined) {
resolve();
return;
}
if (err) {
console.error(`Error reading directory: ${err.message}`);
reject(err);
return;
}

const processingPromises = images.map((image) => {
const imageToProcess = getImageDetails(image);
processImageOriginal(imageToProcess);
processImageVariants(imageToProcess);
return Promise.all([
processImageOriginal(imageToProcess),
processImageVariants(imageToProcess)
]);
});
} else {
console.error(`Error processing file: [${images}]. ${err.message}`);
}

try {
await Promise.all(processingPromises);
resolve();
} catch (err) {
console.error(`Error processing images: ${err.message}`);
reject(err);
}
});
});
}

/**
* Removes the /to-process temporary working folder after variants are created
*/
function removeProcessedImage() {
console.log("Removing processed images");
return del([`content/uploads/_working-images/to-process/*}`]);
return new Promise((resolve, reject) => {
const imageDir = "content/uploads/_working-images/processed";

if (fs.existsSync(imageDir) && fs.readdirSync(imageDir).length > 0) {
return del([
"content/uploads/_working-images/to-process",
]).then(() => resolve()).catch((err) => reject(err));
} else {
resolve();
}
});
}

exports.do = series(processImages, removeProcessedImage);
98 changes: 91 additions & 7 deletions config/gulp/file-upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ const awspublish = require("gulp-awspublish");
const rename = require("gulp-rename");
const del = require("del");
const vinylPaths = require("vinyl-paths");

const fs = require("fs");
const { resolve } = require("path");
// Create a new publisher using S3 options
const publisher = awspublish.create({
region: "us-east-1", // Change to your AWS region
Expand Down Expand Up @@ -58,11 +59,94 @@ function uploadFile() {
}

function cleanup() {
console.log("cleanup");
return del([
"content/uploads/_working-images",
"content/uploads/_working-files",
]);
return new Promise((resolve, reject) => {
let imageDir = "content/uploads/_working-images/processed";
let fileDir = "content/uploads/_working-files/to-process";

if (fs.existsSync(imageDir)) {
if (fs.readdirSync(imageDir).length > 0) {
console.log(`Images have number of files ${fs.readdirSync(imageDir).length}`);
// delete tht folder
del([imageDir]);
resolve();
} else {
resolve();
}
} else {
resolve();
}
});
}

/**
* Determines which files to upload and initiates the upload process.
* @param {Function} done - A callback function to signal that the task is complete.
*
* Key Points:
* 1. The function checks if there are images and files to upload.
* 2. If there are images, it initiates the upload process.
* 3. If there are files, it initiates the upload process.
* 4. The function keeps track of the number of uploads that need to be completed.
* 5. After all uploads are complete, the `done` callback is called to signal the task is complete.
*/
function determineWhichToUpload() {
return new Promise((resolve, reject) => {
const imageDir = "content/uploads/_working-images/processed/";
const fileDir = "content/uploads/_working-files/to-process/";
let imageDirExists = fs.existsSync(imageDir);
let fileDirExists = fs.existsSync(fileDir);
let imageFiles = imageDirExists ? fs.readdirSync(imageDir) : [];
let fileFiles = fileDirExists ? fs.readdirSync(fileDir) : [];

if (!imageDirExists && !fileDirExists) {
console.log("No files or images to upload.");
return resolve();
}

let uploadsToComplete = 0;
let uploadsCompleted = 0;

const checkCompletion = () => {
if (uploadsCompleted === uploadsToComplete) {
resolve();
}
};

if (imageFiles.length > 0) {
uploadsToComplete += 1;
const imageUploadStream = uploadImage();
imageUploadStream.on('finish', () => {
uploadsCompleted += 1;
checkCompletion();
});
imageUploadStream.on('error', (err) => {
console.error("Error uploading images:", err);
reject(err);
});
}

if (fileFiles.length > 0) {
uploadsToComplete += 1;
const fileUploadStream = uploadFile();
fileUploadStream.on('finish', () => {
uploadsCompleted += 1;
checkCompletion();
});
fileUploadStream.on('error', (err) => {
console.error("Error uploading files:", err);
reject(err);
});
}

if (uploadsToComplete === 0) {
resolve(); // If no uploads are initiated, resolve immediately.
}
});
}

exports.do = gulp.series(uploadImage, uploadFile, cleanup);


/**
* Exports a Gulp task series that first determines which files to upload and then cleans up.
*/
exports.do = gulp.series(determineWhichToUpload, cleanup);
Loading

0 comments on commit 710f66a

Please sign in to comment.