classic-ui/scripts/helpers/save.js

38 lines
773 B
JavaScript
Raw Normal View History

import fs from 'fs'
2022-09-21 07:08:59 +03:00
import Jszip from 'jszip'
const compressionConfig = {
type: "nodebuffer",
compression: "DEFLATE",
compressionOptions: {
level: 9
}
}
const fileConfig = {
binary: true,
compression: "DEFLATE"
}
export async function save(filePath) {
const jszip = new Jszip()
const directories = filePath.split('/')
const fileName = directories[directories.length - 1]
try {
2022-09-21 07:08:59 +03:00
const data = fs.readFileSync(`${filePath}`)
await jszip.file(`${fileName}`, data, fileConfig)
await jszip.generateNodeStream({
...compressionConfig,
streamFiles: true
})
.pipe(fs.createWriteStream(`${filePath}.zip`))
return true
} catch (err) {
2022-09-21 07:08:59 +03:00
console.log('on save error', filePath, err.message)
return false
}
}