118 lines
2.8 KiB
JavaScript
118 lines
2.8 KiB
JavaScript
const fs = require('fs');
|
|
const cheerio = require('cheerio');
|
|
const Swal = require('sweetalert2')
|
|
const encode = require('./encode')
|
|
const path = require('path')
|
|
|
|
require('dotenv').config();
|
|
//Get the value from .env
|
|
const isEncrypted = process.env.ENCRYPTED;
|
|
|
|
function saveXmlToFile(xmlData, filePath) {
|
|
console.log(filePath);
|
|
// Check if the file path is provided
|
|
if (!filePath) {
|
|
console.error('File path is required.');
|
|
return;
|
|
}
|
|
|
|
// Write the XML data to the specified file path
|
|
fs.writeFileSync(filePath, xmlData, 'utf8', (err) => {
|
|
console.log("fs write inside ");
|
|
alert("fs write inside")
|
|
if (err) {
|
|
console.error('Error writing XML file:', err);
|
|
} else {
|
|
console.log('XML file saved successfully.');
|
|
|
|
// Clear all data in local storage
|
|
localStorage.clear();
|
|
|
|
}
|
|
});
|
|
localStorage.clear();
|
|
|
|
// window.location.href = 'dashboard.html';
|
|
|
|
}
|
|
|
|
|
|
function savexml(UserFilename) {
|
|
|
|
// Get the XML string from localStorage
|
|
var xmlString = localStorage.getItem("xml");
|
|
|
|
//check Encrypted or not
|
|
var enc = "";
|
|
if (isEncrypted === 'YES') {
|
|
// Encrypt
|
|
enc = encode(xmlString);
|
|
}else{
|
|
enc = xmlString;
|
|
}
|
|
|
|
//XmlPath get from .env
|
|
const xmlPathenv=process.env.XML_FILE_PATH;
|
|
const xmlPath = path.join(__dirname, xmlPathenv)
|
|
// Get the json string from localStorage for set fileName
|
|
var jsonData = localStorage.getItem("json");
|
|
|
|
// Create a filename using on the subtitle
|
|
var xmlfilename = `${UserFilename}.xml`;
|
|
// var jsonfilename = `${UserFilename}.json`;
|
|
|
|
// file path where you want to save the XML file
|
|
// const xmlfilePath = process.cwd() + `/${xmlPath}` + xmlfilename;
|
|
|
|
const xmlfilePath = `${xmlPath}` + xmlfilename;
|
|
// const jsonfilePath = process.cwd() + '/xml/' + jsonfilename;
|
|
|
|
// Call the function to save the XML file
|
|
saveXmlToFile(enc, xmlfilePath);
|
|
|
|
}
|
|
|
|
function promptBox(){
|
|
|
|
|
|
var jsonData = localStorage.getItem("json");
|
|
var jsonString = JSON.parse(jsonData);
|
|
var getTitle = jsonString.question_paper_title;
|
|
var RawFileName = removeHtmlTags(getTitle);
|
|
// console.log(RawFileName)
|
|
|
|
Swal.fire({
|
|
title: 'Enter Filename',
|
|
input: 'text',
|
|
inputValue: RawFileName,
|
|
showCancelButton: true,
|
|
confirmButtonText: 'Submit',
|
|
cancelButtonText: 'Cancel',
|
|
inputValidator: (value) => {
|
|
if (!value) {
|
|
return 'You need to enter Filename!'
|
|
}
|
|
}
|
|
}).then((result) => {
|
|
if (result.isConfirmed) {
|
|
|
|
const inputValue = result.value
|
|
savexml(inputValue)
|
|
|
|
}
|
|
})
|
|
|
|
|
|
}
|
|
|
|
// function using for REMOVE HTML ELEMENT in the String
|
|
function removeHtmlTags(inputString) {
|
|
|
|
const $ = cheerio.load(inputString);
|
|
const text = $.text();
|
|
|
|
return text;
|
|
}
|
|
|
|
|
|
|