carnet api nc
This commit is contained in:
parent
b40fbc6c98
commit
d28940adc5
@ -51,27 +51,38 @@ async function parseXMLResponse(response: Response): Promise<any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function createFolder(nextcloudUrl: string, username: string, password: string, folderPath: string) {
|
async function createFolder(nextcloudUrl: string, username: string, password: string, folderPath: string) {
|
||||||
const response = await fetch(`${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(username)}/${encodeURIComponent(folderPath)}`, {
|
try {
|
||||||
method: 'MKCOL',
|
const response = await fetch(`${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(username)}/${encodeURIComponent(folderPath)}`, {
|
||||||
headers: {
|
method: 'MKCOL',
|
||||||
'Authorization': `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`,
|
headers: {
|
||||||
},
|
'Authorization': `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`,
|
||||||
});
|
},
|
||||||
|
});
|
||||||
|
|
||||||
if (!response.ok && response.status !== 405) { // 405 means folder already exists
|
if (!response.ok && response.status !== 405) { // 405 means folder already exists
|
||||||
throw new Error(`Failed to create folder ${folderPath}: ${response.status} ${response.statusText}`);
|
const errorText = await response.text();
|
||||||
|
console.error(`Failed to create folder ${folderPath}:`, {
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
error: errorText
|
||||||
|
});
|
||||||
|
throw new Error(`Failed to create folder ${folderPath}: ${response.status} ${response.statusText}`);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`Error creating folder ${folderPath}:`, error);
|
||||||
|
throw error;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function ensureFolderStructure(nextcloudUrl: string, username: string, password: string) {
|
async function ensureFolderStructure(nextcloudUrl: string, username: string, password: string) {
|
||||||
try {
|
try {
|
||||||
// Create Private folder if it doesn't exist
|
// First, ensure the Private folder exists
|
||||||
await createFolder(nextcloudUrl, username, password, 'Private');
|
await createFolder(nextcloudUrl, username, password, 'Private');
|
||||||
|
|
||||||
// Create Diary folder if it doesn't exist
|
// Then create Diary folder inside Private
|
||||||
await createFolder(nextcloudUrl, username, password, 'Private/Diary');
|
await createFolder(nextcloudUrl, username, password, 'Private/Diary');
|
||||||
|
|
||||||
// Create Health folder if it doesn't exist
|
// Finally create Health folder inside Private
|
||||||
await createFolder(nextcloudUrl, username, password, 'Private/Health');
|
await createFolder(nextcloudUrl, username, password, 'Private/Health');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error creating folder structure:', error);
|
console.error('Error creating folder structure:', error);
|
||||||
@ -99,12 +110,17 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (verifyResponse.ok) {
|
if (verifyResponse.ok) {
|
||||||
// Ensure required folders exist
|
try {
|
||||||
await ensureFolderStructure(nextcloudUrl, username, credentials.password);
|
// Ensure required folders exist
|
||||||
return credentials.password;
|
await ensureFolderStructure(nextcloudUrl, username, credentials.password);
|
||||||
|
return credentials.password;
|
||||||
|
} catch (folderError) {
|
||||||
|
console.error('Failed to create folders with existing credentials:', folderError);
|
||||||
|
// If folder creation fails, we'll try with new credentials
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// If verification failed, delete the old credentials
|
// If verification failed or folder creation failed, delete the old credentials
|
||||||
await prisma.webDAVCredentials.delete({
|
await prisma.webDAVCredentials.delete({
|
||||||
where: { userId },
|
where: { userId },
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user