carnet api
This commit is contained in:
parent
79f83ad97f
commit
d0793dc45a
@ -1,6 +1,6 @@
|
|||||||
import { NextResponse } from 'next/server';
|
import { NextResponse } from 'next/server';
|
||||||
import { getServerSession } from 'next-auth';
|
import { getServerSession } from 'next-auth';
|
||||||
import { authOptions } from '@/lib/auth';
|
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||||
import { DOMParser } from '@xmldom/xmldom';
|
import { DOMParser } from '@xmldom/xmldom';
|
||||||
import { Buffer } from 'buffer';
|
import { Buffer } from 'buffer';
|
||||||
import { PrismaClient } from '@prisma/client';
|
import { PrismaClient } from '@prisma/client';
|
||||||
@ -204,60 +204,122 @@ async function getWebDAVCredentials(nextcloudUrl: string, username: string, admi
|
|||||||
export async function GET() {
|
export async function GET() {
|
||||||
try {
|
try {
|
||||||
const session = await getServerSession(authOptions);
|
const session = await getServerSession(authOptions);
|
||||||
if (!session?.user?.email) {
|
if (!session?.user?.email || !session?.user?.id || !session?.accessToken) {
|
||||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const userId = session.user.email.split('@')[0];
|
|
||||||
const nextcloudUsername = `cube-${userId}`;
|
|
||||||
const nextcloudUrl = process.env.NEXTCLOUD_URL;
|
const nextcloudUrl = process.env.NEXTCLOUD_URL;
|
||||||
const webDAVUrl = `${nextcloudUrl}/remote.php/dav/files/${nextcloudUsername}/Private/`;
|
const adminUsername = process.env.NEXTCLOUD_ADMIN_USERNAME;
|
||||||
|
const adminPassword = process.env.NEXTCLOUD_ADMIN_PASSWORD;
|
||||||
|
|
||||||
// Check cache first
|
if (!nextcloudUrl || !adminUsername || !adminPassword) {
|
||||||
const cacheKey = nextcloudUsername;
|
console.error('Missing Nextcloud configuration');
|
||||||
const cachedData = folderCache.get(cacheKey);
|
return NextResponse.json({ error: 'Nextcloud configuration is missing' }, { status: 500 });
|
||||||
if (cachedData) {
|
|
||||||
const cacheAge = Date.now() - cachedData.timestamp;
|
|
||||||
if (cacheAge < 5 * 60 * 1000) { // 5 minutes cache
|
|
||||||
return NextResponse.json({ folders: cachedData.folders });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetch folder structure
|
// Test Nextcloud connectivity
|
||||||
const response = await fetch(webDAVUrl, {
|
const testResponse = await fetch(`${nextcloudUrl}/status.php`);
|
||||||
headers: {
|
if (!testResponse.ok) {
|
||||||
'Authorization': `Basic ${Buffer.from(`${nextcloudUsername}:${process.env.NEXTCLOUD_ADMIN_PASSWORD}`).toString('base64')}`,
|
console.error('Nextcloud is not accessible:', await testResponse.text());
|
||||||
'Depth': '1'
|
return NextResponse.json({ error: "Nextcloud n'est pas accessible" }, { status: 503 });
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error('Failed to fetch folder structure');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const xmlData = await response.text();
|
try {
|
||||||
const parser = new DOMParser();
|
// Use the Keycloak ID as the Nextcloud username
|
||||||
const doc = parser.parseFromString(xmlData, 'text/xml');
|
const nextcloudUsername = `cube-${session.user.id}`;
|
||||||
const folders: string[] = [];
|
console.log('Using Nextcloud username:', nextcloudUsername);
|
||||||
|
|
||||||
const responses = doc.getElementsByTagName('d:response');
|
// Get or create WebDAV credentials
|
||||||
for (let i = 0; i < responses.length; i++) {
|
const webdavPassword = await getWebDAVCredentials(
|
||||||
const response = responses[i];
|
nextcloudUrl,
|
||||||
const displayName = response.getElementsByTagName('d:displayname')[0]?.textContent;
|
nextcloudUsername,
|
||||||
if (displayName && displayName !== 'Private') {
|
adminUsername,
|
||||||
folders.push(displayName);
|
adminPassword,
|
||||||
|
session.user.id
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!webdavPassword) {
|
||||||
|
throw new Error('Failed to get WebDAV credentials');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Get user's folders using WebDAV with Basic authentication
|
||||||
|
const webdavUrl = `${nextcloudUrl}/remote.php/dav/files/${encodeURIComponent(nextcloudUsername)}/Private/`;
|
||||||
|
console.log('Requesting WebDAV URL:', webdavUrl);
|
||||||
|
|
||||||
|
const foldersResponse = await fetch(webdavUrl, {
|
||||||
|
method: 'PROPFIND',
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Basic ${Buffer.from(`${nextcloudUsername}:${webdavPassword}`).toString('base64')}`,
|
||||||
|
'Depth': '1',
|
||||||
|
'Content-Type': 'application/xml',
|
||||||
|
},
|
||||||
|
body: '<?xml version="1.0" encoding="UTF-8"?><d:propfind xmlns:d="DAV:"><d:prop><d:resourcetype/><d:displayname/></d:prop></d:propfind>',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (foldersResponse.status === 429) {
|
||||||
|
// Rate limited, wait and retry
|
||||||
|
const retryAfter = foldersResponse.headers.get('Retry-After');
|
||||||
|
await sleep((retryAfter ? parseInt(retryAfter) : 5) * 1000);
|
||||||
|
return GET(); // Retry the entire request
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!foldersResponse.ok) {
|
||||||
|
const errorText = await foldersResponse.text();
|
||||||
|
console.error('Failed to fetch folders. Status:', foldersResponse.status);
|
||||||
|
console.error('Response:', errorText);
|
||||||
|
console.error('Response headers:', Object.fromEntries(foldersResponse.headers.entries()));
|
||||||
|
throw new Error(`Failed to fetch folders: ${errorText}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const folderData = await foldersResponse.text();
|
||||||
|
console.log('Folder data:', folderData);
|
||||||
|
|
||||||
|
// Parse the XML response to get folder names
|
||||||
|
const parser = new DOMParser();
|
||||||
|
const xmlDoc = parser.parseFromString(folderData, 'text/xml');
|
||||||
|
const responses = Array.from(xmlDoc.getElementsByTagName('d:response'));
|
||||||
|
|
||||||
|
const folders: string[] = [];
|
||||||
|
for (const response of responses) {
|
||||||
|
const resourceType = response.getElementsByTagName('d:resourcetype')[0];
|
||||||
|
const isCollection = resourceType?.getElementsByTagName('d:collection').length > 0;
|
||||||
|
|
||||||
|
if (isCollection) {
|
||||||
|
const href = response.getElementsByTagName('d:href')[0]?.textContent;
|
||||||
|
if (href) {
|
||||||
|
// Extract folder name from href
|
||||||
|
const folderName = decodeURIComponent(href.split('/').filter(Boolean).pop() || '');
|
||||||
|
if (folderName && folderName !== 'Private') {
|
||||||
|
folders.push(folderName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log('Parsed folders:', folders);
|
||||||
|
|
||||||
|
// Update cache
|
||||||
|
folderCache.set(nextcloudUsername, {
|
||||||
|
folders,
|
||||||
|
timestamp: Date.now()
|
||||||
|
});
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
isConnected: true,
|
||||||
|
folders
|
||||||
|
});
|
||||||
|
} catch (error: any) {
|
||||||
|
console.error('Error accessing Nextcloud WebDAV:', error);
|
||||||
|
return NextResponse.json(
|
||||||
|
{ error: "Erreur d'accès aux dossiers Nextcloud", details: error?.message || String(error) },
|
||||||
|
{ status: 503 }
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
} catch (error: any) {
|
||||||
// Update cache
|
console.error('Error checking Nextcloud status:', error);
|
||||||
folderCache.set(cacheKey, {
|
return NextResponse.json(
|
||||||
folders,
|
{ error: 'Failed to check Nextcloud status', details: error?.message || String(error) },
|
||||||
timestamp: Date.now()
|
{ status: 500 }
|
||||||
});
|
);
|
||||||
|
|
||||||
return NextResponse.json({ folders });
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error in Nextcloud status:', error);
|
|
||||||
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user