-
Notifications
You must be signed in to change notification settings - Fork 18
fix: serve image bytes from the API for bulk download #369
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
Merged
nGervasyuk
merged 3 commits into
Visual-Regression-Tracker:master
from
nGervasyuk:fix/download-images-cors
Aug 23, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import { HddService } from './hdd.service'; | ||
|
|
||
| describe('HddService', () => { | ||
| const service = new HddService(); | ||
|
|
||
| it('resolves a plain image name inside the image directory', () => { | ||
| expect(service.getImagePath('ABC123.screenshot.png')).toMatch(/imageUploads\/ABC123\.screenshot\.png$/); | ||
| }); | ||
|
|
||
| // a name may begin with dots without climbing out of the directory | ||
| it.each(['..thumbnail.png', '...png', '.hidden.png'])( | ||
| 'resolves a dotted name inside the directory: %s', | ||
| (imageName) => { | ||
| expect(service.getImagePath(imageName)).toContain('imageUploads'); | ||
| expect(() => service.getImagePath(imageName)).not.toThrow(); | ||
| } | ||
| ); | ||
|
|
||
| it.each(['../../etc/passwd', '../outside.png', '/etc/passwd', 'nested/../../outside.png', '..', '.'])( | ||
| 'rejects an image name pointing outside the image directory: %s', | ||
| (imageName) => { | ||
| expect(() => service.getImagePath(imageName)).toThrow(/outside of the image directory/); | ||
| } | ||
| ); | ||
|
|
||
| it('returns null for a missing image', async () => { | ||
| await expect(service.getImageBuffer('definitely-missing.png')).resolves.toBeNull(); | ||
| }); | ||
|
|
||
| it('propagates a traversal attempt instead of reading the file', async () => { | ||
| await expect(service.getImageBuffer('../../etc/passwd')).rejects.toThrow(/outside of the image directory/); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { Test, TestingModule } from '@nestjs/testing'; | ||
| import { BadRequestException, NotFoundException } from '@nestjs/common'; | ||
| import { Response } from 'express'; | ||
| import { StaticController } from './static.controller'; | ||
| import { StaticService } from './static.service'; | ||
|
|
||
| const initController = async (getImageBufferMock = jest.fn()) => { | ||
| const module: TestingModule = await Test.createTestingModule({ | ||
| controllers: [StaticController], | ||
| providers: [ | ||
| { | ||
| provide: StaticService, | ||
| useValue: { getImageBuffer: getImageBufferMock, getImageUrl: jest.fn() }, | ||
| }, | ||
| ], | ||
| }).compile(); | ||
|
|
||
| return module.get<StaticController>(StaticController); | ||
| }; | ||
|
|
||
| const responseMock = () => { | ||
| const res = { set: jest.fn(), send: jest.fn() }; | ||
| return res as unknown as Response & { set: jest.Mock; send: jest.Mock }; | ||
| }; | ||
|
|
||
| describe('download', () => { | ||
| it('answers with the image bytes', async () => { | ||
| const imageBuffer = Buffer.from([1, 2, 3]); | ||
| const getImageBufferMock = jest.fn().mockResolvedValueOnce(imageBuffer); | ||
| const controller = await initController(getImageBufferMock); | ||
| const res = responseMock(); | ||
|
|
||
| await controller.download('image.png', res); | ||
|
|
||
| expect(getImageBufferMock).toHaveBeenCalledWith('image.png'); | ||
| expect(res.set).toHaveBeenCalledWith({ | ||
| 'Content-Type': 'image/png', | ||
| 'Content-Disposition': 'attachment; filename="image.png"', | ||
| }); | ||
| expect(res.send).toHaveBeenCalledWith(imageBuffer); | ||
| }); | ||
|
|
||
| it('answers 404 for an image that is not there', async () => { | ||
| const getImageBufferMock = jest.fn().mockResolvedValueOnce(null); | ||
| const controller = await initController(getImageBufferMock); | ||
|
|
||
| await expect(controller.download('missing.png', responseMock())).rejects.toThrow(NotFoundException); | ||
| }); | ||
|
|
||
| // a name carrying a path could otherwise read any file the process can see | ||
| it.each(['../../etc/passwd', '..', '.', '', 'nested/image.png', '/etc/passwd'])( | ||
| 'rejects %p without touching storage', | ||
| async (fileName) => { | ||
| const getImageBufferMock = jest.fn(); | ||
| const controller = await initController(getImageBufferMock); | ||
|
|
||
| await expect(controller.download(fileName, responseMock())).rejects.toThrow(BadRequestException); | ||
| expect(getImageBufferMock).not.toHaveBeenCalled(); | ||
| } | ||
| ); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.