Skip to content

Commit

Permalink
Merge pull request #1245 from OpenSignLabs/validation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew-opensignlabs authored Sep 23, 2024
2 parents adfdf76 + 0f84cd8 commit 76f5136
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
42 changes: 42 additions & 0 deletions apps/OpenSign/src/constant/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2474,3 +2474,45 @@ export const handleRotateWarning = (signerPos, pageNumber) => {
return false;
}
};

// `generateTitleFromFilename` to generate Title of document from file name
export function generateTitleFromFilename(filename) {
try {
// Step 1: Trim whitespace
let title = filename.trim();

// Step 2: Remove the file extension (everything after the last '.')
const lastDotIndex = title.lastIndexOf(".");
if (lastDotIndex > 0) {
title = title.substring(0, lastDotIndex);
}

// Step 3: Replace special characters (except Unicode letters, digits, spaces, and hyphens)
title = title.replace(/[^\p{L}\p{N}\s-]/gu, " ");

// Step 4: Replace multiple spaces with a single space
title = title.replace(/\s+/g, " ");

// Step 5: Capitalize first letter of each word (Title Case), handling Unicode characters
title = title.replace(
/\p{L}\S*/gu,
(txt) => txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase()
);

// Step 6: Restrict length of title (optional, let's say 100 characters)
if (title.length > 100) {
title = title.substring(0, 100).trim();
}

// Step 7: Handle empty or invalid title by falling back to "Untitled Document"
if (!title || title.length === 0) {
return "Untitled Document";
}

return title;
} catch (error) {
// Handle unexpected errors gracefully by returning a default title
console.error("Error generating title from filename:", error);
return "Untitled Document";
}
}
2 changes: 1 addition & 1 deletion apps/OpenSign/src/json/ReportJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default function reportJson(id) {
"Note",
"Folder",
"File",
"Logs",
"Status",
"Signers"
];
const contactbook = ["Sr.No", "Name", "Email", "Phone"];
Expand Down
12 changes: 10 additions & 2 deletions apps/OpenSign/src/pages/Form.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import SignersInput from "../components/shared/fields/SignersInput";
import Title from "../components/Title";
import PageNotFound from "./PageNotFound";
import { SaveFileSize } from "../constant/saveFileSize";
import { checkIsSubscribed, getFileName, toDataUrl } from "../constant/Utils";
import {
checkIsSubscribed,
generateTitleFromFilename,
getFileName,
toDataUrl
} from "../constant/Utils";
import { PDFDocument } from "pdf-lib";
import axios from "axios";
import { isEnableSubscription } from "../constant/const";
Expand Down Expand Up @@ -330,6 +335,8 @@ const Forms = (props) => {
setfileload(false);
if (response.url()) {
const tenantId = localStorage.getItem("TenantId");
const title = generateTitleFromFilename(file.name);
setFormData((obj) => ({ ...obj, Name: title }));
SaveFileSize(size, response.url(), tenantId);
return response.url();
}
Expand Down Expand Up @@ -367,9 +374,10 @@ const Forms = (props) => {
});
setFileUpload(response.url());
setfileload(false);

if (response.url()) {
const tenantId = localStorage.getItem("TenantId");
const title = generateTitleFromFilename(file.name);
setFormData((obj) => ({ ...obj, Name: title }));
SaveFileSize(size, response.url(), tenantId);
return response.url();
}
Expand Down
4 changes: 4 additions & 0 deletions apps/OpenSignServer/cloud/parsefunction/reportsJson.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export default function reportJson(id, userId) {
'Folder.Name',
'URL',
'ExtUserPtr.Name',
'ExtUserPtr.Email',
'ExtUserPtr.active_mail_adapter',
'Signers.Name',
'Signers.Email',
Expand Down Expand Up @@ -101,6 +102,7 @@ export default function reportJson(id, userId) {
'Folder.Name',
'URL',
'ExtUserPtr.Name',
'ExtUserPtr.Email',
'ExtUserPtr.active_mail_adapter',
'Signers.Name',
'Signers.Email',
Expand Down Expand Up @@ -230,6 +232,7 @@ export default function reportJson(id, userId) {
'Folder.Name',
'URL',
'ExtUserPtr.Name',
'ExtUserPtr.Email',
'ExtUserPtr.active_mail_adapter',
'Signers.Name',
'Signers.Email',
Expand Down Expand Up @@ -272,6 +275,7 @@ export default function reportJson(id, userId) {
'Name',
'URL',
'ExtUserPtr.Name',
'ExtUserPtr.Email',
'ExtUserPtr.active_mail_adapter',
'Signers.Name',
'Signers.UserId',
Expand Down

0 comments on commit 76f5136

Please sign in to comment.