Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Csv upload #77

Merged
merged 7 commits into from
Jan 17, 2025
Merged
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
"@fastify/under-pressure": "^9.0.1",
"@sinclair/typebox": "^0.34.11",
"concurrently": "^9.0.1",
"csv-stringify": "^6.5.2",
"fastify": "^5.0.0",
"fastify-cli": "^7.0.0",
"fastify-plugin": "^5.0.1",
Expand Down
33 changes: 32 additions & 1 deletion src/routes/api/tasks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import {
TaskPaginationResultSchema
} from '../../../schemas/tasks.js'
import path from 'node:path'
import { pipeline } from 'node:stream/promises'
import fs from 'node:fs'
import { pipeline } from 'node:stream/promises'
import { createGzip } from 'node:zlib'
import { stringify } from 'csv-stringify'

const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
fastify.get(
Expand Down Expand Up @@ -355,6 +357,35 @@ const plugin: FastifyPluginAsyncTypebox = async (fastify) => {
})
}
)

fastify.get(
'/download/csv',
{
schema: {
response: {
200: { type: 'string', contentMediaType: 'application/gzip' },
400: Type.Object({ message: Type.String() })
},
tags: ['Tasks']
}
},
async function (request, reply) {
const queryStream = fastify.knex.select('*').from('tasks').stream()

const csvTransform = stringify({
header: true,
columns: undefined
})

reply.header('Content-Type', 'application/gzip')
reply.header(
'Content-Disposition',
`attachment; filename="${encodeURIComponent('tasks.csv.gz')}"`
)

return queryStream.pipe(csvTransform).pipe(createGzip())
}
)
}

function isErrnoException (error: unknown): error is NodeJS.ErrnoException {
Expand Down
57 changes: 55 additions & 2 deletions test/routes/api/tasks/tasks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { pipeline } from 'node:stream/promises'
import path from 'node:path'
import FormData from 'form-data'
import os from 'os'
import { gunzipSync } from 'node:zlib'

async function createUser (
app: FastifyInstance,
Expand All @@ -28,8 +29,16 @@ async function createTask (app: FastifyInstance, taskData: Partial<Task>) {
return id
}

async function uploadImageForTask (app: FastifyInstance, taskId: number, filePath: string, uploadDir: string) {
await app.knex<Task>('tasks').where({ id: taskId }).update({ filename: `${taskId}_short-logo.png` })
async function uploadImageForTask (
app: FastifyInstance,
taskId: number,
filePath: string,
uploadDir: string
) {
await app
.knex<Task>('tasks')
.where({ id: taskId })
.update({ filename: `${taskId}_short-logo.png` })

const file = fs.createReadStream(filePath)
const filename = `${taskId}_short-logo.png`
Expand Down Expand Up @@ -803,4 +812,48 @@ describe('Tasks api (logged user only)', () => {
})
})
})

describe('GET /api/tasks/download/csv', () => {
before(async () => {
const app = await build()
await app.knex('tasks').del()
await app.close()
})

it('should stream a gzipped CSV file', async (t) => {
const app = await build(t)

const tasks = []
for (let i = 0; i < 1000; i++) {
tasks.push({
name: `Task ${i + 1}`,
author_id: 1,
assigned_user_id: 2,
filename: 'task.png',
status: TaskStatusEnum.InProgress
})
}

await app.knex('tasks').insert(tasks)

const res = await app.injectWithLogin('basic', {
method: 'GET',
url: '/api/tasks/download/csv'
})

assert.strictEqual(res.statusCode, 200)
assert.strictEqual(res.headers['content-type'], 'application/gzip')
assert.strictEqual(
res.headers['content-disposition'],
'attachment; filename="tasks.csv.gz"'
)

const decompressed = gunzipSync(res.rawPayload).toString('utf-8')
const lines = decompressed.split('\n')
assert.equal(lines.length - 1, 1001)

assert.ok(lines[1].includes('Task 1,1,2,task.png,in-progress'))
assert.equal(lines[0], 'id,name,author_id,assigned_user_id,filename,status,created_at,updated_at')
})
})
})
Loading