feat: generate document title automatically on the basis file name

This commit is contained in:
prafull-opensignlabs
2024-09-23 18:09:23 +05:30
parent ce671d4294
commit 0f84cd8949
4 changed files with 57 additions and 3 deletions
+42
View File
@@ -2462,3 +2462,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";
}
}
+1 -1
View File
@@ -16,7 +16,7 @@ export default function reportJson(id) {
"Note",
"Folder",
"File",
"Logs",
"Status",
"Signers"
];
const contactbook = ["Sr.No", "Name", "Email", "Phone"];
+10 -2
View File
@@ -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";
@@ -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();
}
@@ -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();
}
@@ -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',
@@ -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',
@@ -229,6 +231,7 @@ export default function reportJson(id, userId) {
'Folder.Name',
'URL',
'ExtUserPtr.Name',
'ExtUserPtr.Email',
'ExtUserPtr.active_mail_adapter',
'Signers.Name',
'Signers.Email',
@@ -271,6 +274,7 @@ export default function reportJson(id, userId) {
'Name',
'URL',
'ExtUserPtr.Name',
'ExtUserPtr.Email',
'ExtUserPtr.active_mail_adapter',
'Signers.Name',
'Signers.UserId',