Add gzipped download endpoint

This commit is contained in:
John Lyon-Smith
2018-04-26 18:00:16 -07:00
parent 5e5daa2e78
commit 96e2599cdb
9 changed files with 390 additions and 164 deletions

View File

@@ -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)
}
}