forked from ztjhz/BetterChatGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle-api.ts
More file actions
191 lines (176 loc) · 4.88 KB
/
Copy pathgoogle-api.ts
File metadata and controls
191 lines (176 loc) · 4.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { debounce } from 'lodash';
import { StorageValue } from 'zustand/middleware';
import useStore from '@store/store';
import useCloudAuthStore from '@store/cloud-auth-store';
import {
GoogleTokenInfo,
GoogleFileResource,
GoogleFileList,
} from '@type/google-api';
import PersistStorageState from '@type/persist';
import { createMultipartRelatedBody } from './helper';
export const createDriveFile = async (
file: File,
accessToken: string
): Promise<GoogleFileResource> => {
const boundary = 'better_chatgpt';
const metadata = {
name: file.name,
mimeType: file.type,
};
const requestBody = createMultipartRelatedBody(metadata, file, boundary);
const response = await fetch(
'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart',
{
method: 'POST',
headers: {
Authorization: `Bearer ${accessToken}`,
'Content-Type': `multipart/related; boundary=${boundary}`,
'Content-Length': requestBody.size.toString(),
},
body: requestBody,
}
);
if (response.ok) {
const result: GoogleFileResource = await response.json();
return result;
} else {
throw new Error(
`Error uploading file: ${response.status} ${response.statusText}`
);
}
};
export const getDriveFile = async <S>(
fileId: string,
accessToken: string
): Promise<StorageValue<S>> => {
const response = await fetch(
`https://content.googleapis.com/drive/v3/files/${fileId}?alt=media`,
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
}
);
const result: StorageValue<S> = await response.json();
return result;
};
export const getDriveFileTyped = async (
fileId: string,
accessToken: string
): Promise<StorageValue<PersistStorageState>> => {
return await getDriveFile(fileId, accessToken);
};
export const listDriveFiles = async (
accessToken: string
): Promise<GoogleFileList> => {
const response = await fetch(
'https://www.googleapis.com/drive/v3/files?orderBy=createdTime desc',
{
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
}
);
if (!response.ok) {
throw new Error(
`Error listing google drive files: ${response.status} ${response.statusText}`
);
}
const result: GoogleFileList = await response.json();
return result;
};
export const updateDriveFile = async (
file: File,
fileId: string,
accessToken: string
): Promise<GoogleFileResource> => {
const response = await fetch(
`https://www.googleapis.com/upload/drive/v3/files/${fileId}`,
{
method: 'PATCH',
headers: {
Authorization: `Bearer ${accessToken}`,
},
body: file,
}
);
if (response.ok) {
const result: GoogleFileResource = await response.json();
return result;
} else {
throw new Error(
`Error uploading file: ${response.status} ${response.statusText}`
);
}
};
export const updateDriveFileName = async (
fileName: string,
fileId: string,
accessToken: string
) => {
const response = await fetch(
`https://www.googleapis.com/drive/v3/files/${fileId}`,
{
method: 'PATCH',
headers: {
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify({ name: fileName }),
}
);
if (response.ok) {
const result: GoogleFileResource = await response.json();
return result;
} else {
throw new Error(
`Error updating file name: ${response.status} ${response.statusText}`
);
}
};
export const deleteDriveFile = async (fileId: string, accessToken: string) => {
const response = await fetch(
`https://www.googleapis.com/drive/v3/files/${fileId}`,
{
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${accessToken}`,
},
}
);
if (response.ok) {
return true;
} else {
throw new Error(
`Error deleting file name: ${response.status} ${response.statusText}`
);
}
};
export const validateGoogleOath2AccessToken = async (accessToken: string) => {
const response = await fetch(
`https://oauth2.googleapis.com/tokeninfo?access_token=${accessToken}`
);
if (!response.ok) return false;
const result: GoogleTokenInfo = await response.json();
return result;
};
export const updateDriveFileDebounced = debounce(
async (file: File, fileId: string, accessToken: string) => {
try {
const result = await updateDriveFile(file, fileId, accessToken);
useCloudAuthStore.getState().setSyncStatus('synced');
return result;
} catch (e: unknown) {
useStore.getState().setToastMessage((e as Error).message);
useStore.getState().setToastShow(true);
useStore.getState().setToastStatus('error');
useCloudAuthStore.getState().setSyncStatus('unauthenticated');
}
},
5000
);