forked from internxt/drive-desktop
-
Notifications
You must be signed in to change notification settings - Fork 4
Fix/pb 5990 implement thumbnail generation #266
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
Open
AlexisMora
wants to merge
7
commits into
main
Choose a base branch
from
fix/pb-5990-implement-thumbnail-generation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
567347f
Fix: thumbnail generation and upload
AlexisMora 1f150db
fix: format
AlexisMora 97e59a9
fix: proper extension for thumbnails since we are using nativeImage
AlexisMora 9d3d354
feat: add thumbanils on already existing files
AlexisMora 391c00b
feat: add necessary tests
AlexisMora d5cf969
fix: format
AlexisMora ca3ae00
chore: remove unnecesary imports
AlexisMora 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
54 changes: 54 additions & 0 deletions
54
src/backend/features/thumbnails/generate-thumbnail.test.ts
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,54 @@ | ||
| import * as nativeImageModule from 'electron'; | ||
| import { generateThumbnail } from './generate-thumbnail'; | ||
| import { partialSpyOn } from 'tests/vitest/utils.helper'; | ||
| import { THUMBNAIL_SIZE } from './thumbnail.constants'; | ||
|
|
||
| describe('generate-thumbnail', () => { | ||
| const createFromBufferMock = partialSpyOn(nativeImageModule.nativeImage, 'createFromBuffer'); | ||
|
|
||
| it('returns error when nativeImage cannot decode the buffer', () => { | ||
| createFromBufferMock.mockReturnValue({ isEmpty: () => true }); | ||
| const { error } = generateThumbnail(Buffer.from('not-an-image')); | ||
| expect(error).toBeInstanceOf(Error); | ||
| }); | ||
|
|
||
| it('resizes image larger than THUMBNAIL_SIZE', () => { | ||
| const imageWidth = 1024; | ||
| const imageHeight = 768; | ||
| const pngBuffer = Buffer.from('png'); | ||
| const resizeMock = vi.fn().mockReturnValue({ toPNG: () => pngBuffer }); | ||
|
|
||
| createFromBufferMock.mockReturnValue({ | ||
| isEmpty: () => false, | ||
| getSize: () => ({ width: imageWidth, height: imageHeight }), | ||
| resize: resizeMock, | ||
| }); | ||
|
|
||
| const { data, error } = generateThumbnail(Buffer.from('large-image')); | ||
|
|
||
| expect(error).toBeUndefined(); | ||
| const scale = Math.min(THUMBNAIL_SIZE / imageWidth, THUMBNAIL_SIZE / imageHeight, 1); | ||
| expect(resizeMock).toBeCalledWith({ | ||
| width: Math.round(imageWidth * scale), | ||
| height: Math.round(imageHeight * scale), | ||
| quality: 'good', | ||
| }); | ||
| expect(data).toBe(pngBuffer); | ||
| }); | ||
|
|
||
| it('does not upscale image smaller than THUMBNAIL_SIZE', () => { | ||
| const imageWidth = 100; | ||
| const imageHeight = 80; | ||
| const pngBuffer = Buffer.from('png'); | ||
| const resizeMock = vi.fn().mockReturnValue({ toPNG: () => pngBuffer }); | ||
|
|
||
| createFromBufferMock.mockReturnValue({ | ||
| isEmpty: () => false, | ||
| getSize: () => ({ width: imageWidth, height: imageHeight }), | ||
| resize: resizeMock, | ||
| }); | ||
| generateThumbnail(Buffer.from('small-image')); | ||
|
|
||
| expect(resizeMock).toBeCalledWith({ width: imageWidth, height: imageHeight, quality: 'good' }); | ||
| }); | ||
| }); |
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,20 @@ | ||
| import { nativeImage } from 'electron'; | ||
| import { Result } from '../../../context/shared/domain/Result'; | ||
| import { THUMBNAIL_SIZE } from './thumbnail.constants'; | ||
|
|
||
| export function generateThumbnail(fileBuffer: Buffer): Result<Buffer, Error> { | ||
| const image = nativeImage.createFromBuffer(fileBuffer); | ||
|
|
||
| if (image.isEmpty()) { | ||
| return { error: new Error('Failed to load image from buffer') }; | ||
| } | ||
|
|
||
| const { width, height } = image.getSize(); | ||
| const scale = Math.min(THUMBNAIL_SIZE / width, THUMBNAIL_SIZE / height, 1); | ||
| const newWidth = Math.round(width * scale); | ||
| const newHeight = Math.round(height * scale); | ||
|
|
||
| const resized = image.resize({ width: newWidth, height: newHeight, quality: 'good' }); | ||
|
|
||
| return { data: resized.toPNG() }; | ||
| } |
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,2 @@ | ||
| export const THUMBNAIL_SIZE = 256; | ||
| export const UPLOAD_TIMEOUT_MS = 30_000; |
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,11 @@ | ||
| /** | ||
| * v2.5.4 | ||
| * Alexis Mora | ||
| * As per Electron docs, only PNG and JPEG are officially supported | ||
| * https://www.electronjs.org/docs/latest/api/native-image#supported-formats | ||
| */ | ||
| export const THUMBNAIL_SUPPORTED_EXTENSIONS = new Set(['jpg', 'jpeg', 'png']); | ||
|
|
||
| export function canGenerateThumbnail(extension: string): boolean { | ||
| return THUMBNAIL_SUPPORTED_EXTENSIONS.has(extension.toLowerCase()); | ||
| } |
60 changes: 60 additions & 0 deletions
60
src/backend/features/thumbnails/upload-and-create-thumbnail.test.ts
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,60 @@ | ||
| import * as uploadThumbnailToBucketModule from './upload-thumbnail-to-bucket'; | ||
| import * as createThumbnailModule from '../../../infra/drive-server/services/files/services/create-thumbnail'; | ||
| import { uploadAndCreateThumbnail } from './upload-and-create-thumbnail'; | ||
| import { call, partialSpyOn } from 'tests/vitest/utils.helper'; | ||
| import { THUMBNAIL_SIZE } from './thumbnail.constants'; | ||
| import { DriveServerError } from '../../../infra/drive-server/drive-server.error'; | ||
| import { Environment } from '@internxt/inxt-js'; | ||
|
|
||
| describe('upload-and-create-thumbnail', () => { | ||
| const uploadThumbnailToBucketMock = partialSpyOn(uploadThumbnailToBucketModule, 'uploadThumbnailToBucket'); | ||
| const createThumbnailMock = partialSpyOn(createThumbnailModule, 'createThumbnail'); | ||
|
|
||
| const environment = {} as Environment; | ||
| const bucket = 'test-bucket'; | ||
| const fileUuid = 'file-uuid'; | ||
| const thumbnailBuffer = Buffer.from('thumbnail-data'); | ||
|
|
||
| it('should return error when upload to bucket fails', async () => { | ||
| const uploadError = new Error('Upload failed'); | ||
| uploadThumbnailToBucketMock.mockResolvedValue({ error: uploadError }); | ||
|
|
||
| const { error } = await uploadAndCreateThumbnail({ thumbnailBuffer, fileUuid, environment, bucket }); | ||
|
|
||
| expect(error).toBe(uploadError); | ||
| expect(createThumbnailMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should call createThumbnail with correct params after successful bucket upload', async () => { | ||
| const contentsId = 'contents-id-123'; | ||
| const thumbnailDto = { id: 1, type: 'png' }; | ||
|
|
||
| uploadThumbnailToBucketMock.mockResolvedValue({ data: contentsId }); | ||
| createThumbnailMock.mockResolvedValue({ data: thumbnailDto }); | ||
|
|
||
| const { data, error } = await uploadAndCreateThumbnail({ thumbnailBuffer, fileUuid, environment, bucket }); | ||
|
|
||
| expect(error).toBeUndefined(); | ||
| expect(data).toStrictEqual(thumbnailDto); | ||
| call(createThumbnailMock).toStrictEqual({ | ||
| fileUuid, | ||
| type: 'png', | ||
| size: thumbnailBuffer.length, | ||
| maxWidth: THUMBNAIL_SIZE, | ||
| maxHeight: THUMBNAIL_SIZE, | ||
| bucketId: bucket, | ||
| bucketFile: contentsId, | ||
| encryptVersion: '03-aes', | ||
| }); | ||
| }); | ||
|
|
||
| it('should return error when createThumbnail fails', async () => { | ||
| const createError = new DriveServerError('SERVER_ERROR'); | ||
| uploadThumbnailToBucketMock.mockResolvedValue({ data: 'contents-id' }); | ||
| createThumbnailMock.mockResolvedValue({ error: createError }); | ||
|
|
||
| const { error } = await uploadAndCreateThumbnail({ thumbnailBuffer, fileUuid, environment, bucket }); | ||
|
|
||
| expect(error).toBe(createError); | ||
| }); | ||
| }); |
37 changes: 37 additions & 0 deletions
37
src/backend/features/thumbnails/upload-and-create-thumbnail.ts
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,37 @@ | ||
| import { Environment } from '@internxt/inxt-js'; | ||
| import { Result } from '../../../context/shared/domain/Result'; | ||
| import { ThumbnailDto } from '../../../infra/drive-server/out/dto'; | ||
| import { createThumbnail } from '../../../infra/drive-server/services/files/services/create-thumbnail'; | ||
| import { THUMBNAIL_SIZE } from './thumbnail.constants'; | ||
| import { uploadThumbnailToBucket } from './upload-thumbnail-to-bucket'; | ||
|
|
||
| type Props = { | ||
| thumbnailBuffer: Buffer; | ||
| fileUuid: string; | ||
| environment: Environment; | ||
| bucket: string; | ||
| }; | ||
|
|
||
| export async function uploadAndCreateThumbnail({ | ||
| thumbnailBuffer, | ||
| fileUuid, | ||
| environment, | ||
| bucket, | ||
| }: Props): Promise<Result<ThumbnailDto, Error>> { | ||
| const uploaded = await uploadThumbnailToBucket(environment, bucket, thumbnailBuffer); | ||
|
|
||
| if (uploaded.error) { | ||
| return { error: uploaded.error }; | ||
| } | ||
|
|
||
| return createThumbnail({ | ||
| fileUuid, | ||
| type: 'png', | ||
| size: thumbnailBuffer.length, | ||
| maxWidth: THUMBNAIL_SIZE, | ||
| maxHeight: THUMBNAIL_SIZE, | ||
| bucketId: bucket, | ||
| bucketFile: uploaded.data, | ||
| encryptVersion: '03-aes', | ||
| }); | ||
| } |
67 changes: 67 additions & 0 deletions
67
src/backend/features/thumbnails/upload-thumbnail-to-bucket.test.ts
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,67 @@ | ||
| import { Environment } from '@internxt/inxt-js'; | ||
| import { uploadThumbnailToBucket } from './upload-thumbnail-to-bucket'; | ||
| import { UPLOAD_TIMEOUT_MS } from './thumbnail.constants'; | ||
| import { UploadOptions } from '@internxt/inxt-js/build/lib/core'; | ||
|
|
||
| function environmentMock(upload: (bucket: string, options: UploadOptions) => void): Environment { | ||
| return { upload } as unknown as Environment; | ||
| } | ||
|
|
||
| describe('upload-thumbnail-to-bucket', () => { | ||
| const bucket = 'test-bucket'; | ||
| const buffer = Buffer.from('image-data'); | ||
| const clearTimeoutMock = vi.spyOn(global, 'clearTimeout'); | ||
|
|
||
| it('should return data with contentsId on successful upload', async () => { | ||
| const contentsId = 'contents-id-123'; | ||
| const environment = environmentMock((_bucket, { finishedCallback }) => { | ||
| finishedCallback(null, contentsId); | ||
| }); | ||
|
|
||
| const { data, error } = await uploadThumbnailToBucket(environment, bucket, buffer); | ||
|
|
||
| expect(error).toBeUndefined(); | ||
| expect(data).toBe(contentsId); | ||
| expect(clearTimeoutMock).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return error when finishedCallback receives an error', async () => { | ||
| const uploadError = new Error('bucket error'); | ||
| const environment = environmentMock((_bucket, { finishedCallback }) => { | ||
| finishedCallback(uploadError, null); | ||
| }); | ||
|
|
||
| const { error } = await uploadThumbnailToBucket(environment, bucket, buffer); | ||
|
|
||
| expect(error).toBe(uploadError); | ||
| expect(clearTimeoutMock).toHaveBeenCalled(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use toBeCalled instead of toHaveBeenCalled |
||
| }); | ||
|
|
||
| it('should return error when finishedCallback has no contentsId', async () => { | ||
| const environment = environmentMock((_bucket, { finishedCallback }) => { | ||
| finishedCallback(null, null); | ||
| }); | ||
|
|
||
| const { error } = await uploadThumbnailToBucket(environment, bucket, buffer); | ||
|
|
||
| expect(error).toBeInstanceOf(Error); | ||
| expect(clearTimeoutMock).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should return error when upload times out', async () => { | ||
| vi.useFakeTimers(); | ||
|
|
||
| const environment = environmentMock(() => { | ||
| // never calls finishedCallback | ||
| }); | ||
|
|
||
| const resultPromise = uploadThumbnailToBucket(environment, bucket, buffer); | ||
| vi.advanceTimersByTime(UPLOAD_TIMEOUT_MS); | ||
|
|
||
| const { error } = await resultPromise; | ||
|
|
||
| expect(error).toBeInstanceOf(Error); | ||
|
|
||
| vi.useRealTimers(); | ||
| }); | ||
| }); | ||
36 changes: 36 additions & 0 deletions
36
src/backend/features/thumbnails/upload-thumbnail-to-bucket.ts
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,36 @@ | ||
| import { Environment } from '@internxt/inxt-js'; | ||
| import { Readable } from 'node:stream'; | ||
| import { Result } from '../../../context/shared/domain/Result'; | ||
| import { UPLOAD_TIMEOUT_MS } from './thumbnail.constants'; | ||
|
|
||
| export function uploadThumbnailToBucket( | ||
| environment: Environment, | ||
| bucket: string, | ||
| buffer: Buffer, | ||
| ): Promise<Result<string, Error>> { | ||
| let timeoutId: ReturnType<typeof setTimeout>; | ||
|
|
||
| const upload = new Promise<Result<string, Error>>((resolve) => { | ||
| environment.upload(bucket, { | ||
| source: Readable.from(buffer), | ||
| fileSize: buffer.length, | ||
| finishedCallback: (err: Error | null, contentsId: string | null) => { | ||
| clearTimeout(timeoutId); | ||
| if (err) { | ||
| return resolve({ error: err }); | ||
| } | ||
| if (!contentsId) { | ||
| return resolve({ error: new Error('Upload finished but no contentsId returned') }); | ||
| } | ||
| resolve({ data: contentsId }); | ||
| }, | ||
| progressCallback: () => {}, | ||
| }); | ||
| }); | ||
|
|
||
| const timeout = new Promise<Result<string, Error>>((resolve) => { | ||
| timeoutId = setTimeout(() => resolve({ error: new Error('Thumbnail bucket upload timed out') }), UPLOAD_TIMEOUT_MS); | ||
| }); | ||
|
|
||
| return Promise.race([upload, timeout]); | ||
| } |
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use partialSpyOn instead of vi.spyOn