Skip to content

Commit

Permalink
Merge pull request #3220 from huridocs/hotfix_3219_utf8_filenames
Browse files Browse the repository at this point in the history
[HOTFIX] support download files with utf8 names
  • Loading branch information
mfacar authored Sep 28, 2020
2 parents 47898ce + de31b4e commit 817d796
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
5 changes: 4 additions & 1 deletion app/api/files/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ export default (app: Application) => {
}

if (file.originalname) {
res.setHeader('Content-Disposition', `filename=${file.originalname}`);
res.setHeader(
'Content-Disposition',
`filename*=UTF-8''${encodeURIComponent(file.originalname)}`
);
}

res.sendFile(uploadsPath(filename));
Expand Down
36 changes: 33 additions & 3 deletions app/api/files/specs/downloadRoute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import { Application, Request, Response, NextFunction } from 'express';
import { setUpApp } from 'api/utils/testingRoutes';
import db from 'api/utils/testing_db';

import { fixtures, fileName1 } from './fixtures';
import { fixtures, fileName1, uploadId } from './fixtures';

import uploadRoutes from '../routes';
import { files } from '../files';

jest.mock(
'../../auth/authMiddleware.ts',
Expand All @@ -26,12 +27,41 @@ describe('files routes download', () => {

describe('GET/', () => {
it('should send the file', async () => {
const response: SuperTestResponse = await request(app).get(`/api/files/${fileName1}`);
const response: SuperTestResponse = await request(app)
.get(`/api/files/${fileName1}`)
.expect(200);

expect(response.header['content-disposition']).toBe('filename=upload1');
expect(response.body instanceof Buffer).toBe(true);
});

it('should set the original filename as content-disposition header', async () => {
const response: SuperTestResponse = await request(app)
.get(`/api/files/${fileName1}`)
.expect(200);

expect(response.get('Content-Disposition')).toBe("filename*=UTF-8''upload1");
});

it('should properly uri encode original names', async () => {
await files.save({ _id: uploadId, originalname: '테스트 한글chinese-file' });

const response: SuperTestResponse = await request(app)
.get(`/api/files/${fileName1}`)
.expect(200);

expect(response.get('Content-Disposition')).toBe(
`filename*=UTF-8''${encodeURIComponent('테스트 한글chinese-file')}`
);
});

it('should not set content-disposition header when the file does not have an original name', async () => {
const response: SuperTestResponse = await request(app)
.get('/api/files/fileNotInDisk')
.expect(404);

expect(response.get('Content-Disposition')).toBeUndefined();
});

describe('when file entry does not exist', () => {
it('should respond with 404', async () => {
const response = await request(app)
Expand Down
12 changes: 1 addition & 11 deletions app/api/files/specs/routes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { setUpApp } from 'api/utils/testingRoutes';
import connections from 'api/relationships';

import { FileType } from 'shared/types/fileType';
import { fileName1, fixtures, uploadId, uploadId2 } from './fixtures';
import { fixtures, uploadId, uploadId2 } from './fixtures';
import { files } from '../files';
import uploadRoutes from '../routes';

Expand Down Expand Up @@ -62,16 +62,6 @@ describe('files routes', () => {
'upload2',
]);
});

it('should set the original filename as content-disposition header', async () => {
const response: SuperTestResponse = await request(app).get(`/api/files/${fileName1}`);
expect(response.get('Content-Disposition')).toBe('filename=upload1');
});

it('should not set content-disposition header when the file does not have an original name', async () => {
const response: SuperTestResponse = await request(app).get('/api/files/fileNotInDisk');
expect(response.get('Content-Disposition')).toBeUndefined();
});
});

describe('DELETE/api/files', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uwazi",
"version": "1.10.0",
"version": "1.10.1",
"description": "Uwazi is a free, open-source solution for organising, analysing and publishing your documents.",
"keywords": [
"react"
Expand Down

0 comments on commit 817d796

Please sign in to comment.