Skip to content

Commit 3fa3b71

Browse files
committed
[optimize] get Document Title before copying automatically
1 parent b4b565e commit 3fa3b71

File tree

5 files changed

+53
-21
lines changed

5 files changed

+53
-21
lines changed

ReadMe.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,12 @@ await writeFile('document.html', markup);
150150
## Scaffolds
151151

152152
1. https://github.com/idea2app/Lark-Next-Bootstrap-ts
153+
2. https://github.com/idea2app/Lark-Next-Shadcn-ts
153154

154155
## User cases
155156

156157
1. [idea2app web-site](https://github.com/idea2app/idea2app.github.io/tree/main/models)
157-
2. [KaiYuanShe web-site](https://github.com/kaiyuanshe/kaiyuanshe.github.io/tree/main/models)
158+
2. [Open-Source-Bazaar web-site](https://github.com/Open-Source-Bazaar/Open-Source-Bazaar.github.io/tree/main/models)
158159

159160
## Related with
160161

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobx-lark",
3-
"version": "2.6.0-rc.0",
3+
"version": "2.6.0-rc.1",
44
"license": "LGPL-3.0",
55
"author": "shiy2008@gmail.com",
66
"description": "Unofficial TypeScript SDK for FeiShu/Lark API, which is based on MobX-RESTful.",

src/Lark.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { buildURLData, cache, sleep } from 'web-utility';
44
import {
55
CopiedFile,
66
DocumentModel,
7-
DriveModel,
7+
DriveFileModel,
88
UserIdType,
99
WikiNode,
1010
WikiNodeModel
@@ -42,7 +42,7 @@ export class LarkApp implements LarkAppOption {
4242
client: HTTPClient<Context>;
4343
accessToken = '';
4444

45-
driveStore: DriveModel;
45+
driveFileStore: DriveFileModel;
4646
wikiNodeStore: WikiNodeModel;
4747
documentStore: DocumentModel;
4848

@@ -59,7 +59,7 @@ export class LarkApp implements LarkAppOption {
5959

6060
const { client } = this;
6161

62-
this.driveStore = new (class extends DriveModel {
62+
this.driveFileStore = new (class extends DriveFileModel {
6363
client = client;
6464
})();
6565
this.wikiNodeStore = new (class extends WikiNodeModel {
@@ -176,40 +176,40 @@ export class LarkApp implements LarkAppOption {
176176
}, 'JS ticket');
177177

178178
/**
179-
* @see {@link DriveModel#downloadFile}
179+
* @see {@link DriveFileModel#downloadOne}
180180
*/
181181
async downloadFile(id: string) {
182182
await this.getAccessToken();
183183

184-
return this.driveStore.downloadFile(id);
184+
return this.driveFileStore.downloadOne(id);
185185
}
186186

187187
/**
188-
* @see {@link DriveModel#uploadFile}
188+
* @see {@link DriveFileModel#uploadOne}
189189
*/
190190
async uploadFile(file: File, parent_type: UploadTargetType, parent_node: string) {
191191
await this.getAccessToken();
192192

193-
return this.driveStore.uploadFile(file, parent_type, parent_node);
193+
return this.driveFileStore.uploadOne(file, parent_type, parent_node);
194194
}
195195

196196
/**
197-
* @see {@link DriveModel#copyFile}
197+
* @see {@link DriveFileModel#copyOne}
198198
* @see {@link WikiNodeModel#moveDocument}
199199
*/
200200
copyFile(
201201
URI: `${string}/wiki/${string}`,
202-
name: string,
202+
name?: string,
203203
parent_node_token?: string,
204204
user_id_type?: UserIdType
205205
): Promise<WikiNode>;
206206
copyFile(
207207
URI: `${string}/${LarkDocumentType}/${string}`,
208-
name: string,
208+
name?: string,
209209
folder_token?: string,
210210
user_id_type?: UserIdType
211211
): Promise<CopiedFile>;
212-
async copyFile(URI: string, name: string, folder_token?: string, user_id_type?: UserIdType) {
212+
async copyFile(URI: string, name?: string, folder_token?: string, user_id_type?: UserIdType) {
213213
await this.getAccessToken();
214214

215215
let [type, token] = new URL(URI, 'http://localhost').pathname.split('/'),
@@ -220,11 +220,12 @@ export class LarkApp implements LarkAppOption {
220220
({
221221
obj_type: type,
222222
obj_token: token,
223+
title: name,
223224
space_id,
224225
parent_node_token
225226
} = await this.wiki2drive(token));
226227

227-
const copidFile = await this.driveStore.copyFile(
228+
const copidFile = await this.driveFileStore.copyOne(
228229
type as LarkDocumentType,
229230
token,
230231
name,

src/module/Drive/index.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
import { makeFormData } from 'koajax';
2-
import { BaseModel, RESTClient, toggle } from 'mobx-restful';
2+
import { BaseListModel, RESTClient, toggle } from 'mobx-restful';
33
import { buildURLData } from 'web-utility';
44

55
import { LarkData, LarkDocumentType, UploadTargetType } from '../../type';
66
import { UserIdType } from '../User/type';
7+
import { CopiedFile, DriveFile } from './type';
78

8-
export type CopiedFile = Record<'token' | 'type' | 'name' | 'parent_token' | 'url', string>;
9+
export * from './type';
910

10-
export abstract class DriveModel extends BaseModel {
11+
export abstract class DriveFileModel extends BaseListModel<DriveFile> {
1112
baseURI = 'drive/v1';
1213
abstract client: RESTClient;
1314

15+
/**
16+
* @see {@link https://open.feishu.cn/document/server-docs/docs/drive-v1/file/batch_query}
17+
*
18+
* @param URI such as `docx/xxxyyyzzz`
19+
*/
20+
@toggle('downloading')
21+
async getOne(URI: string, user_id_type?: UserIdType) {
22+
let [doc_type, doc_token] = new URL(URI, 'http://localhost').pathname.split('/');
23+
24+
const { body } = await this.client.post<LarkData<{ metas: DriveFile[] }>>(
25+
`${this.baseURI}/metas/batch_query?${buildURLData({ user_id_type })}`,
26+
{ request_docs: [{ doc_type, doc_token }], with_url: true }
27+
);
28+
return body!.data!.metas[0];
29+
}
30+
1431
/**
1532
* @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/download}
1633
*/
1734
@toggle('downloading')
18-
async downloadFile(id: string) {
35+
async downloadOne(id: string) {
1936
const { headers, body } = await this.client.get<Blob>(
2037
`${this.baseURI}/medias/${id}/download`,
2138
{},
@@ -33,7 +50,7 @@ export abstract class DriveModel extends BaseModel {
3350
* @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/media/upload_all}
3451
*/
3552
@toggle('uploading')
36-
async uploadFile(file: File, parent_type: UploadTargetType, parent_node: string) {
53+
async uploadOne(file: File, parent_type: UploadTargetType, parent_node: string) {
3754
const form = makeFormData({
3855
file,
3956
file_name: file.name,
@@ -64,13 +81,14 @@ export abstract class DriveModel extends BaseModel {
6481
* @see {@link https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/drive-v1/file/copy}
6582
*/
6683
@toggle('uploading')
67-
async copyFile(
84+
async copyOne(
6885
type: LarkDocumentType,
6986
file_token: string,
70-
name: string,
87+
name?: string,
7188
folder_token?: string,
7289
user_id_type?: UserIdType
7390
) {
91+
name ||= (await this.getOne(`${type}/${file_token}`, user_id_type)).title + ' (copy)';
7492
folder_token ||= (await this.getRootFolder()).token;
7593

7694
const { body } = await this.client.post<LarkData<{ file: CopiedFile }>>(

src/module/Drive/type.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
export type DriveFile = Record<
2+
| `doc_${'token' | 'type'}`
3+
| 'title'
4+
| 'owner_id'
5+
| 'create_time'
6+
| `latest_modify_${'user' | 'time'}`
7+
| 'url'
8+
| 'sec_label_name',
9+
string
10+
>;
11+
12+
export type CopiedFile = Record<'token' | 'type' | 'name' | 'parent_token' | 'url', string>;

0 commit comments

Comments
 (0)