1- import { Context , HTTPClient , makeFormData } from 'koajax' ;
1+ import { Context , HTTPClient } from 'koajax' ;
22import { buildURLData , cache , sleep } from 'web-utility' ;
33
4- import { DocumentModel , WikiNodeModel } from './module' ;
4+ import {
5+ CopiedFile ,
6+ DocumentModel ,
7+ DriveModel ,
8+ UserIdType ,
9+ WikiNode ,
10+ WikiNodeModel
11+ } from './module' ;
512import {
613 isLarkError ,
714 JSTicket ,
815 LarkData ,
16+ LarkDocumentType ,
917 TenantAccessToken ,
1018 UploadTargetType ,
1119 UserMeta
@@ -34,6 +42,10 @@ export class LarkApp implements LarkAppOption {
3442 client : HTTPClient < Context > ;
3543 accessToken = '' ;
3644
45+ driveStore : DriveModel ;
46+ wikiNodeStore : WikiNodeModel ;
47+ documentStore : DocumentModel ;
48+
3749 constructor ( option : LarkAppServerOption | LarkAppClientOption ) {
3850 Object . assign ( this , option ) ;
3951
@@ -44,6 +56,18 @@ export class LarkApp implements LarkAppOption {
4456 responseType : 'json'
4557 } ) ;
4658 this . boot ( ) ;
59+
60+ const { client } = this ;
61+
62+ this . driveStore = new ( class extends DriveModel {
63+ client = client ;
64+ } ) ( ) ;
65+ this . wikiNodeStore = new ( class extends WikiNodeModel {
66+ client = client ;
67+ } ) ( '' , '' ) ;
68+ this . documentStore = new ( class extends DocumentModel {
69+ client = client ;
70+ } ) ( '' ) ;
4771 }
4872
4973 private boot ( ) {
@@ -152,56 +176,90 @@ export class LarkApp implements LarkAppOption {
152176 } , 'JS ticket' ) ;
153177
154178 /**
155- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/download }
179+ * @see {@link DriveModel#downloadFile }
156180 */
157181 async downloadFile ( id : string ) {
158182 await this . getAccessToken ( ) ;
159183
160- const { headers, body } = await this . client . get < Blob > (
161- `drive/v1/medias/${ id } /download` ,
162- { } ,
163- { responseType : 'blob' }
164- ) ;
165- const { 'Content-Disposition' : CD , 'Content-Type' : CT } = headers ;
166-
167- const [ type ] = ( CT as string ) ?. split ( ';' ) || [ ] ,
168- [ , fileName ] = ( CD as string ) ?. match ( / f i l e n a m e = " ? ( .* ?) " ? $ / ) || [ ] ;
169-
170- return new File ( [ body ! ] , fileName , { type } ) ;
184+ return this . driveStore . downloadFile ( id ) ;
171185 }
172186
173187 /**
174- * @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/upload_all }
188+ * @see {@link DriveModel#uploadFile }
175189 */
176190 async uploadFile ( file : File , parent_type : UploadTargetType , parent_node : string ) {
177191 await this . getAccessToken ( ) ;
178192
179- const form = makeFormData ( {
180- file,
181- file_name : file . name ,
182- size : file . size ,
183- parent_type,
184- parent_node
185- } ) ;
186- const { body } = await this . client . post < LarkData < { file_token : string } > > (
187- 'drive/v1/medias/upload_all' ,
188- form
189- ) ;
190- return body ! . data ! . file_token ;
193+ return this . driveStore . uploadFile ( file , parent_type , parent_node ) ;
191194 }
192195
193196 /**
194- * @see {@link WikiNodeModel.getOne }
197+ * @see {@link DriveModel#copyFile }
198+ * @see {@link WikiNodeModel#moveDocument }
195199 */
196- async wiki2docx ( id : string ) {
200+ copyFile (
201+ URI : `${string } /wiki/${string } `,
202+ name : string ,
203+ parent_node_token ?: string ,
204+ user_id_type ?: UserIdType
205+ ) : Promise < WikiNode > ;
206+ copyFile (
207+ URI : `${string } /${LarkDocumentType } /${string } `,
208+ name : string ,
209+ folder_token ?: string ,
210+ user_id_type ?: UserIdType
211+ ) : Promise < CopiedFile > ;
212+ async copyFile ( URI : string , name : string , folder_token ?: string , user_id_type ?: UserIdType ) {
197213 await this . getAccessToken ( ) ;
198214
215+ let [ type , token ] = new URL ( URI , 'http://localhost' ) . pathname . split ( '/' ) ,
216+ space_id : string | undefined ,
217+ parent_node_token = folder_token ;
218+
219+ if ( type === 'wiki' )
220+ ( {
221+ obj_type : type ,
222+ obj_token : token ,
223+ space_id,
224+ parent_node_token
225+ } = await this . wiki2drive ( token ) ) ;
226+
227+ const copidFile = await this . driveStore . copyFile (
228+ type as LarkDocumentType ,
229+ token ,
230+ name ,
231+ folder_token ,
232+ user_id_type
233+ ) ;
234+ if ( ! space_id ) return copidFile ;
235+
199236 const { client } = this ;
200237
201238 class InternalWikiNodeModel extends WikiNodeModel {
202239 client = client ;
203240 }
204- const { obj_type, obj_token } = await new InternalWikiNodeModel ( '' , '' ) . getOne ( id ) ;
241+ const wikiNodeStore = new InternalWikiNodeModel ( '' , space_id ) ;
242+
243+ return wikiNodeStore . moveDocument (
244+ { obj_type : type , obj_token : token } as WikiNode ,
245+ parent_node_token
246+ ) ;
247+ }
248+
249+ /**
250+ * @see {@link WikiNodeModel#getOne }
251+ */
252+ async wiki2drive ( id : string ) {
253+ await this . getAccessToken ( ) ;
254+
255+ return this . wikiNodeStore . getOne ( id ) ;
256+ }
257+
258+ /**
259+ * @deprecated Use {@link LarkApp#wiki2drive} instead
260+ */
261+ async wiki2docx ( id : string ) {
262+ const { obj_type, obj_token } = await this . wiki2drive ( id ) ;
205263
206264 return obj_type === 'docx' ? obj_token : '' ;
207265 }
@@ -216,12 +274,8 @@ export class LarkApp implements LarkAppOption {
216274
217275 const [ , type , id ] = URI . match ( LarkApp . documentPathPattern ) || [ ] ;
218276
219- const doc_token = type === 'wiki' ? await this . wiki2docx ( id ) : id ,
220- { client } = this ;
277+ const doc_token = type === 'wiki' ? ( await this . wiki2drive ( id ) ) . obj_token : id ;
221278
222- class InternalDocumentModel extends DocumentModel {
223- client = client ;
224- }
225- return new InternalDocumentModel ( '' ) . getOneContent ( doc_token , 'markdown' ) ;
279+ return this . documentStore . getOneContent ( doc_token , 'markdown' ) ;
226280 }
227281}
0 commit comments