Skip to content

Commit b08187a

Browse files
committed
🐛 修复Google Drive授权提示问题
1 parent 05a71f0 commit b08187a

File tree

2 files changed

+54
-56
lines changed

2 files changed

+54
-56
lines changed

packages/filesystem/googledrive/googledrive.ts

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ export default class GoogleDriveFileSystem implements FileSystem {
3333

3434
create(path: string): Promise<FileWriter> {
3535
return Promise.resolve(new GoogleDriveFileWriter(this, joinPath(this.path, path)));
36-
} async createDir(dir: string): Promise<void> {
36+
}
37+
async createDir(dir: string): Promise<void> {
3738
if (!dir) {
3839
return Promise.resolve();
3940
}
@@ -42,16 +43,16 @@ export default class GoogleDriveFileSystem implements FileSystem {
4243
const dirs = fullPath.split("/").filter(Boolean);
4344

4445
// 从根目录开始逐级创建目录
45-
let parentId = "root";
46+
let parentId = "appDataFolder";
4647
let currentPath = "";
4748

4849
// 逐级创建目录,使用缓存减少重复请求
4950
for (const dirName of dirs) {
5051
currentPath = joinPath(currentPath, dirName);
51-
52+
5253
// 先检查缓存
5354
let folderId = this.pathToIdCache.get(currentPath);
54-
55+
5556
if (!folderId) {
5657
// 缓存中没有,查找目录是否已存在
5758
let folder = await this.findFolderByName(dirName, parentId);
@@ -60,21 +61,22 @@ export default class GoogleDriveFileSystem implements FileSystem {
6061
folder = await this.createFolder(dirName, parentId);
6162
}
6263
folderId = folder.id;
63-
64+
6465
// 更新缓存
6566
this.pathToIdCache.set(currentPath, folderId);
6667
}
67-
68+
6869
parentId = folderId;
6970
}
70-
71+
7172
return Promise.resolve();
72-
} async findFolderByName(name: string, parentId: string): Promise<{ id: string; name: string } | null> {
73+
}
74+
async findFolderByName(name: string, parentId: string): Promise<{ id: string; name: string } | null> {
7375
const query = `name='${name}' and mimeType='application/vnd.google-apps.folder' and '${parentId}' in parents and trashed=false`;
7476
const response = await this.request(
75-
`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=files(id,name)`
77+
`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=files(id,name)&spaces=appDataFolder`
7678
);
77-
79+
7880
if (response.files && response.files.length > 0) {
7981
return response.files[0];
8082
}
@@ -85,7 +87,7 @@ export default class GoogleDriveFileSystem implements FileSystem {
8587
const myHeaders = new Headers();
8688
myHeaders.append("Content-Type", "application/json");
8789

88-
const response = await this.request("https://www.googleapis.com/drive/v3/files", {
90+
const response = await this.request("https://www.googleapis.com/drive/v3/files?spaces=appDataFolder", {
8991
method: "POST",
9092
headers: myHeaders,
9193
body: JSON.stringify({
@@ -136,18 +138,19 @@ export default class GoogleDriveFileSystem implements FileSystem {
136138
}
137139
return data;
138140
});
139-
} async delete(path: string): Promise<void> {
141+
}
142+
async delete(path: string): Promise<void> {
140143
const fullPath = joinPath(this.path, path);
141-
144+
142145
// 首先,找到要删除的文件或文件夹
143146
const fileId = await this.getFileId(fullPath);
144147
if (!fileId) {
145148
throw new Error(`File or directory not found: ${path}`);
146149
}
147-
150+
148151
// 删除文件或文件夹
149152
await this.request(
150-
`https://www.googleapis.com/drive/v3/files/${fileId}`,
153+
`https://www.googleapis.com/drive/v3/files/${fileId}?spaces=appDataFolder`,
151154
{
152155
method: "DELETE",
153156
},
@@ -157,55 +160,57 @@ export default class GoogleDriveFileSystem implements FileSystem {
157160
throw new Error(await resp.text());
158161
}
159162
});
160-
163+
161164
// 清除相关缓存
162165
this.clearRelatedCache(fullPath);
163-
}async getFileId(path: string): Promise<string | null> {
166+
}
167+
async getFileId(path: string): Promise<string | null> {
164168
if (path === "/" || path === "") {
165-
return "root";
169+
return "appDataFolder";
166170
}
167-
171+
168172
// 先检查缓存
169173
const cachedId = this.pathToIdCache.get(path);
170174
if (cachedId) {
171175
return cachedId;
172176
}
173-
177+
174178
// 从根目录开始逐级查找
175179
const pathParts = path.split("/").filter(Boolean);
176-
let parentId = "root";
180+
let parentId = "appDataFolder";
177181
let currentPath = "";
178-
182+
179183
// 逐级查找路径
180184
for (const part of pathParts) {
181185
currentPath = joinPath(currentPath, part);
182-
186+
183187
// 检查这个路径是否已经缓存
184188
const cachedPartId = this.pathToIdCache.get(currentPath);
185189
if (cachedPartId) {
186190
parentId = cachedPartId;
187191
continue;
188192
}
189-
193+
190194
const query = `name='${part}' and '${parentId}' in parents and trashed=false`;
191195
const response = await this.request(
192-
`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=files(id,name)`
196+
`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=files(id,name)&spaces=appDataFolder`
193197
);
194-
198+
195199
if (!response.files || response.files.length === 0) {
196200
return null;
197201
}
198-
202+
199203
parentId = response.files[0].id;
200-
204+
201205
// 缓存这个路径的ID
202206
this.pathToIdCache.set(currentPath, parentId);
203207
}
204-
208+
205209
return parentId;
206-
} async list(): Promise<File[]> {
207-
let folderId = "root";
208-
210+
}
211+
async list(): Promise<File[]> {
212+
let folderId = "appDataFolder";
213+
209214
// 获取当前目录的ID
210215
if (this.path !== "/") {
211216
const foundId = await this.getFileId(this.path);
@@ -214,13 +219,13 @@ export default class GoogleDriveFileSystem implements FileSystem {
214219
}
215220
folderId = foundId;
216221
}
217-
222+
218223
// 列出目录内容
219224
const query = `'${folderId}' in parents and trashed=false`;
220225
const response = await this.request(
221-
`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=files(id,name,mimeType,size,md5Checksum,createdTime,modifiedTime)`
226+
`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=files(id,name,mimeType,size,md5Checksum,createdTime,modifiedTime)&spaces=appDataFolder`
222227
);
223-
228+
224229
const list: File[] = [];
225230
if (response.files) {
226231
for (const item of response.files) {
@@ -234,59 +239,52 @@ export default class GoogleDriveFileSystem implements FileSystem {
234239
});
235240
}
236241
}
237-
242+
238243
return list;
239244
}
240245

241246
// 辅助方法:在指定目录中查找文件
242247
async findFileInDirectory(fileName: string, parentId: string): Promise<string | null> {
243248
const query = `name='${fileName}' and '${parentId}' in parents and trashed=false and mimeType!='application/vnd.google-apps.folder'`;
244249
const response = await this.request(
245-
`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=files(id)`
250+
`https://www.googleapis.com/drive/v3/files?q=${encodeURIComponent(query)}&fields=files(id)&spaces=appDataFolder`
246251
);
247-
252+
248253
if (response.files && response.files.length > 0) {
249254
return response.files[0].id;
250255
}
251256
return null;
252257
}
253-
258+
254259
// 清除相关缓存
255260
clearRelatedCache(path: string): void {
256261
// 清除路径缓存
257-
const pathsToRemove = Array.from(this.pathToIdCache.keys()).filter(p => p.startsWith(path));
258-
pathsToRemove.forEach(p => this.pathToIdCache.delete(p));
262+
const pathsToRemove = Array.from(this.pathToIdCache.keys()).filter((p) => p.startsWith(path));
263+
pathsToRemove.forEach((p) => this.pathToIdCache.delete(p));
259264
}
260265

261266
async getDirUrl(): Promise<string> {
262-
// Retrieve the folder ID for the current path
263-
const folderId = await this.getFileId(this.path);
264-
if (!folderId) {
265-
throw new Error(`Directory not found: ${this.path}`);
266-
}
267-
268-
// Construct and return the Google Drive folder URL
269-
return `https://drive.google.com/drive/folders/${folderId}`;
267+
throw new Error("Method not implemented.");
270268
}
271269

272270
// 确保目录存在并返回目录ID,优化Writer避免重复获取
273271
async ensureDirExists(dirPath: string): Promise<string> {
274272
if (dirPath === "/" || dirPath === "") {
275-
return "root";
273+
return "appDataFolder";
276274
}
277-
275+
278276
// 先检查缓存
279277
const cachedId = this.pathToIdCache.get(dirPath);
280278
if (cachedId) {
281279
return cachedId;
282280
}
283-
281+
284282
// 如果没有缓存,使用getFileId方法
285283
const foundId = await this.getFileId(dirPath);
286284
if (!foundId) {
287285
throw new Error(`Failed to create or find directory: ${dirPath}`);
288286
}
289-
287+
290288
// 缓存结果
291289
this.pathToIdCache.set(dirPath, foundId);
292290
return foundId;

packages/filesystem/googledrive/rw.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export class GoogleDriveFileReader implements FileReader {
2020
}
2121

2222
// 获取文件内容
23-
const data = await this.fs.request(`https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`, {}, true);
23+
const data = await this.fs.request(`https://www.googleapis.com/drive/v3/files/${fileId}?alt=media&spaces=appDataFolder`, {}, true);
2424

2525
if (data.status !== 200) {
2626
return Promise.reject(await data.text());
@@ -78,7 +78,7 @@ export class GoogleDriveFileWriter implements FileWriter {
7878
formData.append("metadata", new Blob([JSON.stringify(metadata)], { type: "application/json" }));
7979
formData.append("file", content instanceof Blob ? content : new Blob([content]));
8080

81-
await this.fs.request(`https://www.googleapis.com/upload/drive/v3/files/${fileId}?uploadType=multipart`, {
81+
await this.fs.request(`https://www.googleapis.com/upload/drive/v3/files/${fileId}?uploadType=multipart&spaces=appDataFolder`, {
8282
method: "PATCH",
8383
body: formData,
8484
});
@@ -98,7 +98,7 @@ export class GoogleDriveFileWriter implements FileWriter {
9898
formData.append("metadata", new Blob([JSON.stringify(metadata)], { type: "application/json" }));
9999
formData.append("file", content instanceof Blob ? content : new Blob([content]));
100100

101-
await this.fs.request(`https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart`, {
101+
await this.fs.request(`https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&spaces=appDataFolder`, {
102102
method: "POST",
103103
body: formData,
104104
});

0 commit comments

Comments
 (0)