mirror of
https://github.com/open-webui/open-webui
synced 2025-06-26 18:26:48 +00:00
refactor
This commit is contained in:
parent
2d7062fc99
commit
55d077b52a
@ -5,7 +5,6 @@ import { v4 as uuidv4 } from 'uuid';
|
|||||||
class OneDriveConfig {
|
class OneDriveConfig {
|
||||||
private static instance: OneDriveConfig;
|
private static instance: OneDriveConfig;
|
||||||
private clientId: string = '';
|
private clientId: string = '';
|
||||||
private authorityType: 'personal' | 'organizations' = 'personal';
|
|
||||||
private sharepointUrl: string = '';
|
private sharepointUrl: string = '';
|
||||||
private msalInstance: PublicClientApplication | null = null;
|
private msalInstance: PublicClientApplication | null = null;
|
||||||
private currentAuthorityType: 'personal' | 'organizations' = 'personal';
|
private currentAuthorityType: 'personal' | 'organizations' = 'personal';
|
||||||
@ -21,7 +20,6 @@ class OneDriveConfig {
|
|||||||
|
|
||||||
public async initialize(authorityType?: 'personal' | 'organizations'): Promise<void> {
|
public async initialize(authorityType?: 'personal' | 'organizations'): Promise<void> {
|
||||||
if (authorityType && this.currentAuthorityType !== authorityType) {
|
if (authorityType && this.currentAuthorityType !== authorityType) {
|
||||||
console.log('Authority type changed, resetting msalInstance');
|
|
||||||
this.currentAuthorityType = authorityType;
|
this.currentAuthorityType = authorityType;
|
||||||
this.msalInstance = null;
|
this.msalInstance = null;
|
||||||
}
|
}
|
||||||
@ -32,7 +30,7 @@ class OneDriveConfig {
|
|||||||
await this.initialize(authorityType);
|
await this.initialize(authorityType);
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getCredentials(selectedAuthorityType?: 'personal' | 'organizations'): Promise<void> {
|
private async getCredentials(): Promise<void> {
|
||||||
let response;
|
let response;
|
||||||
const headers: HeadersInit = {
|
const headers: HeadersInit = {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
@ -62,7 +60,7 @@ class OneDriveConfig {
|
|||||||
if (!newClientId) {
|
if (!newClientId) {
|
||||||
throw new Error('OneDrive configuration is incomplete');
|
throw new Error('OneDrive configuration is incomplete');
|
||||||
}
|
}
|
||||||
|
|
||||||
this.clientId = newClientId;
|
this.clientId = newClientId;
|
||||||
this.sharepointUrl = newSharepointUrl;
|
this.sharepointUrl = newSharepointUrl;
|
||||||
}
|
}
|
||||||
@ -123,8 +121,6 @@ async function getToken(resource?: string, authorityType?: 'personal' | 'organiz
|
|||||||
const scopes = currentAuthorityType === 'organizations'
|
const scopes = currentAuthorityType === 'organizations'
|
||||||
? [`${resource || config.getBaseUrl()}/.default`]
|
? [`${resource || config.getBaseUrl()}/.default`]
|
||||||
: ['OneDrive.ReadWrite'];
|
: ['OneDrive.ReadWrite'];
|
||||||
|
|
||||||
console.log('scopes', scopes);
|
|
||||||
|
|
||||||
const authParams: PopupRequest = { scopes };
|
const authParams: PopupRequest = { scopes };
|
||||||
let accessToken = '';
|
let accessToken = '';
|
||||||
@ -157,8 +153,7 @@ async function getToken(resource?: string, authorityType?: 'personal' | 'organiz
|
|||||||
return accessToken;
|
return accessToken;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get picker parameters based on account type
|
interface PickerParams {
|
||||||
function getPickerParams(): {
|
|
||||||
sdk: string;
|
sdk: string;
|
||||||
entry: {
|
entry: {
|
||||||
oneDrive: Record<string, unknown>;
|
oneDrive: Record<string, unknown>;
|
||||||
@ -172,56 +167,58 @@ function getPickerParams(): {
|
|||||||
mode: string;
|
mode: string;
|
||||||
pivots: Record<string, boolean>;
|
pivots: Record<string, boolean>;
|
||||||
};
|
};
|
||||||
} {
|
}
|
||||||
|
|
||||||
|
interface PickerResult {
|
||||||
|
command?: string;
|
||||||
|
items?: OneDriveFileInfo[];
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get picker parameters based on account type
|
||||||
|
function getPickerParams(): PickerParams {
|
||||||
const channelId = uuidv4();
|
const channelId = uuidv4();
|
||||||
|
const config = OneDriveConfig.getInstance();
|
||||||
|
|
||||||
if (OneDriveConfig.getInstance().getAuthorityType() === 'organizations') {
|
const params: PickerParams = {
|
||||||
// Parameters for OneDrive for Business
|
sdk: '8.0',
|
||||||
return {
|
entry: {
|
||||||
sdk: '8.0',
|
oneDrive: {}
|
||||||
entry: {
|
},
|
||||||
oneDrive: {}
|
authentication: {},
|
||||||
},
|
messaging: {
|
||||||
authentication: {},
|
origin: window?.location?.origin || '',
|
||||||
messaging: {
|
channelId
|
||||||
origin: window?.location?.origin || '',
|
},
|
||||||
channelId
|
typesAndSources: {
|
||||||
},
|
mode: 'files',
|
||||||
typesAndSources: {
|
pivots: {
|
||||||
mode: 'files',
|
oneDrive: true,
|
||||||
pivots: {
|
recent: true
|
||||||
oneDrive: true,
|
|
||||||
recent: true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
} else {
|
};
|
||||||
// Parameters for personal OneDrive
|
|
||||||
return {
|
// For personal accounts, set files object in oneDrive
|
||||||
sdk: '8.0',
|
if (config.getAuthorityType() !== 'organizations') {
|
||||||
entry: {
|
params.entry.oneDrive = { files: {} };
|
||||||
oneDrive: {
|
|
||||||
files: {}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
authentication: {},
|
|
||||||
messaging: {
|
|
||||||
origin: window?.location?.origin || '',
|
|
||||||
channelId
|
|
||||||
},
|
|
||||||
typesAndSources: {
|
|
||||||
mode: 'files',
|
|
||||||
pivots: {
|
|
||||||
oneDrive: true,
|
|
||||||
recent: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface OneDriveFileInfo {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
parentReference: {
|
||||||
|
driveId: string;
|
||||||
|
};
|
||||||
|
'@sharePoint.endpoint': string;
|
||||||
|
[key: string]: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Download file from OneDrive
|
// Download file from OneDrive
|
||||||
async function downloadOneDriveFile(fileInfo: Record<string, any>, authorityType?: 'personal' | 'organizations'): Promise<Blob> {
|
async function downloadOneDriveFile(fileInfo: OneDriveFileInfo, authorityType?: 'personal' | 'organizations'): Promise<Blob> {
|
||||||
const accessToken = await getToken(undefined, authorityType);
|
const accessToken = await getToken(undefined, authorityType);
|
||||||
if (!accessToken) {
|
if (!accessToken) {
|
||||||
throw new Error('Unable to retrieve OneDrive access token.');
|
throw new Error('Unable to retrieve OneDrive access token.');
|
||||||
@ -237,43 +234,34 @@ async function downloadOneDriveFile(fileInfo: Record<string, any>, authorityType
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Failed to fetch file information.');
|
throw new Error(`Failed to fetch file information: ${response.status} ${response.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const fileData = await response.json();
|
const fileData = await response.json();
|
||||||
const downloadUrl = fileData['@content.downloadUrl'];
|
const downloadUrl = fileData['@content.downloadUrl'];
|
||||||
|
|
||||||
|
if (!downloadUrl) {
|
||||||
|
throw new Error('Download URL not found in file data');
|
||||||
|
}
|
||||||
|
|
||||||
const downloadResponse = await fetch(downloadUrl);
|
const downloadResponse = await fetch(downloadUrl);
|
||||||
|
|
||||||
if (!downloadResponse.ok) {
|
if (!downloadResponse.ok) {
|
||||||
throw new Error('Failed to download file.');
|
throw new Error(`Failed to download file: ${downloadResponse.status} ${downloadResponse.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return await downloadResponse.blob();
|
return await downloadResponse.blob();
|
||||||
}
|
}
|
||||||
|
|
||||||
interface PickerResult {
|
|
||||||
items?: Array<{
|
|
||||||
id: string;
|
|
||||||
name: string;
|
|
||||||
parentReference: {
|
|
||||||
driveId: string;
|
|
||||||
};
|
|
||||||
'@sharePoint.endpoint': string;
|
|
||||||
[key: string]: any;
|
|
||||||
}>;
|
|
||||||
command?: string;
|
|
||||||
[key: string]: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Open OneDrive file picker and return selected file metadata
|
// Open OneDrive file picker and return selected file metadata
|
||||||
export async function openOneDrivePicker(): Promise<PickerResult | null> {
|
export async function openOneDrivePicker(authorityType?: 'personal' | 'organizations'): Promise<PickerResult | null> {
|
||||||
if (typeof window === 'undefined') {
|
if (typeof window === 'undefined') {
|
||||||
throw new Error('Not in browser environment');
|
throw new Error('Not in browser environment');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Force reinitialization of OneDrive config
|
// Initialize OneDrive config with the specified authority type
|
||||||
const config = OneDriveConfig.getInstance();
|
const config = OneDriveConfig.getInstance();
|
||||||
await config.initialize();
|
await config.initialize(authorityType);
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let pickerWindow: Window | null = null;
|
let pickerWindow: Window | null = null;
|
||||||
@ -305,8 +293,8 @@ export async function openOneDrivePicker(): Promise<PickerResult | null> {
|
|||||||
case 'authenticate': {
|
case 'authenticate': {
|
||||||
try {
|
try {
|
||||||
// Pass the resource from the command for org accounts
|
// Pass the resource from the command for org accounts
|
||||||
const resource = OneDriveConfig.getInstance().getAuthorityType() === 'organizations' ? command.resource : undefined;
|
const resource = config.getAuthorityType() === 'organizations' ? command.resource : undefined;
|
||||||
const newToken = await getToken(resource);
|
const newToken = await getToken(resource, authorityType);
|
||||||
if (newToken) {
|
if (newToken) {
|
||||||
channelPort?.postMessage({
|
channelPort?.postMessage({
|
||||||
type: 'result',
|
type: 'result',
|
||||||
@ -370,7 +358,7 @@ export async function openOneDrivePicker(): Promise<PickerResult | null> {
|
|||||||
|
|
||||||
const initializePicker = async () => {
|
const initializePicker = async () => {
|
||||||
try {
|
try {
|
||||||
const authToken = await getToken();
|
const authToken = await getToken(undefined, authorityType);
|
||||||
if (!authToken) {
|
if (!authToken) {
|
||||||
return reject(new Error('Failed to acquire access token'));
|
return reject(new Error('Failed to acquire access token'));
|
||||||
}
|
}
|
||||||
@ -385,13 +373,12 @@ export async function openOneDrivePicker(): Promise<PickerResult | null> {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let url = '';
|
let url = '';
|
||||||
if(OneDriveConfig.getInstance().getAuthorityType() === 'organizations') {
|
if(config.getAuthorityType() === 'organizations') {
|
||||||
url = baseUrl + `/_layouts/15/FilePicker.aspx?${queryString}`;
|
url = baseUrl + `/_layouts/15/FilePicker.aspx?${queryString}`;
|
||||||
}else{
|
} else {
|
||||||
url = baseUrl + `?${queryString}`;
|
url = baseUrl + `?${queryString}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const form = pickerWindow.document.createElement('form');
|
const form = pickerWindow.document.createElement('form');
|
||||||
form.setAttribute('action', url);
|
form.setAttribute('action', url);
|
||||||
form.setAttribute('method', 'POST');
|
form.setAttribute('method', 'POST');
|
||||||
@ -419,11 +406,7 @@ export async function openOneDrivePicker(): Promise<PickerResult | null> {
|
|||||||
|
|
||||||
// Pick and download file from OneDrive
|
// Pick and download file from OneDrive
|
||||||
export async function pickAndDownloadFile(authorityType?: 'personal' | 'organizations'): Promise<{ blob: Blob; name: string } | null> {
|
export async function pickAndDownloadFile(authorityType?: 'personal' | 'organizations'): Promise<{ blob: Blob; name: string } | null> {
|
||||||
// Force reinitialization with selected authority type
|
const pickerResult = await openOneDrivePicker(authorityType);
|
||||||
const config = OneDriveConfig.getInstance();
|
|
||||||
await config.initialize(authorityType);
|
|
||||||
|
|
||||||
const pickerResult = await openOneDrivePicker();
|
|
||||||
|
|
||||||
if (!pickerResult || !pickerResult.items || pickerResult.items.length === 0) {
|
if (!pickerResult || !pickerResult.items || pickerResult.items.length === 0) {
|
||||||
return null;
|
return null;
|
||||||
|
Loading…
Reference in New Issue
Block a user