Add gzipped download endpoint
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
import passport from "passport"
|
||||
import createError from "http-errors"
|
||||
import autobind from "autobind-decorator"
|
||||
import zlib from "zlib"
|
||||
import { Readable } from "stream"
|
||||
import { catchAll } from "."
|
||||
|
||||
@autobind
|
||||
@@ -38,6 +40,13 @@ export class TeamRoutes {
|
||||
passport.authenticate("bearer", { session: false }),
|
||||
catchAll(this.deleteTeam)
|
||||
)
|
||||
|
||||
app
|
||||
.route("/teams/status")
|
||||
.get(
|
||||
passport.authenticate("bearer", { session: false }),
|
||||
catchAll(this.getTeamStatus)
|
||||
)
|
||||
}
|
||||
|
||||
async listTeams(req, res, next) {
|
||||
@@ -146,4 +155,32 @@ export class TeamRoutes {
|
||||
|
||||
res.json({})
|
||||
}
|
||||
|
||||
async getTeamStatus(req, res, next) {
|
||||
const Team = this.db.Team
|
||||
const Activity = this.db.Activity
|
||||
let teams = await Team.find({}).exec()
|
||||
|
||||
teams = teams.map((doc) => doc.toObject({ versionKey: false }))
|
||||
|
||||
for (let team of teams) {
|
||||
let activities = await Activity.find({ team: team._id }).exec()
|
||||
|
||||
team.activities = activities.map((doc) =>
|
||||
doc.toObject({ versionKey: false })
|
||||
)
|
||||
}
|
||||
|
||||
const gzip = zlib.createGzip()
|
||||
let readable = new Readable()
|
||||
|
||||
readable.push(JSON.stringify(teams, null, " "))
|
||||
readable.push(null)
|
||||
|
||||
res.writeHead(200, {
|
||||
"Content-Type": "text/html",
|
||||
"Content-Encoding": "gzip",
|
||||
})
|
||||
readable.pipe(gzip).pipe(res)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user