carnet api
This commit is contained in:
parent
576b9e7fc9
commit
9b039cb6fd
@ -10,10 +10,18 @@ async function sleep(ms: number) {
|
|||||||
|
|
||||||
async function parseXMLResponse(response: Response): Promise<any> {
|
async function parseXMLResponse(response: Response): Promise<any> {
|
||||||
const text = await response.text();
|
const text = await response.text();
|
||||||
|
console.log('XML Response:', text); // Debug log
|
||||||
|
|
||||||
const parser = new DOMParser();
|
const parser = new DOMParser();
|
||||||
const xmlDoc = parser.parseFromString(text, 'text/xml');
|
const xmlDoc = parser.parseFromString(text, 'text/xml');
|
||||||
|
|
||||||
// Convert XML to a simple object
|
// Check for parsing errors
|
||||||
|
const parserError = xmlDoc.getElementsByTagName('parsererror');
|
||||||
|
if (parserError.length > 0) {
|
||||||
|
console.error('XML Parsing Error:', parserError[0].textContent);
|
||||||
|
throw new Error('Failed to parse XML response');
|
||||||
|
}
|
||||||
|
|
||||||
const result: any = {};
|
const result: any = {};
|
||||||
const root = xmlDoc.documentElement;
|
const root = xmlDoc.documentElement;
|
||||||
|
|
||||||
@ -34,6 +42,7 @@ async function parseXMLResponse(response: Response): Promise<any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function getWebDAVCredentials(nextcloudUrl: string, username: string, adminUsername: string, adminPassword: string) {
|
async function getWebDAVCredentials(nextcloudUrl: string, username: string, adminUsername: string, adminPassword: string) {
|
||||||
|
try {
|
||||||
// First, try to get the user's WebDAV password
|
// First, try to get the user's WebDAV password
|
||||||
const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
|
const userInfoResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
|
||||||
headers: {
|
headers: {
|
||||||
@ -44,14 +53,19 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
|
|||||||
|
|
||||||
if (!userInfoResponse.ok) {
|
if (!userInfoResponse.ok) {
|
||||||
console.error('Failed to get user info:', await userInfoResponse.text());
|
console.error('Failed to get user info:', await userInfoResponse.text());
|
||||||
return null;
|
throw new Error(`Failed to get user info: ${userInfoResponse.status} ${userInfoResponse.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const userInfo = await parseXMLResponse(userInfoResponse);
|
const userInfo = await parseXMLResponse(userInfoResponse);
|
||||||
|
console.log('User Info:', userInfo); // Debug log
|
||||||
|
|
||||||
const webdavPassword = userInfo.webdav_password;
|
const webdavPassword = userInfo.webdav_password;
|
||||||
|
|
||||||
if (!webdavPassword) {
|
if (!webdavPassword) {
|
||||||
// If no WebDAV password exists, create one
|
// If no WebDAV password exists, create one
|
||||||
|
const randomPassword = Math.random().toString(36).slice(-8);
|
||||||
|
console.log('Creating new WebDAV password:', randomPassword); // Debug log
|
||||||
|
|
||||||
const createPasswordResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
|
const createPasswordResponse = await fetch(`${nextcloudUrl}/ocs/v1.php/cloud/users/${encodeURIComponent(username)}`, {
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
headers: {
|
headers: {
|
||||||
@ -61,13 +75,13 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
|
|||||||
},
|
},
|
||||||
body: new URLSearchParams({
|
body: new URLSearchParams({
|
||||||
key: 'webdav_password',
|
key: 'webdav_password',
|
||||||
value: Math.random().toString(36).slice(-8), // Generate a random password
|
value: randomPassword,
|
||||||
}).toString(),
|
}).toString(),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!createPasswordResponse.ok) {
|
if (!createPasswordResponse.ok) {
|
||||||
console.error('Failed to create WebDAV password:', await createPasswordResponse.text());
|
console.error('Failed to create WebDAV password:', await createPasswordResponse.text());
|
||||||
return null;
|
throw new Error(`Failed to create WebDAV password: ${createPasswordResponse.status} ${createPasswordResponse.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the new WebDAV password
|
// Get the new WebDAV password
|
||||||
@ -80,14 +94,24 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
|
|||||||
|
|
||||||
if (!newUserInfoResponse.ok) {
|
if (!newUserInfoResponse.ok) {
|
||||||
console.error('Failed to get new user info:', await newUserInfoResponse.text());
|
console.error('Failed to get new user info:', await newUserInfoResponse.text());
|
||||||
return null;
|
throw new Error(`Failed to get new user info: ${newUserInfoResponse.status} ${newUserInfoResponse.statusText}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const newUserInfo = await parseXMLResponse(newUserInfoResponse);
|
const newUserInfo = await parseXMLResponse(newUserInfoResponse);
|
||||||
|
console.log('New User Info:', newUserInfo); // Debug log
|
||||||
|
|
||||||
|
if (!newUserInfo.webdav_password) {
|
||||||
|
throw new Error('WebDAV password was not set after creation');
|
||||||
|
}
|
||||||
|
|
||||||
return newUserInfo.webdav_password;
|
return newUserInfo.webdav_password;
|
||||||
}
|
}
|
||||||
|
|
||||||
return webdavPassword;
|
return webdavPassword;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error in getWebDAVCredentials:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function GET() {
|
export async function GET() {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user