import { existsSync , readFile , writeFile } from 'fs-extra' ; import { IStickerConfig , IStickerOptions } from './Types' ; import axios from 'axios' ; import Utils , { defaultBg } from './Utils' ; import { fromBuffer } from 'file-type' ; import convert from './internal/convert' ; import Exif from './internal/Metadata/Exif' ; import { StickerTypes } from './internal/Metadata/StickerTypes' ; import { Categories , extractMetadata } from '.' ; import { Color } from 'sharp' ; /**
 * Sticker class
 */ export class Sticker { param { string | Buffer } data param { IStickerOptions } options constructor ( private data , public metadata = { } ) { this . metadata . author = this . metadata . author ?? '' ; this . metadata . pack = this . metadata . pack ?? '' ; this . metadata . id = this . metadata . id ?? Utils . generateStickerID ( ) ; this . metadata . quality = this . metadata . quality ?? 100 ; this . metadata . type = Object . values ( StickerTypes ) . includes ( this . metadata . type as StickerTypes ) ? this . metadata . type : StickerTypes . DEFAULT ; this . metadata . background = this . metadata . background ?? defaultBg ; } private _parse = async ( ) : Promise < Buffer > => Buffer . isBuffer ( this . data ) ? this . data : 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 ) : Promise < string > => { const type = await fromBuffer ( data ) ; if ( ! type ) { if ( typeof this . data === 'string' ) return 'image/svg+xml' ; throw new Error ( 'Invalid file type' ) ; } return type . mime ; } ; returns { Promise < Buffer > } example public build = async ( ) : Promise < Buffer > => { const data = await this . _parse ( ) ; const mime = await this . _getMimeType ( data ) ; return new Exif ( this . metadata as IStickerConfig ) . add ( await convert ( data , mime , this . metadata ) ) ; } ; param { string } type returns { Promise < Buffer > } example public toBuffer = this . build ; public get defaultFilename ( ) : string { return `./${ this . metadata . pack }-${ this . metadata . author }.webp` ; } param filename returns example public toFile = async ( filename = this . defaultFilename ) : Promise < string > => { await writeFile ( filename , await this . build ( ) ) ; return filename ; } ; param pack returns { this } example public setPack = ( pack ) : this => { this . metadata . pack = pack ; return this ; } ; param author returns public setAuthor = ( author ) : this => { this . metadata . author = author ; return this ; } ; param id returns { this } example public setID = ( id ) : this => { this . metadata . id = id ; return this ; } ; param categories returns { this } example public setCategories = ( categories ) : this => { this . metadata . categories = categories ; return this ; } ; param { StickerTypes | string } type returns { this } public setType = ( type ) : this => { this . metadata . type = type ; return this ; } ; param { number } quality returns { this } public setQuality = ( quality ) : this => { this . metadata . quality = quality ; return this ; } ; param { Color } background returns { this } public setBackground = ( background ) : this => { this . metadata . background = background ; return this ; } ; deprecated public get = this . build ; returns { { sticker : Buffer } } example public toMessage = async ( ) : Promise < { sticker : Buffer ; } > => ( { sticker : await this . build ( ) } ) ; param { Buffer } image public static extractMetadata = extractMetadata ; } param { string | Buffer } data param { IStickerOptions } options returns { Promise < Buffer > } export const createSticker = async ( ... args ) : Promise < Buffer > => { return new Sticker ( ... args ) . build ( ) ; } ;