mirror of
https://git.mirrors.martin98.com/https://github.com/open-webui/open-webui
synced 2025-08-16 10:15:58 +08:00
fix formatting
This commit is contained in:
parent
81bb816881
commit
15f14d0318
1
.gitignore
vendored
1
.gitignore
vendored
@ -307,3 +307,4 @@ dist
|
|||||||
cypress/videos
|
cypress/videos
|
||||||
cypress/screenshots
|
cypress/screenshots
|
||||||
.vscode/settings.json
|
.vscode/settings.json
|
||||||
|
.aider*
|
||||||
|
70
package-lock.json
generated
70
package-lock.json
generated
@ -1815,9 +1815,10 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@polka/url": {
|
"node_modules/@polka/url": {
|
||||||
"version": "1.0.0-next.25",
|
"version": "1.0.0-next.28",
|
||||||
"resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.25.tgz",
|
"resolved": "https://registry.npmjs.org/@polka/url/-/url-1.0.0-next.28.tgz",
|
||||||
"integrity": "sha512-j7P6Rgr3mmtdkeDGTe0E/aYyWEWVtc5yFXtHCRHs28/jptDEWfaVOc5T7cblqy1XKPPfCxJc/8DwQ5YgLOZOVQ=="
|
"integrity": "sha512-8LduaNlMZGwdZ6qWrKlfa+2M4gahzFkprZiAt2TF8uS0qQgBizKXpXURqvTJ4WtmupWxaLqjRb2UCTe72mu+Aw==",
|
||||||
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/@popperjs/core": {
|
"node_modules/@popperjs/core": {
|
||||||
"version": "2.11.8",
|
"version": "2.11.8",
|
||||||
@ -2230,22 +2231,23 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@sveltejs/kit": {
|
"node_modules/@sveltejs/kit": {
|
||||||
"version": "2.6.2",
|
"version": "2.11.1",
|
||||||
"resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-2.6.2.tgz",
|
"resolved": "https://registry.npmjs.org/@sveltejs/kit/-/kit-2.11.1.tgz",
|
||||||
"integrity": "sha512-ruogrSPXjckn5poUiZU8VYNCSPHq66SFR1AATvOikQxtP6LNI4niAZVX/AWZRe/EPDG3oY2DNJ9c5z7u0t2NAQ==",
|
"integrity": "sha512-dAiHDEd+AOm20eYdMPV1a2eKBOc0s/7XsSs7PCoNv2kKS7BAoVRC9uzR+FQmxLtp8xuEo9z8CtrMQoszkThltQ==",
|
||||||
"hasInstallScript": true,
|
"hasInstallScript": true,
|
||||||
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@types/cookie": "^0.6.0",
|
"@types/cookie": "^0.6.0",
|
||||||
"cookie": "^0.7.0",
|
"cookie": "^0.6.0",
|
||||||
"devalue": "^5.1.0",
|
"devalue": "^5.1.0",
|
||||||
"esm-env": "^1.0.0",
|
"esm-env": "^1.2.1",
|
||||||
"import-meta-resolve": "^4.1.0",
|
"import-meta-resolve": "^4.1.0",
|
||||||
"kleur": "^4.1.5",
|
"kleur": "^4.1.5",
|
||||||
"magic-string": "^0.30.5",
|
"magic-string": "^0.30.5",
|
||||||
"mrmime": "^2.0.0",
|
"mrmime": "^2.0.0",
|
||||||
"sade": "^1.8.1",
|
"sade": "^1.8.1",
|
||||||
"set-cookie-parser": "^2.6.0",
|
"set-cookie-parser": "^2.6.0",
|
||||||
"sirv": "^2.0.4",
|
"sirv": "^3.0.0",
|
||||||
"tiny-glob": "^0.2.9"
|
"tiny-glob": "^0.2.9"
|
||||||
},
|
},
|
||||||
"bin": {
|
"bin": {
|
||||||
@ -2255,9 +2257,9 @@
|
|||||||
"node": ">=18.13"
|
"node": ">=18.13"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"@sveltejs/vite-plugin-svelte": "^3.0.0 || ^4.0.0-next.1",
|
"@sveltejs/vite-plugin-svelte": "^3.0.0 || ^4.0.0-next.1 || ^5.0.0",
|
||||||
"svelte": "^4.0.0 || ^5.0.0-next.0",
|
"svelte": "^4.0.0 || ^5.0.0-next.0",
|
||||||
"vite": "^5.0.3"
|
"vite": "^5.0.3 || ^6.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/@sveltejs/vite-plugin-svelte": {
|
"node_modules/@sveltejs/vite-plugin-svelte": {
|
||||||
@ -3955,9 +3957,10 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/cookie": {
|
"node_modules/cookie": {
|
||||||
"version": "0.7.1",
|
"version": "0.6.0",
|
||||||
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.7.1.tgz",
|
"resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz",
|
||||||
"integrity": "sha512-6DnInpx7SJ2AK3+CTUE/ZM0vWTUboZCegxhC2xiIydHR9jNuTAASBrfEpHhiGOZw/nX51bHt6YQl8jsGo4y/0w==",
|
"integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==",
|
||||||
|
"license": "MIT",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 0.6"
|
"node": ">= 0.6"
|
||||||
}
|
}
|
||||||
@ -3993,9 +3996,10 @@
|
|||||||
"integrity": "sha512-VQ2MBenTq1fWZUH9DJNGti7kKv6EeAuYr3cLwxUWhIu1baTaXh4Ib5W2CqHVqib4/MqbYGJqiL3Zb8GJZr3l4g=="
|
"integrity": "sha512-VQ2MBenTq1fWZUH9DJNGti7kKv6EeAuYr3cLwxUWhIu1baTaXh4Ib5W2CqHVqib4/MqbYGJqiL3Zb8GJZr3l4g=="
|
||||||
},
|
},
|
||||||
"node_modules/cross-spawn": {
|
"node_modules/cross-spawn": {
|
||||||
"version": "7.0.3",
|
"version": "7.0.6",
|
||||||
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz",
|
"resolved": "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.6.tgz",
|
||||||
"integrity": "sha512-iRDPJKUPVEND7dHPO8rkbOnPpyDygcDFtWjpeWNCgy8WP2rXcxXL8TskReQl6OrB2G7+UJrags1q15Fudc7G6w==",
|
"integrity": "sha512-uV2QOWP2nWzsy2aMp8aRibhi9dlzF5Hgh5SHaB9OiTGEyDTiJJyx0uy51QXdyWbtAHNua4XJzUKca3OzKUd3vA==",
|
||||||
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"path-key": "^3.1.0",
|
"path-key": "^3.1.0",
|
||||||
"shebang-command": "^2.0.0",
|
"shebang-command": "^2.0.0",
|
||||||
@ -5240,9 +5244,10 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/esm-env": {
|
"node_modules/esm-env": {
|
||||||
"version": "1.0.0",
|
"version": "1.2.1",
|
||||||
"resolved": "https://registry.npmjs.org/esm-env/-/esm-env-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/esm-env/-/esm-env-1.2.1.tgz",
|
||||||
"integrity": "sha512-Cf6VksWPsTuW01vU9Mk/3vRue91Zevka5SjyNf3nEpokFRuqt/KjUQoGAwq9qMmhpLTHmXzSIrFRw8zxWzmFBA=="
|
"integrity": "sha512-U9JedYYjCnadUlXk7e1Kr+aENQhtUaoaV9+gZm1T8LC/YBAPJx3NSPIAurFOC0U5vrdSevnUJS2/wUVxGwPhng==",
|
||||||
|
"license": "MIT"
|
||||||
},
|
},
|
||||||
"node_modules/espree": {
|
"node_modules/espree": {
|
||||||
"version": "9.6.1",
|
"version": "9.6.1",
|
||||||
@ -7755,6 +7760,7 @@
|
|||||||
"version": "2.0.0",
|
"version": "2.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/mrmime/-/mrmime-2.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/mrmime/-/mrmime-2.0.0.tgz",
|
||||||
"integrity": "sha512-eu38+hdgojoyq63s+yTpN4XMBdt5l8HhMhc4VKLO9KM5caLIBvUm4thi7fFaxyTmCKeNnXZ5pAlBwCUnhA09uw==",
|
"integrity": "sha512-eu38+hdgojoyq63s+yTpN4XMBdt5l8HhMhc4VKLO9KM5caLIBvUm4thi7fFaxyTmCKeNnXZ5pAlBwCUnhA09uw==",
|
||||||
|
"license": "MIT",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10"
|
"node": ">=10"
|
||||||
}
|
}
|
||||||
@ -7776,15 +7782,16 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/nanoid": {
|
"node_modules/nanoid": {
|
||||||
"version": "5.0.6",
|
"version": "5.0.9",
|
||||||
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-5.0.6.tgz",
|
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-5.0.9.tgz",
|
||||||
"integrity": "sha512-rRq0eMHoGZxlvaFOUdK1Ev83Bd1IgzzR+WJ3IbDJ7QOSdAxYjlurSPqFs9s4lJg29RT6nPwizFtJhQS6V5xgiA==",
|
"integrity": "sha512-Aooyr6MXU6HpvvWXKoVoXwKMs/KyVakWwg7xQfv5/S/RIgJMy0Ifa45H9qqYy7pTCszrHzP21Uk4PZq2HpEM8Q==",
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
"type": "github",
|
"type": "github",
|
||||||
"url": "https://github.com/sponsors/ai"
|
"url": "https://github.com/sponsors/ai"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"license": "MIT",
|
||||||
"bin": {
|
"bin": {
|
||||||
"nanoid": "bin/nanoid.js"
|
"nanoid": "bin/nanoid.js"
|
||||||
},
|
},
|
||||||
@ -8485,15 +8492,16 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"node_modules/postcss/node_modules/nanoid": {
|
"node_modules/postcss/node_modules/nanoid": {
|
||||||
"version": "3.3.7",
|
"version": "3.3.8",
|
||||||
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz",
|
"resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.8.tgz",
|
||||||
"integrity": "sha512-eSRppjcPIatRIMC1U6UngP8XFcz8MQWGQdt1MTBQ7NaAmvXDfvNxbvWV3x2y6CdEUciCSsDHDQZbhYaB8QEo2g==",
|
"integrity": "sha512-WNLf5Sd8oZxOm+TzppcYk8gVOgP+l58xNy58D0nbUnOxOWRWvlcCV4kUF7ltmI6PsrLl/BgKEyS4mqsGChFN0w==",
|
||||||
"funding": [
|
"funding": [
|
||||||
{
|
{
|
||||||
"type": "github",
|
"type": "github",
|
||||||
"url": "https://github.com/sponsors/ai"
|
"url": "https://github.com/sponsors/ai"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"license": "MIT",
|
||||||
"bin": {
|
"bin": {
|
||||||
"nanoid": "bin/nanoid.cjs"
|
"nanoid": "bin/nanoid.cjs"
|
||||||
},
|
},
|
||||||
@ -9456,16 +9464,17 @@
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/sirv": {
|
"node_modules/sirv": {
|
||||||
"version": "2.0.4",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/sirv/-/sirv-2.0.4.tgz",
|
"resolved": "https://registry.npmjs.org/sirv/-/sirv-3.0.0.tgz",
|
||||||
"integrity": "sha512-94Bdh3cC2PKrbgSOUqTiGPWVZeSiXfKOVZNJniWoqrWrRkB1CJzBU3NEbiTsPcYy1lDsANA/THzS+9WBiy5nfQ==",
|
"integrity": "sha512-BPwJGUeDaDCHihkORDchNyyTvWFhcusy1XMmhEVTQTwGeybFbp8YEmB+njbPnth1FibULBSBVwCQni25XlCUDg==",
|
||||||
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@polka/url": "^1.0.0-next.24",
|
"@polka/url": "^1.0.0-next.24",
|
||||||
"mrmime": "^2.0.0",
|
"mrmime": "^2.0.0",
|
||||||
"totalist": "^3.0.0"
|
"totalist": "^3.0.0"
|
||||||
},
|
},
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">= 10"
|
"node": ">=18"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"node_modules/slash": {
|
"node_modules/slash": {
|
||||||
@ -10334,6 +10343,7 @@
|
|||||||
"version": "3.0.1",
|
"version": "3.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/totalist/-/totalist-3.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/totalist/-/totalist-3.0.1.tgz",
|
||||||
"integrity": "sha512-sf4i37nQ2LBx4m3wB74y+ubopq6W/dIzXg0FDGjsYnZHVa1Da8FH853wlL2gtUhg+xJXjfk3kUZS3BRoQeoQBQ==",
|
"integrity": "sha512-sf4i37nQ2LBx4m3wB74y+ubopq6W/dIzXg0FDGjsYnZHVa1Da8FH853wlL2gtUhg+xJXjfk3kUZS3BRoQeoQBQ==",
|
||||||
|
"license": "MIT",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=6"
|
"node": ">=6"
|
||||||
}
|
}
|
||||||
|
@ -385,8 +385,8 @@
|
|||||||
// Configure fetch options with proper headers
|
// Configure fetch options with proper headers
|
||||||
const fetchOptions = {
|
const fetchOptions = {
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': fileData.headers.Authorization,
|
Authorization: fileData.headers.Authorization,
|
||||||
'Accept': '*/*'
|
Accept: '*/*'
|
||||||
},
|
},
|
||||||
method: 'GET'
|
method: 'GET'
|
||||||
};
|
};
|
||||||
@ -407,7 +407,7 @@
|
|||||||
// Convert response to blob
|
// Convert response to blob
|
||||||
console.log('Converting response to blob...');
|
console.log('Converting response to blob...');
|
||||||
const fileBlob = await fileResponse.blob();
|
const fileBlob = await fileResponse.blob();
|
||||||
|
|
||||||
if (fileBlob.size === 0) {
|
if (fileBlob.size === 0) {
|
||||||
throw new Error('Retrieved file is empty');
|
throw new Error('Retrieved file is empty');
|
||||||
}
|
}
|
||||||
@ -418,7 +418,7 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Create File object with proper MIME type
|
// Create File object with proper MIME type
|
||||||
const file = new File([fileBlob], fileData.name, {
|
const file = new File([fileBlob], fileData.name, {
|
||||||
type: fileBlob.type || contentType
|
type: fileBlob.type || contentType
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -441,7 +441,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
console.log('File uploaded successfully:', uploadedFile);
|
console.log('File uploaded successfully:', uploadedFile);
|
||||||
|
|
||||||
// Update file item with upload results
|
// Update file item with upload results
|
||||||
fileItem.status = 'uploaded';
|
fileItem.status = 'uploaded';
|
||||||
fileItem.file = uploadedFile;
|
fileItem.file = uploadedFile;
|
||||||
@ -449,15 +449,17 @@
|
|||||||
fileItem.size = file.size;
|
fileItem.size = file.size;
|
||||||
fileItem.collection_name = uploadedFile?.meta?.collection_name;
|
fileItem.collection_name = uploadedFile?.meta?.collection_name;
|
||||||
fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
|
fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
|
||||||
|
|
||||||
files = files;
|
files = files;
|
||||||
toast.success($i18n.t('File uploaded successfully'));
|
toast.success($i18n.t('File uploaded successfully'));
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error uploading file:', e);
|
console.error('Error uploading file:', e);
|
||||||
files = files.filter((f) => f.itemId !== tempItemId);
|
files = files.filter((f) => f.itemId !== tempItemId);
|
||||||
toast.error($i18n.t('Error uploading file: {{error}}', {
|
toast.error(
|
||||||
error: e.message || 'Unknown error'
|
$i18n.t('Error uploading file: {{error}}', {
|
||||||
}));
|
error: e.message || 'Unknown error'
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -90,7 +90,6 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const uploadFileHandler = async (file) => {
|
const uploadFileHandler = async (file) => {
|
||||||
|
|
||||||
const tempItemId = uuidv4();
|
const tempItemId = uuidv4();
|
||||||
const fileItem = {
|
const fileItem = {
|
||||||
type: 'file',
|
type: 'file',
|
||||||
@ -138,7 +137,7 @@
|
|||||||
name: fileItem.name,
|
name: fileItem.name,
|
||||||
collection: uploadedFile?.meta?.collection_name
|
collection: uploadedFile?.meta?.collection_name
|
||||||
});
|
});
|
||||||
|
|
||||||
if (uploadedFile.error) {
|
if (uploadedFile.error) {
|
||||||
console.warn('File upload warning:', uploadedFile.error);
|
console.warn('File upload warning:', uploadedFile.error);
|
||||||
toast.warning(uploadedFile.error);
|
toast.warning(uploadedFile.error);
|
||||||
@ -147,7 +146,8 @@
|
|||||||
fileItem.status = 'uploaded';
|
fileItem.status = 'uploaded';
|
||||||
fileItem.file = uploadedFile;
|
fileItem.file = uploadedFile;
|
||||||
fileItem.id = uploadedFile.id;
|
fileItem.id = uploadedFile.id;
|
||||||
fileItem.collection_name = uploadedFile?.meta?.collection_name || uploadedFile?.collection_name;
|
fileItem.collection_name =
|
||||||
|
uploadedFile?.meta?.collection_name || uploadedFile?.collection_name;
|
||||||
fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
|
fileItem.url = `${WEBUI_API_BASE_URL}/files/${uploadedFile.id}`;
|
||||||
|
|
||||||
files = files;
|
files = files;
|
||||||
@ -526,9 +526,11 @@
|
|||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Google Drive Error:', error);
|
console.error('Google Drive Error:', error);
|
||||||
toast.error($i18n.t('Error accessing Google Drive: {{error}}', {
|
toast.error(
|
||||||
error: error.message
|
$i18n.t('Error accessing Google Drive: {{error}}', {
|
||||||
}));
|
error: error.message
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
onClose={async () => {
|
onClose={async () => {
|
||||||
|
@ -1,16 +1,19 @@
|
|||||||
// Google Drive Picker API configuration
|
// Google Drive Picker API configuration
|
||||||
const API_KEY = import.meta.env.VITE_GOOGLE_API_KEY;
|
const API_KEY = import.meta.env.VITE_GOOGLE_API_KEY;
|
||||||
const CLIENT_ID = import.meta.env.VITE_GOOGLE_CLIENT_ID;
|
const CLIENT_ID = import.meta.env.VITE_GOOGLE_CLIENT_ID;
|
||||||
const SCOPE = ['https://www.googleapis.com/auth/drive.readonly', 'https://www.googleapis.com/auth/drive.file'];
|
const SCOPE = [
|
||||||
|
'https://www.googleapis.com/auth/drive.readonly',
|
||||||
|
'https://www.googleapis.com/auth/drive.file'
|
||||||
|
];
|
||||||
|
|
||||||
// Validate required credentials
|
// Validate required credentials
|
||||||
const validateCredentials = () => {
|
const validateCredentials = () => {
|
||||||
if (!API_KEY || !CLIENT_ID) {
|
if (!API_KEY || !CLIENT_ID) {
|
||||||
throw new Error('Google Drive API credentials not configured');
|
throw new Error('Google Drive API credentials not configured');
|
||||||
}
|
}
|
||||||
if (API_KEY === 'your-api-key' || CLIENT_ID === 'your-client-id') {
|
if (API_KEY === 'your-api-key' || CLIENT_ID === 'your-client-id') {
|
||||||
throw new Error('Please configure valid Google Drive API credentials');
|
throw new Error('Please configure valid Google Drive API credentials');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let pickerApiLoaded = false;
|
let pickerApiLoaded = false;
|
||||||
@ -18,170 +21,176 @@ let oauthToken: string | null = null;
|
|||||||
let initialized = false;
|
let initialized = false;
|
||||||
|
|
||||||
export const loadGoogleDriveApi = () => {
|
export const loadGoogleDriveApi = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (typeof gapi === 'undefined') {
|
if (typeof gapi === 'undefined') {
|
||||||
const script = document.createElement('script');
|
const script = document.createElement('script');
|
||||||
script.src = 'https://apis.google.com/js/api.js';
|
script.src = 'https://apis.google.com/js/api.js';
|
||||||
script.onload = () => {
|
script.onload = () => {
|
||||||
gapi.load('picker', () => {
|
gapi.load('picker', () => {
|
||||||
pickerApiLoaded = true;
|
pickerApiLoaded = true;
|
||||||
resolve(true);
|
resolve(true);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
script.onerror = reject;
|
script.onerror = reject;
|
||||||
document.body.appendChild(script);
|
document.body.appendChild(script);
|
||||||
} else {
|
} else {
|
||||||
gapi.load('picker', () => {
|
gapi.load('picker', () => {
|
||||||
pickerApiLoaded = true;
|
pickerApiLoaded = true;
|
||||||
resolve(true);
|
resolve(true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const loadGoogleAuthApi = () => {
|
export const loadGoogleAuthApi = () => {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
if (typeof google === 'undefined') {
|
if (typeof google === 'undefined') {
|
||||||
const script = document.createElement('script');
|
const script = document.createElement('script');
|
||||||
script.src = 'https://accounts.google.com/gsi/client';
|
script.src = 'https://accounts.google.com/gsi/client';
|
||||||
script.onload = resolve;
|
script.onload = resolve;
|
||||||
script.onerror = reject;
|
script.onerror = reject;
|
||||||
document.body.appendChild(script);
|
document.body.appendChild(script);
|
||||||
} else {
|
} else {
|
||||||
resolve(true);
|
resolve(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getAuthToken = async () => {
|
export const getAuthToken = async () => {
|
||||||
if (!oauthToken) {
|
if (!oauthToken) {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const tokenClient = google.accounts.oauth2.initTokenClient({
|
const tokenClient = google.accounts.oauth2.initTokenClient({
|
||||||
client_id: CLIENT_ID,
|
client_id: CLIENT_ID,
|
||||||
scope: SCOPE.join(' '),
|
scope: SCOPE.join(' '),
|
||||||
callback: (response: any) => {
|
callback: (response: any) => {
|
||||||
if (response.access_token) {
|
if (response.access_token) {
|
||||||
oauthToken = response.access_token;
|
oauthToken = response.access_token;
|
||||||
resolve(oauthToken);
|
resolve(oauthToken);
|
||||||
} else {
|
} else {
|
||||||
reject(new Error('Failed to get access token'));
|
reject(new Error('Failed to get access token'));
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
error_callback: (error: any) => {
|
error_callback: (error: any) => {
|
||||||
reject(new Error(error.message || 'OAuth error occurred'));
|
reject(new Error(error.message || 'OAuth error occurred'));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
tokenClient.requestAccessToken();
|
tokenClient.requestAccessToken();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
return oauthToken;
|
return oauthToken;
|
||||||
};
|
};
|
||||||
|
|
||||||
const initialize = async () => {
|
const initialize = async () => {
|
||||||
if (!initialized) {
|
if (!initialized) {
|
||||||
validateCredentials();
|
validateCredentials();
|
||||||
await Promise.all([loadGoogleDriveApi(), loadGoogleAuthApi()]);
|
await Promise.all([loadGoogleDriveApi(), loadGoogleAuthApi()]);
|
||||||
initialized = true;
|
initialized = true;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createPicker = () => {
|
export const createPicker = () => {
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
try {
|
try {
|
||||||
console.log('Initializing Google Drive Picker...');
|
console.log('Initializing Google Drive Picker...');
|
||||||
await initialize();
|
await initialize();
|
||||||
console.log('Getting auth token...');
|
console.log('Getting auth token...');
|
||||||
const token = await getAuthToken();
|
const token = await getAuthToken();
|
||||||
if (!token) {
|
if (!token) {
|
||||||
console.error('Failed to get OAuth token');
|
console.error('Failed to get OAuth token');
|
||||||
throw new Error('Unable to get OAuth token');
|
throw new Error('Unable to get OAuth token');
|
||||||
}
|
}
|
||||||
console.log('Auth token obtained successfully');
|
console.log('Auth token obtained successfully');
|
||||||
|
|
||||||
const picker = new google.picker.PickerBuilder()
|
const picker = new google.picker.PickerBuilder()
|
||||||
.enableFeature(google.picker.Feature.NAV_HIDDEN)
|
.enableFeature(google.picker.Feature.NAV_HIDDEN)
|
||||||
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
|
.enableFeature(google.picker.Feature.MULTISELECT_ENABLED)
|
||||||
.addView(new google.picker.DocsView()
|
.addView(
|
||||||
.setIncludeFolders(false)
|
new google.picker.DocsView()
|
||||||
.setSelectFolderEnabled(false)
|
.setIncludeFolders(false)
|
||||||
.setMimeTypes('application/pdf,text/plain,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.google-apps.document'))
|
.setSelectFolderEnabled(false)
|
||||||
.setOAuthToken(token)
|
.setMimeTypes(
|
||||||
.setDeveloperKey(API_KEY)
|
'application/pdf,text/plain,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/vnd.google-apps.document'
|
||||||
// Remove app ID setting as it's not needed and can cause 404 errors
|
)
|
||||||
.setCallback(async (data: any) => {
|
)
|
||||||
if (data[google.picker.Response.ACTION] === google.picker.Action.PICKED) {
|
.setOAuthToken(token)
|
||||||
try {
|
.setDeveloperKey(API_KEY)
|
||||||
const doc = data[google.picker.Response.DOCUMENTS][0];
|
// Remove app ID setting as it's not needed and can cause 404 errors
|
||||||
const fileId = doc[google.picker.Document.ID];
|
.setCallback(async (data: any) => {
|
||||||
const fileName = doc[google.picker.Document.NAME];
|
if (data[google.picker.Response.ACTION] === google.picker.Action.PICKED) {
|
||||||
const fileUrl = doc[google.picker.Document.URL];
|
try {
|
||||||
|
const doc = data[google.picker.Response.DOCUMENTS][0];
|
||||||
if (!fileId || !fileName) {
|
const fileId = doc[google.picker.Document.ID];
|
||||||
throw new Error('Required file details missing');
|
const fileName = doc[google.picker.Document.NAME];
|
||||||
}
|
const fileUrl = doc[google.picker.Document.URL];
|
||||||
|
|
||||||
// Construct download URL based on MIME type
|
|
||||||
const mimeType = doc[google.picker.Document.MIME_TYPE];
|
|
||||||
|
|
||||||
let downloadUrl;
|
if (!fileId || !fileName) {
|
||||||
let exportFormat;
|
throw new Error('Required file details missing');
|
||||||
|
}
|
||||||
if (mimeType.includes('google-apps')) {
|
|
||||||
// Handle Google Workspace files
|
|
||||||
if (mimeType.includes('document')) {
|
|
||||||
exportFormat = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
|
||||||
} else if (mimeType.includes('presentation')) {
|
|
||||||
exportFormat = 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
|
|
||||||
} else {
|
|
||||||
exportFormat = 'application/pdf';
|
|
||||||
}
|
|
||||||
downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}/export?mimeType=${encodeURIComponent(exportFormat)}`;
|
|
||||||
} else {
|
|
||||||
// Regular files use direct download URL
|
|
||||||
downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`;
|
|
||||||
}
|
|
||||||
// Create a Blob from the file download
|
|
||||||
const response = await fetch(downloadUrl, {
|
|
||||||
headers: {
|
|
||||||
'Authorization': `Bearer ${token}`,
|
|
||||||
'Accept': '*/*'
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
// Construct download URL based on MIME type
|
||||||
const errorText = await response.text();
|
const mimeType = doc[google.picker.Document.MIME_TYPE];
|
||||||
console.error('Download failed:', {
|
|
||||||
status: response.status,
|
|
||||||
statusText: response.statusText,
|
|
||||||
error: errorText
|
|
||||||
});
|
|
||||||
throw new Error(`Failed to download file (${response.status}): ${errorText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const blob = await response.blob();
|
let downloadUrl;
|
||||||
const result = {
|
let exportFormat;
|
||||||
id: fileId,
|
|
||||||
name: fileName,
|
if (mimeType.includes('google-apps')) {
|
||||||
url: downloadUrl,
|
// Handle Google Workspace files
|
||||||
blob: blob,
|
if (mimeType.includes('document')) {
|
||||||
headers: {
|
exportFormat =
|
||||||
'Authorization': `Bearer ${token}`,
|
'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
|
||||||
'Accept': '*/*'
|
} else if (mimeType.includes('presentation')) {
|
||||||
}
|
exportFormat =
|
||||||
};
|
'application/vnd.openxmlformats-officedocument.presentationml.presentation';
|
||||||
resolve(result);
|
} else {
|
||||||
} catch (error) {
|
exportFormat = 'application/pdf';
|
||||||
reject(error);
|
}
|
||||||
}
|
downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}/export?mimeType=${encodeURIComponent(exportFormat)}`;
|
||||||
} else if (data[google.picker.Response.ACTION] === google.picker.Action.CANCEL) {
|
} else {
|
||||||
resolve(null);
|
// Regular files use direct download URL
|
||||||
}
|
downloadUrl = `https://www.googleapis.com/drive/v3/files/${fileId}?alt=media`;
|
||||||
})
|
}
|
||||||
.build();
|
// Create a Blob from the file download
|
||||||
picker.setVisible(true);
|
const response = await fetch(downloadUrl, {
|
||||||
} catch (error) {
|
headers: {
|
||||||
console.error('Google Drive Picker error:', error);
|
Authorization: `Bearer ${token}`,
|
||||||
reject(error);
|
Accept: '*/*'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text();
|
||||||
|
console.error('Download failed:', {
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
error: errorText
|
||||||
|
});
|
||||||
|
throw new Error(`Failed to download file (${response.status}): ${errorText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const blob = await response.blob();
|
||||||
|
const result = {
|
||||||
|
id: fileId,
|
||||||
|
name: fileName,
|
||||||
|
url: downloadUrl,
|
||||||
|
blob: blob,
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
Accept: '*/*'
|
||||||
|
}
|
||||||
|
};
|
||||||
|
resolve(result);
|
||||||
|
} catch (error) {
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
} else if (data[google.picker.Response.ACTION] === google.picker.Action.CANCEL) {
|
||||||
|
resolve(null);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.build();
|
||||||
|
picker.setVisible(true);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Google Drive Picker error:', error);
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user