-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdetectFolderMode.ts
More file actions
50 lines (45 loc) · 1.53 KB
/
detectFolderMode.ts
File metadata and controls
50 lines (45 loc) · 1.53 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
import * as vscode from 'vscode';
import { generateUserAgent } from '../utils/userAgent';
import { isPlaceholderConfig } from './configUtils';
/**
* Detects if the cloud supports dynamic folders by making a request to the root folder API.
* @param cloudName - The cloud name.
* @param apiKey - The API key.
* @param apiSecret - The API secret.
* @returns True if dynamic folders are supported, false otherwise.
*/
export default async function detectFolderMode(
cloudName: string,
apiKey: string,
apiSecret: string
): Promise<boolean> {
if (!cloudName || !apiKey || !apiSecret) {
vscode.window.showErrorMessage("❌ Cloud name, API key, and API secret are required.");
return false;
}
// Don't make API calls with placeholder credentials
if (isPlaceholderConfig(cloudName, apiKey, apiSecret)) {
return false;
}
const authHeader = `Basic ${Buffer.from(`${apiKey}:${apiSecret}`).toString('base64')}`;
const url = `https://api.cloudinary.com/v1_1/${cloudName}/folders`;
try {
const response = await fetch(url, {
headers: {
Authorization: authHeader,
'User-Agent': generateUserAgent(),
},
});
if (response.status === 200) {
return true; // Dynamic folders are supported
} else if (response.status === 420) {
return false; // Dynamic folders are not supported (fixed folder mode)
} else {
// Fallback to fixed folder mode for other errors
return false;
}
} catch (error) {
// Default to fixed folder mode if the request fails
return false;
}
}