Skip to content

Commit

Permalink
Add file format validation
Browse files Browse the repository at this point in the history
- Add underscore as valid character in file name
  • Loading branch information
clemensheithecker committed Mar 28, 2021
1 parent 0092263 commit ef7b786
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Try out the app on [pdf-duplex-scan.vercel.app](https://pdf-duplex-scan.vercel.a
- Array reordering based on pattern
- User interface for dark and light color schemes
- JavaScript browser validation
- File format validation

## Getting started

Expand Down
84 changes: 56 additions & 28 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,6 @@ dropzone.addEventListener("drop", (e) => {
// Set focus to dropzone
dropzone.focus();

// Re-enable and reset other input fields if previously disabled because
// of prior jobs
if (submit.disabled === true && fileName.disabled === true) {
// Enable submit button and file name input
submit.disabled = false;
fileName.disabled = false;

// Reset submit button styles
submit.value = "Reorder PDF!";
submit.classList.remove("bg-green-600", "dark:bg-green-800");
submit.classList.add(
"bg-blue-600",
"dark:bg-blue-800",
"hover:bg-blue-700",
"dark:hover:bg-blue-700"
);
}

// Hold data that is dropped on dropzone
const dataTransfer = e.dataTransfer;

Expand All @@ -96,15 +78,61 @@ dropzone.addEventListener("drop", (e) => {
// Read as binary file
fileReader.readAsDataURL(dataTransfer.files[0]);

// Get source PDF file name, remove file extension (.pdf) and any everything
// but a word character, underscore, space, or minus (-)
pdfName = dataTransfer.files[0].name
.replace(".pdf", "")
.replace(/[^\w -]/g, "")
.trim();
// Get file extension
const fileExtension = dataTransfer.files[0].name
.replace(/^(.*?)\./, "")
.toLowerCase();

// Check if selected file is a PDF
if (fileExtension === "pdf") {
console.log("Hello");
// Reset dropzone styles
const dropzoneParentElement = dropzone.parentElement;
const smallElement = dropzoneParentElement.querySelector("small");

dropzone.classList.remove(
"border-red-600",
"dark:border-red-400",
"focus:border-red-600",
"dark:focus:border-red-400"
);
smallElement.classList.add("invisible");

// Re-enable and reset other input fields if previously disabled because
// of prior jobs
if (submit.disabled === true || fileName.disabled === true) {
// Enable submit button and file name input
submit.disabled = false;
fileName.disabled = false;

// Reset submit button styles
submit.value = "Reorder PDF!";
submit.classList.remove("bg-green-600", "dark:bg-green-800");
submit.classList.add(
"bg-blue-600",
"dark:bg-blue-800",
"hover:bg-blue-700",
"dark:hover:bg-blue-700"
);
}

// Set file name input to source PDF file name
fileName.value = pdfName;
// Get source PDF file name, remove file extension (.pdf) and any everything
// but a word character, underscore, space, or minus (-)
pdfName = dataTransfer.files[0].name
.replace(".pdf", "")
.replace(/[^\w_ -]/g, "")
.trim();

// Set file name input to source PDF file name
fileName.value = pdfName;
} else {
// Disable submit button
submit.disabled = true;
submit.classList.remove("hover:bg-blue-700", "dark:hover:bg-blue-700");

// Set error message for dropzone
setErrorFor(dropzone, "Please select a PDF file");
}
});

// Open file selection dialog on dropzone click
Expand All @@ -130,7 +158,7 @@ fileInput.addEventListener("change", () => {
// but a word character, underscore, space, or minus (-)
pdfName = fileInput.files[0].name
.replace(".pdf", "")
.replace(/[^\w -]/g, "")
.replace(/[^\w_ -]/g, "")
.trim();

// Set file name input to source PDF file name
Expand Down Expand Up @@ -246,7 +274,7 @@ function setSuccessFor(input) {
function isFileName(fileName) {
// Returns true if fileName consists only of a word character, underscore,
// space, or minus (-)
return /^[ \w\s-]+$/.test(fileName);
return /^[ \w_ -]+$/.test(fileName);
}

async function createPdf() {
Expand Down

1 comment on commit ef7b786

@vercel
Copy link

@vercel vercel bot commented on ef7b786 Mar 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.