Skip to content

Commit

Permalink
feat(): svg support. Fixes #25
Browse files Browse the repository at this point in the history
  • Loading branch information
AlenVelocity committed Nov 12, 2021
1 parent 015f2dc commit 76048c1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/Sticker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@ export class Sticker {
private _parse = async (): Promise<Buffer> =>
Buffer.isBuffer(this.data)
? this.data
: (async () =>
: this.data.trim().startsWith('<svg') ? Buffer.from(this.data) : (async () =>
existsSync(this.data)
? readFile(this.data)
: axios.get(this.data as string, { responseType: 'arraybuffer' }).then(({ data }) => data))()

private _getMimeType = async (data: Buffer): Promise<string> => {
const type = await fromBuffer(data)
if (!type) throw new Error('Invalid Buffer Instance')
if (!type) {
if (typeof this.data === 'string') return 'image/svg+xml'
throw new Error('Invalid file type')
}
return type.mime
}

Expand All @@ -52,10 +55,10 @@ export class Sticker {
*/
public build = async (
): Promise<Buffer> => {
const buffer = await this._parse()
const mime = await this._getMimeType(buffer)
const data = await this._parse()
const mime = await this._getMimeType(data)
return new Exif(this.metadata as IStickerConfig).add(
await convert(buffer, mime, this.metadata)
await convert(data, mime, this.metadata)
)
}

Expand Down
1 change: 1 addition & 0 deletions src/internal/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const convert = async (
const isVideo = mime.startsWith('video')
let image = isVideo ? await videoToGif(data) : data
const isAnimated = isVideo || mime.includes('gif')

if (isAnimated && type === 'crop') {
const filename = `${tmpdir()}/${Math.random().toString(36)}.webp`
await writeFile(filename, image)
Expand Down

0 comments on commit 76048c1

Please sign in to comment.