-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathi-hope-it-works.js
More file actions
61 lines (56 loc) · 1.64 KB
/
i-hope-it-works.js
File metadata and controls
61 lines (56 loc) · 1.64 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
const readline = require('readline')
const fs = require('fs')
const path = require('path')
const math = require('mathjs')
function lighter () {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
})
rl.question(
'Enter a file to split (or type "exit" to stop): ',
function (input) {
if (input.toLowerCase() === 'exit') {
rl.close()
return
}
if (fs.existsSync(input)) {
const fileStat = fs.statSync(input)
if (fileStat.isFile()) {
const file_size = fileStat.size
if (file_size <= 100 * 1024 * 1024) {
console.log(input)
} else {
const {
dir: fileDir,
name: fileName,
ext: fileExt
} = path.parse(input)
const num_parts = math.ceil(file_size / (100 * 1024 * 1024))
const dir_name = `${fileName}_parts`
fs.mkdirSync(dir_name, { recursive: true })
const fileData = fs.readFileSync(input)
for (let i = 0; i < num_parts; i++) {
const part_path = path.join(
dir_name,
`${fileName}_part${i + 1}${fileExt}`
)
const partData = fileData.slice(
i * 100 * 1024 * 1024,
(i + 1) * 100 * 1024 * 1024
)
fs.writeFileSync(part_path, partData)
console.log(part_path)
}
}
} else {
console.log(`Not a file: ${input}`)
}
} else {
console.log(`File not found: ${input}`)
}
lighter()
}
)
}
lighter()