11import { Context , HTTPClient , makeFormData } from 'koajax' ;
22import { buildURLData , cache , Second } from 'web-utility' ;
33
4+ import { LarkWikiNode } from './module' ;
45import {
56 isLarkError ,
67 JSTicket ,
@@ -165,11 +166,17 @@ export class LarkApp implements LarkAppOption {
165166 async downloadFile ( id : string ) {
166167 await this . getAccessToken ( ) ;
167168
168- const { body } = await this . client . request < ArrayBuffer > ( {
169- path : `drive/v1/medias/${ id } /download` ,
170- responseType : 'arraybuffer'
171- } ) ;
172- return body ! ;
169+ const { headers, body } = await this . client . get < Blob > (
170+ `drive/v1/medias/${ id } /download` ,
171+ { } ,
172+ { responseType : 'blob' }
173+ ) ;
174+ const { 'Content-Disposition' : CD , 'Content-Type' : CT } = headers ;
175+
176+ const [ type ] = ( CT as string ) ?. split ( ';' ) || [ ] ,
177+ [ , fileName ] = ( CD as string ) ?. match ( / f i l e n a m e = " ? ( .* ?) " ? $ / ) || [ ] ;
178+
179+ return new File ( [ body ! ] , fileName , { type } ) ;
173180 }
174181
175182 /**
@@ -195,4 +202,41 @@ export class LarkApp implements LarkAppOption {
195202
196203 return body ! . data ! . file_token ;
197204 }
205+
206+ /**
207+ * @see {@link https://open.feishu.cn/document/server-docs/docs/wiki-v2/space-node/get_node }
208+ */
209+ async wiki2docx ( id : string ) {
210+ await this . getAccessToken ( ) ;
211+
212+ const { body } = await this . client . get <
213+ LarkData < { node : LarkWikiNode } >
214+ > ( `wiki/v2/spaces/get_node?token=${ id } ` ) ;
215+
216+ const { obj_type, obj_token } = body ! . data ! . node ;
217+
218+ return obj_type === 'docx' ? obj_token : '' ;
219+ }
220+
221+ static documentPathPattern = / ( w i k i | d o c x ) \/ ( \w + ) / ;
222+
223+ /**
224+ * @see {@link https://open.feishu.cn/document/ukTMukTMukTM/uUDN04SN0QjL1QDN/docs-v1/content/get }
225+ */
226+ async downloadMarkdown ( URI : string ) {
227+ await this . getAccessToken ( ) ;
228+
229+ const [ , type , id ] = URI . match ( LarkApp . documentPathPattern ) || [ ] ;
230+
231+ const doc_token = type === 'wiki' ? await this . wiki2docx ( id ) : id ;
232+
233+ const { body } = await this . client . get < LarkData < { content : string } > > (
234+ `docs/v1/content?${ new URLSearchParams ( {
235+ doc_type : 'docx' ,
236+ doc_token,
237+ content_type : 'markdown'
238+ } ) } `
239+ ) ;
240+ return body ! . data ! . content ;
241+ }
198242}
0 commit comments