| 1 | import { eq } from "drizzle-orm"; |
| 2 | import { db } |
| 3 | import { settings, userGoogleDrives |
| 4 | import { env } |
| 5 | import { encrypt, safeDecrypt |
| 6 | |
| 7 | const TOKEN_URL = " |
| 8 | const DRIVE_API = " |
| 9 | const USERINFO_URL = " |
| 10 | |
| 11 | export interface GDriveOperatorConfig |
| 12 | clientId: string; |
| 13 | clientSecret: string; |
| 14 | } |
| 15 | |
| 16 | export interface GDriveConnection |
| 17 | accessToken: string; |
| 18 | refreshToken: string; |
| 19 | email: string; |
| 20 | } |
| 21 | |
| 22 | async function readSettingsMap() |
| 23 | const rows = |
| 24 | return Object.fromEntries |
| 25 | } |
| 26 | |
| 27 | export async function getOperatorGDriveConfig |
| 28 | const s = |
| 29 | if (!s.google_drive_client_id |
| 30 | return { |
| 31 | clientId: s.google_drive_client_id |
| 32 | clientSecret: safeDecrypt(s |
| 33 | }; |
| 34 | } |
| 35 | |
| 36 | export async function getAppUrl |
| 37 | const s = |
| 38 | return env.APP_URL |
| 39 | } |
| 40 | |
| 41 | interface TokenResponse { |
| 42 | access_token: string; |
| 43 | expires_in: number; |
| 44 | refresh_token?: string |
| 45 | } |
| 46 | |
| 47 | async function tokenRequest( |
| 48 | const res = |
| 49 | method: "POST", |
| 50 | headers: { "Content-Type": |
| 51 | body: new URLSearchParams |
| 52 | }); |
| 53 | if (!res.ok |
| 54 | return (await res |
| 55 | } |
| 56 | |
| 57 | export async function refreshAccessToken |
| 58 | config: GDriveOperatorConfig |
| 59 | refreshToken: string, |
| 60 | ): Promise<{ |
| 61 | const data = |
| 62 | client_id: config.clientId |
| 63 | client_secret: config.clientSecret |
| 64 | refresh_token: refreshToken, |
| 65 | grant_type: "refresh_token", |
| 66 | }); |
| 67 | return { accessToken: |
| 68 | } |
| 69 | |
| 70 | export async function exchangeAuthorizationCode |
| 71 | code: string, |
| 72 | redirectUri: string, |
| 73 | ): Promise<{ |
| 74 | const config = |
| 75 | if (!config) |
| 76 | const data = |
| 77 | code, |
| 78 | client_id: config.clientId |
| 79 | client_secret: config.clientSecret |
| 80 | redirect_uri: redirectUri, |
| 81 | grant_type: "authorization_code", |
| 82 | }); |
| 83 | if (!data.refresh_token |
| 84 | return { |
| 85 | accessToken: data.access_token |
| 86 | refreshToken: data.refresh_token |
| 87 | expiresIn: data.expires_in |
| 88 | }; |
| 89 | } |
| 90 | |
| 91 | export async function fetchUserInfo |
| 92 | const res = |
| 93 | headers: { Authorization: ` |
| 94 | }); |
| 95 | if (!res.ok |
| 96 | return (await res |
| 97 | } |
| 98 | |
| 99 | const connectionLoads = new |
| 100 | |
| 101 | export function getGDriveConnection( |
| 102 | userId: string, |
| 103 | ): Promise<GDriveConnection |
| 104 | const existing = |
| 105 | if (existing) return |
| 106 | const promise = |
| 107 | connectionLoads.delete(userId |
| 108 | }); |
| 109 | connectionLoads.set(userId |
| 110 | return promise; |
| 111 | } |
| 112 | |
| 113 | async function loadGDriveConnection |
| 114 | userId: string, |
| 115 | ): Promise<GDriveConnection |
| 116 | const row = |
| 117 | where: eq(userGoogleDrives |
| 118 | }); |
| 119 | if (!row) |
| 120 | const config = |
| 121 | if (!config) |
| 122 | |
| 123 | const accessToken = |
| 124 | const refreshToken = |
| 125 | |
| 126 | if (row.expiresAt |
| 127 | return { accessToken, |
| 128 | } |
| 129 | |
| 130 | try { |
| 131 | const refreshed = |
| 132 | await db |
| 133 | .update(userGoogleDrives) |
| 134 | .set({ |
| 135 | accessToken: encrypt(refreshed |
| 136 | expiresAt: new Date |
| 137 | }) |
| 138 | .where(eq(userGoogleDrives |
| 139 | return { accessToken: |
| 140 | } catch { |
| 141 | return null; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | async function driveRequest( |
| 146 | config: GDriveOperatorConfig |
| 147 | connection: GDriveConnection |
| 148 | path: string, |
| 149 | init?: RequestInit |
| 150 | ): Promise<Response |
| 151 | const send = |
| 152 | fetch(`${DRIVE_API}${ |
| 153 | ...init, |
| 154 | headers: { |
| 155 | ...(init?.headers |
| 156 | Authorization: `Bearer ${token |
| 157 | }, |
| 158 | }); |
| 159 | |
| 160 | const res = |
| 161 | if (res.status |
| 162 | const refreshed = |
| 163 | return send(refreshed |
| 164 | } |
| 165 | |
| 166 | function escapeDriveQuery(value: |
| 167 | return value.replace |
| 168 | } |
| 169 | |
| 170 | async function findOrCreateFolder |
| 171 | config: GDriveOperatorConfig |
| 172 | connection: GDriveConnection |
| 173 | name: string, |
| 174 | parentId?: string |
| 175 | ): Promise<string |
| 176 | const q = |
| 177 | parentId ? ` and '${ |
| 178 | }`; |
| 179 | const listRes = |
| 180 | config, |
| 181 | connection, |
| 182 | `?q=${encodeURIComponent(q |
| 183 | ); |
| 184 | if (listRes.ok |
| 185 | const data = |
| 186 | if (data.files |
| 187 | } |
| 188 | |
| 189 | const createRes = |
| 190 | method: "POST", |
| 191 | headers: { "Content-Type": |
| 192 | body: JSON.stringify |
| 193 | name, |
| 194 | mimeType: "application/vnd.google-apps.folder", |
| 195 | ...(parentId ? |
| 196 | }), |
| 197 | }); |
| 198 | if (!createRes.ok |
| 199 | throw new Error( |
| 200 | } |
| 201 | const created = |
| 202 | return created.id |
| 203 | } |
| 204 | |
| 205 | const folderResolves = new |
| 206 | |
| 207 | export function ensureGDriveFolders( |
| 208 | connection: GDriveConnection |
| 209 | serverName: string, |
| 210 | serverUuid: string, |
| 211 | ): Promise<string |
| 212 | const key = |
| 213 | const existing = |
| 214 | if (existing) return |
| 215 | const promise = |
| 216 | () => { |
| 217 | folderResolves.delete(key |
| 218 | }, |
| 219 | ); |
| 220 | folderResolves.set(key |
| 221 | return promise; |
| 222 | } |
| 223 | |
| 224 | async function resolveGDriveFolders |
| 225 | connection: GDriveConnection |
| 226 | serverName: string, |
| 227 | serverUuid: string, |
| 228 | ): Promise<string |
| 229 | const config = |
| 230 | if (!config) |
| 231 | const parentId = |
| 232 | return findOrCreateFolder( |
| 233 | config, |
| 234 | connection, |
| 235 | `${serverName} (${serverUuid |
| 236 | parentId, |
| 237 | ); |
| 238 | } |
| 239 | |
| 240 | export async function downloadDriveFile |
| 241 | connection: GDriveConnection |
| 242 | fileId: string, |
| 243 | ): Promise<Response |
| 244 | const config = |
| 245 | if (!config) |
| 246 | return driveRequest(config |
| 247 | } |
| 248 | |
| 249 | export async function revokeGoogleToken |
| 250 | await fetch("https://oauth2.googleapis.com/revoke |
| 251 | method: "POST", |
| 252 | headers: { "Content-Type": |
| 253 | body: new URLSearchParams |
| 254 | }).catch(() |
| 255 | } |