-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwebserver-scripts-fileManager.js
More file actions
187 lines (147 loc) · 5.89 KB
/
webserver-scripts-fileManager.js
File metadata and controls
187 lines (147 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// read text string from file
function readFile(types='*.*', timeout=60) {
return new Promise((res) => {
const input = document.createElement('input')
input.type = 'file'
input.accept = types
input.addEventListener('change', async (event) => {
if (event.target.files.length === 0) res(undefined)
const type = event.target.files[0].name.split('.').slice(-1)[0]
if (!types.includes(type.toLowerCase()) && !types.includes(type)) res(undefined)
const fileResponse = await fetch(window.URL.createObjectURL(event.target.files[0]))
fileResponse.fileType = type
res(fileResponse)
})
input.click()
setTimeout(() => {
res(undefined)
delete input
}, timeout*1000)
})
}
// parse CSV from file in JSON (nested arrays)
function parseCSV(file, timeout=60) {
const parse = (csvString) => {
if (typeof csvString !== 'string') return undefined
const lines = []
let currentLine = []
let currentField = ''
let insideQuotes = false
let currentChar = ''
for (let i = 0; i < csvString.length; i++) {
currentChar = csvString[i]
if (insideQuotes) {
if (currentChar === '"') {
if (csvString[i + 1] === '"') {
currentField += '"'
i++
} else insideQuotes = false
} else currentField += currentChar
} else {
if (currentChar === '"') insideQuotes = true
else if (currentChar === ',') {
currentLine.push(currentField)
currentField = ''
} else if (currentChar === '\n' || (currentChar === '\r' && csvString[i + 1] === '\n')) {
if (currentChar === '\r') i++
currentLine.push(currentField)
lines.push(currentLine)
currentLine = []
currentField = ''
} else currentField += currentChar
}
}
if (currentField || csvString[csvString.length - 1] === ',') currentLine.push(currentField)
if (currentLine.length > 0) lines.push(currentLine)
const numColumns = lines[0]?.length || 0
for (let line of lines) {
if (line.length !== numColumns) return undefined
}
return lines
}
return new Promise(async (res) => {
setTimeout(() => res(undefined), timeout*1000)
file.text().then((text) => res(parse(text))).then(() => res(undefined))
})
}
function parseXLSX(file, timeout=60) {
const parse = (xlsxString => {
if (typeof xlsxString !== 'string') return undefined
})
console.log(file)
// return new Promise(async (res) => {
// setTimeout(() => res(undefined), timeout*1000)
// file.text().then((text) => res(parse(text))).then(() => res(undefined))
// })
}
// Function to handle file input
// document.getElementById('input').addEventListener('change', function(e) {
// const file = e.target.files[0];
// const reader = new FileReader();
// // When file is read
// reader.onload = function(event) {
// const arrayBuffer = event.target.result;
// processExcelFile(arrayBuffer);
// };
// reader.readAsArrayBuffer(file);
// });
// Function to process the Excel file
// async function processExcelFile(arrayBuffer) {
// // Unzip the xlsx file
// const zip = await JSZip.loadAsync(arrayBuffer);
// // Get the 'sheet1.xml' from the unzipped file
// const sheetFile = zip.file("xl/worksheets/sheet1.xml");
// if (sheetFile) {
// const sheetXml = await sheetFile.async("string");
// // Parse the XML content
// const parser = new DOMParser();
// const xmlDoc = parser.parseFromString(sheetXml, "text/xml");
// // Extract cell values from the XML
// const rows = xmlDoc.getElementsByTagName("row");
// for (let i = 0; i < rows.length; i++) {
// const cells = rows[i].getElementsByTagName("c");
// let rowData = [];
// for (let j = 0; j < cells.length; j++) {
// const valueElement = cells[j].getElementsByTagName("v")[0];
// const value = valueElement ? valueElement.textContent : "";
// rowData.push(value);
// }
// console.log(rowData.join(", ")); // Log the row as comma-separated values
// }
// }
// }
// serialize JSON (nested arrays) to CSV
function serializeCSV(csvData, timeout=60) {
const serialize = () => {
if (!Array.isArray(csvData) || csvData.length === 0) return undefined
let csvString = ''
for (const line of csvData) {
if (!Array.isArray(line) || line.length === 0) continue
let lineString = ''
for (const element of line) lineString += `"${element.replaceAll('"', '""')}",`
csvString += lineString.slice(0, -1) + '\r\n'
}
return csvString
}
return new Promise((res) => {
setTimeout(() => res(undefined), timeout*1000)
res(serialize())
})
}
// write data (text/plain by default) to file
function writeFile(data, filename, type='text/plain', timeout=60) {
return new Promise((res) => {
if (data === undefined) {
res(false)
return
}
setTimeout(() => res(false), timeout*1000)
const link = document.createElement('a')
const file = new Blob([data], { type: type })
link.href = URL.createObjectURL(file)
link.download = filename
link.click()
URL.revokeObjectURL(link.href)
res(true)
})
}