import { OpenLinkAnonProfile } from '../openlink' ; import { AsyncCommandResult , KnownDataStatusCode } from '../request' ; import { OpenProfilePostUploadStruct , OpenProfileUploadStruct } from './struct' ; import { createWebClient , TextWebRequest , WebClient } from './web-client' ; /**
 * OpenLink profile, link post upload api
 */ export class OpenUploadApiClient { private _client : TextWebRequest ; constructor ( client ) { this . _client = new TextWebRequest ( client ) ; } param { string } name param { Uint8Array } file param { string } contentType return { AsyncCommandResult < OpenProfileUploadStruct > } async uploadLinkImage ( name , file , contentType = 'image/jpeg' ) { const res = await this . _client . requestText ( 'POST' , 'up/open-chat-profile' , { 'file_1' : { value : file , options : { filename : name , contentType : contentType } } } ) ; try { const json = JSON . parse ( res ) as OpenProfileUploadStruct ; return { status : KnownDataStatusCode . SUCCESS , success : true , result : json } ; } catch ( e ) { return { status : KnownDataStatusCode . OPERATION_DENIED , success : false } ; } } param { string } name param { Uint8Array } file param { string } contentType return { AsyncCommandResult < OpenProfileUploadStruct > } async uploadLinkPostImage ( name , file , contentType = 'image/jpeg' ) { const res = await this . _client . requestText ( 'POST' , 'up/open-chat-profile-post' , { 'file_1' : { value : file , options : { filename : name , contentType : contentType } } } ) ; try { const json = JSON . parse ( res ) as OpenProfilePostUploadStruct ; return { status : KnownDataStatusCode . SUCCESS , success : true , result : json } ; } catch ( e ) { return { status : KnownDataStatusCode . OPERATION_DENIED , success : false } ; } } /**
     * Create default OpenUploadApiClient.
     */ static async create ( ) { return new OpenUploadApiClient ( await createWebClient ( 'https' , 'up-api1-kage.kakao.com' ) ) ; } } export namespace OpenUploadAPI { let client = null ; export function getOriginalLinkImageURL ( accessKey ) { return `http://open.kakaocdn.net/dn/${ accessKey }/img.jpg` ; } export function getSmallLinkImageURL ( accessKey ) { return `http://open.kakaocdn.net/dn/${ accessKey }/img_s.jpg` ; } export function getLargeLinkImageURL ( accessKey ) { return `http://open.kakaocdn.net/dn/${ accessKey }/img_l.jpg` ; } param { string } nickname param { Uint8Array } profile return { AsyncCommandResult < OpenLinkAnonProfile > } export async function buildProfile ( nickname , profile ) { if ( ! client ) client = await OpenUploadApiClient . create ( ) ; const res = await client . uploadLinkImage ( 'profile.png' , profile ) ; if ( ! res . success ) return res ; return { status : KnownDataStatusCode . SUCCESS , success : true , result : { nickname , profilePath : res . result . access_key } } ; } }