Skip to content
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

feat: Convert buffer image to PNG format #17

Merged
merged 3 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
"@actions/github": "^5.1.1",
"dayjs": "^1.11.7",
"download": "^8.0.0",
"file-type": "^19.0.0",
"gray-matter": "^4.0.3",
"mkdirp": "^2.1.3",
"sharp": "^0.33.1",
"yaml": "^2.2.1"
},
"devDependencies": {
Expand Down
32 changes: 31 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import download from 'download'
import dayjs from 'dayjs'
import {extractImages} from './extract-images'
import {formatFrontMatterValue} from './format'
import sharp from 'sharp'
import {fileTypeFromBuffer} from 'file-type'

async function run(): Promise<void> {
const token: string = core.getInput('token')
Expand Down Expand Up @@ -103,11 +105,39 @@ async function run(): Promise<void> {
let bodyText = bodyWithoutFrontMatter
const images = extractImages(bodyText)
for (const image of images) {
const newImageFilename = path.basename(image.filename)
let newImageFilename = path.basename(image.filename)
fs.writeFileSync(
path.join(dirname, newImageFilename),
await download(image.filename)
)

const imagePath = path.join('./', newImageFilename)
const imageExt = path.extname(image.filename).toLocaleLowerCase()

if (imageExt === '') {
const buffer = fs.readFileSync(imagePath)
const imageType = await fileTypeFromBuffer(buffer)
sharp.cache(false)

if (
imageType !== undefined &&
sharp.format.hasOwnProperty(imageType?.ext)
) {
if (imageType.ext === 'gif') {
await sharp(`./${newImageFilename}`, {
limitInputPixels: false,
animated: true,
density: 1
}).toFile(`newImageFilename.${imageType.ext}`)
} else {
await sharp(`./${newImageFilename}`).toFile(
`newImageFilename.${imageType.ext}`
)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
}).toFile(`newImageFilename.${imageType.ext}`)
} else {
await sharp(`./${newImageFilename}`).toFile(
`newImageFilename.${imageType.ext}`
)
}).toFile(`${newImageFilename}.${imageType.ext}`)
} else {
await sharp(`./${newImageFilename}`).toFile(
`${newImageFilename}.${imageType.ext}`
)

이렇게 되는게 맞으려나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eunjae-lee
커밋만 하고 글을 작성하지 못했네요 ㅠㅠ
말씀하신대로 이미지 확장자를 제외한 다른 확장자를 고려하지 못해서 sharp 에서 변환이 가능한 확장자만 변환하도록 했습니다!

  1. 확장자가 존재하지 않는다면
  2. 버퍼 파일을 학인해 파일 종류를 확인합니다.
  3. sharp에서 변환이 가능하다면 변환을 진행합니다.
    3-1. gif 일 때는 옵션을 따로 설정합니다.
    limitInputPixels: false, //이미지 픽셀 설정 제한을 해제 
    animated: true, // animated 설정 
    density: 1 // 해상도설정, 기본값을 했을 때 오래 걸려 낮췄습니다. 

참고

  1. sharp.cache(false) : 파일 캐시가 존재할 때 원본 파일의 삭제를 막아서 캐시 설정을 해제했습니다.
    참고

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

설명 감사합니다! 그럼 마지막으로 제가 저 위에 suggestion 남긴 것만 확인 부탁드려요! newImageFilename 변수값이 제대로 사용되지 않은 거 같아서요.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

앗 그러네요 ! 수정하겠습니다!

}
newImageFilename += `.${imageType.ext}`
fs.unlinkSync(imagePath)
}
}
bodyText = bodyText.replace(
image.match,
`![${image.alt}](./${newImageFilename}${
Expand Down