-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #436 from BinaryStudioAcademy/task/OV-423-generate…
…-preview-with-bg OV-423: Generate preview with background
- Loading branch information
Showing
13 changed files
with
575 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,13 +1,17 @@ | ||
import { logger } from '~/common/logger/logger.js'; | ||
import { remotionService } from '~/common/services/services.js'; | ||
import { imageService, remotionService } from '~/common/services/services.js'; | ||
|
||
import { VideoController } from './video.controller.js'; | ||
import { VideoModel } from './video.model.js'; | ||
import { VideoRepository } from './video.repository.js'; | ||
import { VideoService } from './video.service.js'; | ||
|
||
const videoRepository = new VideoRepository(VideoModel); | ||
const videoService = new VideoService(videoRepository, remotionService); | ||
const videoRepository = new VideoRepository(VideoModel, imageService); | ||
const videoService = new VideoService( | ||
videoRepository, | ||
remotionService, | ||
imageService, | ||
); | ||
const videoController = new VideoController(logger, videoService); | ||
|
||
export { videoController, videoService }; |
This file contains 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,4 @@ | ||
const PREVIEW_WIDTH = 1920; | ||
const PREVIEW_HEIGHT = 1080; | ||
|
||
export { PREVIEW_HEIGHT, PREVIEW_WIDTH }; |
This file contains 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,25 @@ | ||
import { type Http, ContentType, HTTPMethod } from 'shared'; | ||
|
||
import { BaseHttpApi } from '~/common/http/base-http-api.js'; | ||
|
||
type Constructor = { | ||
http: Http; | ||
}; | ||
|
||
class ImageApi extends BaseHttpApi { | ||
public constructor({ http }: Constructor) { | ||
super({ path: '', baseUrl: '', http }); | ||
} | ||
|
||
public async getImageBuffer(url: string): Promise<Buffer> { | ||
const response = await this.load(url, { | ||
method: HTTPMethod.GET, | ||
contentType: ContentType.IMAGE, | ||
}); | ||
|
||
const arrayBuffer = await response.arrayBuffer(); | ||
return Buffer.from(arrayBuffer); | ||
} | ||
} | ||
|
||
export { ImageApi }; |
This file contains 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,109 @@ | ||
import sharp from 'sharp'; | ||
|
||
import { type FileService } from '~/common/services/file/file.service.js'; | ||
|
||
import { PREVIEW_HEIGHT, PREVIEW_WIDTH } from './constants/constants.js'; | ||
import { type ImageApi } from './image-base.js'; | ||
import { type Scene } from './types/types.js'; | ||
|
||
type Constructor = { | ||
fileService: FileService; | ||
imageApi: ImageApi; | ||
}; | ||
|
||
class ImageService { | ||
private fileService: FileService; | ||
private imageApi: ImageApi; | ||
|
||
public constructor({ fileService, imageApi }: Constructor) { | ||
this.fileService = fileService; | ||
this.imageApi = imageApi; | ||
} | ||
|
||
public async generatePreview(scene: Scene): Promise<string> { | ||
const avatarImage = scene.avatar?.url ?? ''; | ||
const background = scene.background; | ||
|
||
if (background?.url) { | ||
const backgroundImageBuffer = await this.imageApi.getImageBuffer( | ||
background.url, | ||
); | ||
|
||
return await this.combineAvatarWithBackground( | ||
avatarImage, | ||
backgroundImageBuffer, | ||
); | ||
} | ||
|
||
if (background?.color) { | ||
const backgroundColorImageBuffer = | ||
await this.createImageWithBackgroundColor(background.color); | ||
|
||
return await this.combineAvatarWithBackground( | ||
avatarImage, | ||
backgroundColorImageBuffer, | ||
); | ||
} | ||
|
||
return avatarImage; | ||
} | ||
|
||
private async combineAvatarWithBackground( | ||
avatarImage: string, | ||
background: Buffer, | ||
): Promise<string> { | ||
const avatarImageBuffer = | ||
await this.imageApi.getImageBuffer(avatarImage); | ||
|
||
const previewBuffer = await this.composeImages( | ||
avatarImageBuffer, | ||
background, | ||
); | ||
|
||
const fileName = `preview_${Date.now()}.jpg`; | ||
|
||
await this.fileService.uploadFile(previewBuffer, fileName); | ||
|
||
return this.fileService.getCloudFrontFileUrl(fileName); | ||
} | ||
|
||
private async composeImages( | ||
avatar: Buffer, | ||
background: Buffer, | ||
): Promise<Buffer> { | ||
const resizedBackground = await sharp(background) | ||
.resize(PREVIEW_WIDTH, PREVIEW_HEIGHT, { | ||
fit: 'cover', | ||
position: 'center', | ||
}) | ||
.toBuffer(); | ||
|
||
const resizedAvatar = await sharp(avatar) | ||
.resize(PREVIEW_WIDTH, PREVIEW_HEIGHT, { | ||
fit: 'inside', | ||
position: 'bottom', | ||
}) | ||
.toBuffer(); | ||
|
||
return await sharp(resizedBackground) | ||
.composite([{ input: resizedAvatar, blend: 'over' }]) | ||
.toBuffer(); | ||
} | ||
|
||
private async createImageWithBackgroundColor( | ||
backgroundColor: string, | ||
): Promise<Buffer> { | ||
return await sharp({ | ||
create: { | ||
width: PREVIEW_WIDTH, | ||
height: PREVIEW_HEIGHT, | ||
channels: 3, | ||
background: backgroundColor, | ||
}, | ||
}) | ||
.toFormat('png') | ||
.toBuffer(); | ||
} | ||
} | ||
|
||
export { ImageService }; |
This file contains 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,7 @@ | ||
import { baseHttp } from '~/common/http/http.js'; | ||
|
||
import { ImageApi } from './image-base.js'; | ||
|
||
const imageApi = new ImageApi({ http: baseHttp }); | ||
|
||
export { imageApi }; |
This file contains 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 @@ | ||
export { type Scene } from 'shared'; |
This file contains 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.