Skip to content

Commit

Permalink
fix: fix the bug when delete a file
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbubu committed Oct 23, 2020
1 parent 933be39 commit 58b746a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
Binary file added jacobbubu-rest-file-server-0.0.0.tgz
Binary file not shown.
16 changes: 10 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,21 @@ export class FileServer {

app.get(`/${_options.route}/:filename`, (request, response) => {
const result = readFile(request.params.filename)
if (result.status >= 200 && result.status < 300) {
response.writeHead(result.status, {
'Content-Type': 'application/octet-stream',
'Content-disposition': 'attachment;filename=' + result.filename ?? 'temp.dat',
})
response.end(result.data, 'binary')

response.writeHead(200, {
'Content-Type': 'application/octet-stream',
'Content-disposition': 'attachment;filename=' + result.filename ?? 'temp.dat',
})
response.end(result.data, 'binary')
return
}
response.status(result.status).send(result.data)
})

app.delete(`/${_options.route}/:filename`, (request, response) => {
const result = removeFile(request.params.filename)
response.status(result.status)
response.status(result.status).send({})
})

app.post(`/${_options.route}`, upload.single('file'), async (request, response) => {
Expand Down
11 changes: 9 additions & 2 deletions test/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ describe('basic', () => {
})

superagent.get(url + '/' + fileName).pipe(
concat((result) => {
concat(async (result) => {
const elapsed = Date.now() - startTime
expect(elapsed).toBeLessThan(500)
expect(result.toString('utf8')).toBe(expectedContent)
done()

await superagent.del(url + '/' + fileName)
try {
await superagent.get(url + '/' + fileName)
} catch (err) {
expect(err.status).toBe(404)
done()
}
})
)
})
Expand Down

0 comments on commit 58b746a

Please sign in to comment.