Skip to content

Commit

Permalink
feat(convert-sharp): add option of resize (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
ayocodingit authored Sep 23, 2024
1 parent 7a0cc04 commit c0ef32f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/modules/files/entity/interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Resize } from '../../../pkg/sharp'

export interface RequestImage {
url: string
property: {
Expand All @@ -24,12 +26,12 @@ export interface RequestPdf {
seconds: number
}

export interface RequestConvertImage {
export type RequestConvertImage = {
url: string
seconds: number
quality: number
convertTo: 'webp' | 'jpeg'
}
} & Resize

export interface RequestReplaceDoc {
url: string
Expand Down
11 changes: 11 additions & 0 deletions src/modules/files/entity/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ export const RequestConvertImage = Joi.object({
seconds: Joi.number().default(seconds).optional(),
quality: Joi.number().min(1).max(100).optional().default(80),
convertTo: Joi.string().valid('jpeg', 'webp').optional().default('webp'),
resize: Joi.boolean().optional().default(false),
width: Joi.number().when('resize', {
is: true,
then: Joi.required(),
otherwise: Joi.optional(),
}),
height: Joi.number().when('resize', {
is: true,
then: Joi.required(),
otherwise: Joi.optional(),
}),
})

export const RequestReplaceDoc = Joi.object({
Expand Down
9 changes: 7 additions & 2 deletions src/modules/files/usecase/usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import mime from 'mime-types'
import axios from 'axios'
import error from '../../../pkg/error'
import statusCode from '../../../pkg/statusCode'
import Sharp from '../../../pkg/sharp'
import Sharp, { Resize } from '../../../pkg/sharp'
import {
RegexContentTypeDoc,
RegexContentTypeImage,
Expand Down Expand Up @@ -109,6 +109,9 @@ class Usecase {
url,
quality,
convertTo,
resize,
height,
width,
}: RequestConvertImage) {
try {
const { data, status, headers } = await axios.get(url, {
Expand All @@ -122,11 +125,13 @@ class Usecase {
statusCode.BAD_REQUEST,
statusCode[statusCode.BAD_REQUEST]
)
const optionResize: Resize = { resize, height, width }

const { source, meta } = await Sharp.Convert(
data,
convertTo,
quality
quality,
optionResize
)

return {
Expand Down
17 changes: 15 additions & 2 deletions src/pkg/sharp.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import sharp from 'sharp'

export type Resize = {
resize: boolean
height: number
width: number
}

class Sharp {
constructor() {}

Expand All @@ -14,9 +20,16 @@ class Sharp {
public static async Convert(
source: Buffer,
convertTo: 'webp' | 'jpeg',
quality: number
quality: number,
resize?: Resize
) {
const sharpImage = sharp(source)
let sharpImage = sharp(source)
if (resize?.resize)
sharpImage = sharpImage.resize({
height: resize.height,
width: resize.width,
fit: 'inside',
})
const { data, info } = await sharpImage[convertTo]({
quality,
}).toBuffer({ resolveWithObject: true })
Expand Down

0 comments on commit c0ef32f

Please sign in to comment.