Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"@signalk/signalk-schema": "^1.7.1",
"@signalk/streams": "5.1.x",
"api-schema-builder": "^2.0.11",
"archiver": "^7.0.1",
"baconjs": "^1.0.1",
"bcryptjs": "^2.4.3",
"body-parser": "^1.14.1",
Expand All @@ -95,7 +96,6 @@
"errorhandler": "^1.3.0",
"esm-resolve": "^1.0.11",
"express": "^4.10.4",
"express-easy-zip": "^1.1.5",
"express-namespace": "^0.1.1",
"express-rate-limit": "^8.2.1",
"figlet": "^1.2.0",
Expand Down Expand Up @@ -141,6 +141,7 @@
"@eslint/js": "^9.24.0",
"@signalk/typedoc-signalk-theme": "^0.3.0",
"@tsconfig/node20": "^20.1.5",
"@types/archiver": "^7.0.0",
"@types/baconjs": "^0.7.34",
"@types/busboy": "^1.5.0",
"@types/chai": "^4.2.15",
Expand Down
1 change: 0 additions & 1 deletion src/@types/express-easy-zip.d.ts

This file was deleted.

8 changes: 2 additions & 6 deletions src/serverroutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import busboy from 'busboy'
import commandExists from 'command-exists'
import express, { IRouter, NextFunction, Request, Response } from 'express'
import zip from 'express-easy-zip'
import { sendZip } from './zip'
import fs from 'fs'
import { forIn, get, isNumber, isUndefined, set, uniq, unset } from 'lodash'
import moment from 'moment'
Expand Down Expand Up @@ -1268,8 +1268,6 @@ module.exports = function (
}
)

app.use(zip())

app.get(`${SERVERROUTESPREFIX}/backup`, (req: Request, res: Response) => {
readdir(app.config.configPath).then((filenames) => {
const files = filenames
Expand All @@ -1290,9 +1288,7 @@ module.exports = function (
name
}
})
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const anyRes = res as any
anyRes.zip({
sendZip(res, {
files,
filename: `signalk-${moment().format('MMM-DD-YYYY-HHTmm')}.backup`
})
Expand Down
48 changes: 48 additions & 0 deletions src/zip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import archiver from 'archiver'
import { Response } from 'express'
import fs from 'fs'

interface ZipFile {
path: string
name: string
}

interface ZipOptions {
files: ZipFile[]
filename: string
}

/**
* Send a zip file as a download response.
* Replacement for express-easy-zip using archiver directly.
*/
export function sendZip(res: Response, options: ZipOptions): void {
const { files, filename } = options

res.setHeader('Content-Type', 'application/zip')
res.setHeader('Content-Disposition', `attachment; filename="${filename}.zip"`)

const archive = archiver('zip', {
zlib: { level: 9 }
})

archive.on('error', (err: Error) => {
console.error('Zip archive error:', err)
if (!res.headersSent) {
res.status(500).send('Error creating zip file')
}
})

archive.pipe(res)

for (const file of files) {
const stat = fs.statSync(file.path)
if (stat.isDirectory()) {
archive.directory(file.path, file.name)
} else {
archive.file(file.path, { name: file.name })
}
}

archive.finalize()
}
Loading