|
| 1 | +const { v4 } = require("uuid"); |
| 2 | +const xlsx = require("node-xlsx").default; |
| 3 | +const path = require("path"); |
| 4 | +const fs = require("fs"); |
| 5 | +const { |
| 6 | + createdDate, |
| 7 | + trashFile, |
| 8 | + writeToServerDocuments, |
| 9 | +} = require("../../utils/files"); |
| 10 | +const { tokenizeString } = require("../../utils/tokenizer"); |
| 11 | +const { default: slugify } = require("slugify"); |
| 12 | + |
| 13 | +function convertToCSV(data) { |
| 14 | + return data |
| 15 | + .map((row) => |
| 16 | + row |
| 17 | + .map((cell) => { |
| 18 | + if (cell === null || cell === undefined) return ""; |
| 19 | + if (typeof cell === "string" && cell.includes(",")) |
| 20 | + return `"${cell}"`; |
| 21 | + return cell; |
| 22 | + }) |
| 23 | + .join(",") |
| 24 | + ) |
| 25 | + .join("\n"); |
| 26 | +} |
| 27 | + |
| 28 | +async function asXlsx({ fullFilePath = "", filename = "" }) { |
| 29 | + const documents = []; |
| 30 | + const folderName = slugify(`${path.basename(filename)}-${v4().slice(0, 4)}`, { |
| 31 | + lower: true, |
| 32 | + trim: true, |
| 33 | + }); |
| 34 | + |
| 35 | + const outFolderPath = |
| 36 | + process.env.NODE_ENV === "development" |
| 37 | + ? path.resolve( |
| 38 | + __dirname, |
| 39 | + `../../../server/storage/documents/${folderName}` |
| 40 | + ) |
| 41 | + : path.resolve(process.env.STORAGE_DIR, `documents/${folderName}`); |
| 42 | + |
| 43 | + try { |
| 44 | + const workSheetsFromFile = xlsx.parse(fullFilePath); |
| 45 | + if (!fs.existsSync(outFolderPath)) |
| 46 | + fs.mkdirSync(outFolderPath, { recursive: true }); |
| 47 | + |
| 48 | + for (const sheet of workSheetsFromFile) { |
| 49 | + try { |
| 50 | + const { name, data } = sheet; |
| 51 | + const content = convertToCSV(data); |
| 52 | + |
| 53 | + if (!content?.length) { |
| 54 | + console.warn(`Sheet "${name}" is empty. Skipping.`); |
| 55 | + continue; |
| 56 | + } |
| 57 | + |
| 58 | + console.log(`-- Processing sheet: ${name} --`); |
| 59 | + const sheetData = { |
| 60 | + id: v4(), |
| 61 | + url: `file://${path.join(outFolderPath, `${slugify(name)}.csv`)}`, |
| 62 | + title: `${filename} - Sheet:${name}`, |
| 63 | + docAuthor: "Unknown", |
| 64 | + description: `Spreadsheet data from sheet: ${name}`, |
| 65 | + docSource: "an xlsx file uploaded by the user.", |
| 66 | + chunkSource: "", |
| 67 | + published: createdDate(fullFilePath), |
| 68 | + wordCount: content.split(/\s+/).length, |
| 69 | + pageContent: content, |
| 70 | + token_count_estimate: tokenizeString(content).length, |
| 71 | + }; |
| 72 | + |
| 73 | + const document = writeToServerDocuments( |
| 74 | + sheetData, |
| 75 | + `sheet-${slugify(name)}`, |
| 76 | + outFolderPath |
| 77 | + ); |
| 78 | + documents.push(document); |
| 79 | + console.log( |
| 80 | + `[SUCCESS]: Sheet "${name}" converted & ready for embedding.` |
| 81 | + ); |
| 82 | + } catch (err) { |
| 83 | + console.error(`Error processing sheet "${name}":`, err); |
| 84 | + continue; |
| 85 | + } |
| 86 | + } |
| 87 | + } catch (err) { |
| 88 | + console.error("Could not process xlsx file!", err); |
| 89 | + return { |
| 90 | + success: false, |
| 91 | + reason: `Error processing ${filename}: ${err.message}`, |
| 92 | + documents: [], |
| 93 | + }; |
| 94 | + } finally { |
| 95 | + trashFile(fullFilePath); |
| 96 | + } |
| 97 | + |
| 98 | + if (documents.length === 0) { |
| 99 | + console.error(`No valid sheets found in ${filename}.`); |
| 100 | + return { |
| 101 | + success: false, |
| 102 | + reason: `No valid sheets found in ${filename}.`, |
| 103 | + documents: [], |
| 104 | + }; |
| 105 | + } |
| 106 | + |
| 107 | + console.log( |
| 108 | + `[SUCCESS]: ${filename} fully processed. Created ${documents.length} document(s).\n` |
| 109 | + ); |
| 110 | + return { success: true, reason: null, documents }; |
| 111 | +} |
| 112 | + |
| 113 | +module.exports = asXlsx; |
0 commit comments