Skip to content

Commit 94e1822

Browse files
committed
Use the .csv extension if the MIME type is csv
1 parent d5a603c commit 94e1822

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/SIL.XForge.Scripture/ClientApp/src/xforge-common/file.service.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,29 @@ describe('FileService', () => {
249249
expect(saveAs).toHaveBeenCalled();
250250
env.httpMock.verify();
251251
}));
252+
253+
it('detects csv MIME type and adds the file extension', fakeAsync(() => {
254+
const env = new TestEnvironment();
255+
const filename: string = 'training-data.xlsx';
256+
const source: string = '/path/to/training-data.xlsx';
257+
258+
// Spy on the saveAs function
259+
const saveAsSpy = spyOn(saveAs, 'saveAs').and.stub();
260+
261+
env.service.onlineDownloadFile(FileType.TrainingData, source, filename);
262+
263+
// Verify the HTTP request was made with the correct URL
264+
const expectedUrl: string = formatFileSource(FileType.TrainingData, source);
265+
const req = env.httpMock.expectOne(expectedUrl);
266+
expect(req.request.method).toBe('GET');
267+
268+
// Respond with a blob
269+
const testBlob: Blob = new Blob([], { type: 'text/csv' });
270+
req.flush(testBlob);
271+
tick();
272+
expect(saveAsSpy).toHaveBeenCalledWith(testBlob, 'training-data.csv');
273+
env.httpMock.verify();
274+
}));
252275
});
253276

254277
interface ChildData {

src/SIL.XForge.Scripture/ClientApp/src/xforge-common/file.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,10 @@ export class FileService {
224224
const url: string = formatFileSource(fileType, source);
225225
void this.onlineRequestFile(url).then(blob => {
226226
if (blob != null) {
227+
if (blob.type === 'text/csv') {
228+
// use the .csv extension explicitly if the MIME type is csv
229+
filename = filename.split('.')[0] + '.csv';
230+
}
227231
saveAs(blob, filename);
228232
}
229233
});

0 commit comments

Comments
 (0)