carnet api
This commit is contained in:
parent
01ba7f754a
commit
f7cbd3e2b3
@ -8,10 +8,30 @@ export async function getNextCloudService() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Get the NextCloud token from the session
|
// Get the NextCloud token from the session
|
||||||
const token = session.accessToken;
|
const token = session.nextcloudToken;
|
||||||
if (!token) {
|
if (!token) {
|
||||||
|
console.error('Session details:', {
|
||||||
|
hasSession: !!session,
|
||||||
|
hasUser: !!session?.user,
|
||||||
|
email: session?.user?.email,
|
||||||
|
sessionKeys: Object.keys(session || {}),
|
||||||
|
// Don't log sensitive data
|
||||||
|
tokenPresent: !!token,
|
||||||
|
hasNextCloudToken: 'nextcloudToken' in session
|
||||||
|
});
|
||||||
throw new Error('No NextCloud token available');
|
throw new Error('No NextCloud token available');
|
||||||
}
|
}
|
||||||
|
|
||||||
return new NextCloudService(token);
|
try {
|
||||||
|
console.log('Initializing NextCloud service with token length:', token.length);
|
||||||
|
const service = new NextCloudService(token);
|
||||||
|
return service;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Failed to initialize NextCloud service:', {
|
||||||
|
error: error instanceof Error ? error.message : error,
|
||||||
|
hasToken: !!token,
|
||||||
|
tokenLength: token.length
|
||||||
|
});
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
115
lib/nextcloud.ts
115
lib/nextcloud.ts
@ -7,9 +7,22 @@ export class NextCloudService {
|
|||||||
private subFolders: string[] = ['Journal', 'Santé', 'Notes'];
|
private subFolders: string[] = ['Journal', 'Santé', 'Notes'];
|
||||||
|
|
||||||
constructor(token: string) {
|
constructor(token: string) {
|
||||||
console.log('Initializing NextCloudService with URL:', process.env.NEXTCLOUD_URL);
|
const nextcloudUrl = process.env.NEXTCLOUD_URL;
|
||||||
|
console.log('NextCloud Configuration:', {
|
||||||
|
baseUrl: nextcloudUrl,
|
||||||
|
tokenLength: token?.length || 0,
|
||||||
|
hasToken: !!token
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!nextcloudUrl) {
|
||||||
|
throw new Error('NEXTCLOUD_URL environment variable is not set');
|
||||||
|
}
|
||||||
|
|
||||||
|
const webdavUrl = `${nextcloudUrl}/remote.php/dav/files`;
|
||||||
|
console.log('WebDAV endpoint:', webdavUrl);
|
||||||
|
|
||||||
this.webdav = new WebDAV(
|
this.webdav = new WebDAV(
|
||||||
process.env.NEXTCLOUD_URL + '/remote.php/dav/files',
|
webdavUrl,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
Authorization: `Bearer ${token}`,
|
Authorization: `Bearer ${token}`,
|
||||||
@ -18,56 +31,119 @@ export class NextCloudService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async testConnection() {
|
||||||
|
try {
|
||||||
|
console.log('Testing WebDAV connection...');
|
||||||
|
// Try to list root directory
|
||||||
|
const rootContents = await this.webdav.getDirectoryContents('/');
|
||||||
|
console.log('WebDAV connection successful, root directory contents:', rootContents);
|
||||||
|
return true;
|
||||||
|
} catch (error) {
|
||||||
|
console.error('WebDAV connection test failed:', {
|
||||||
|
error: error instanceof Error ? error.message : error,
|
||||||
|
response: error.response?.data,
|
||||||
|
status: error.response?.status,
|
||||||
|
headers: error.response?.headers
|
||||||
|
});
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async initializeUserFolders(username: string) {
|
async initializeUserFolders(username: string) {
|
||||||
console.log(`Initializing folders for user: ${username}`);
|
console.log('=== Starting folder initialization ===');
|
||||||
|
console.log(`User: ${username}`);
|
||||||
|
|
||||||
|
// Test connection first
|
||||||
|
const connectionOk = await this.testConnection();
|
||||||
|
if (!connectionOk) {
|
||||||
|
throw new Error('Failed to connect to NextCloud WebDAV service');
|
||||||
|
}
|
||||||
|
|
||||||
const userBasePath = `${username}${this.basePath}`;
|
const userBasePath = `${username}${this.basePath}`;
|
||||||
console.log('Base path:', userBasePath);
|
console.log('Target base path:', userBasePath);
|
||||||
|
|
||||||
// Create base Carnet folder
|
// Create base Carnet folder
|
||||||
try {
|
try {
|
||||||
console.log('Checking if base folder exists:', userBasePath);
|
console.log('Checking base folder existence...');
|
||||||
const baseExists = await this.webdav.exists(userBasePath);
|
const baseExists = await this.webdav.exists(userBasePath);
|
||||||
console.log('Base folder exists:', baseExists);
|
console.log('Base folder check result:', baseExists);
|
||||||
|
|
||||||
if (!baseExists) {
|
if (!baseExists) {
|
||||||
console.log('Creating base folder:', userBasePath);
|
console.log('Creating base folder structure...');
|
||||||
|
try {
|
||||||
await this.webdav.createDirectory(userBasePath, { recursive: true });
|
await this.webdav.createDirectory(userBasePath, { recursive: true });
|
||||||
console.log('Base folder created successfully');
|
console.log('Base folder created successfully');
|
||||||
|
} catch (createError) {
|
||||||
|
console.error('Failed to create base folder:', {
|
||||||
|
error: createError instanceof Error ? createError.message : createError,
|
||||||
|
response: createError.response?.data,
|
||||||
|
status: createError.response?.status
|
||||||
|
});
|
||||||
|
throw createError;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error checking/creating base folder:', {
|
console.error('Error in base folder operation:', {
|
||||||
path: userBasePath,
|
path: userBasePath,
|
||||||
error: error instanceof Error ? error.message : error,
|
error: error instanceof Error ? error.message : error,
|
||||||
fullError: error
|
response: error.response?.data,
|
||||||
|
status: error.response?.status,
|
||||||
|
headers: error.response?.headers
|
||||||
});
|
});
|
||||||
throw new Error('Failed to initialize base folder');
|
throw new Error(`Failed to initialize base folder: ${error.message || 'Unknown error'}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create subfolders
|
// Create subfolders
|
||||||
for (const folder of this.subFolders) {
|
for (const folder of this.subFolders) {
|
||||||
const folderPath = `${userBasePath}/${folder}`;
|
const folderPath = `${userBasePath}/${folder}`;
|
||||||
console.log(`Processing subfolder: ${folder}`);
|
console.log(`\nProcessing subfolder: ${folder}`);
|
||||||
|
console.log('Target path:', folderPath);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
console.log('Checking if subfolder exists:', folderPath);
|
console.log(`Checking if ${folder} exists...`);
|
||||||
const exists = await this.webdav.exists(folderPath);
|
const exists = await this.webdav.exists(folderPath);
|
||||||
console.log(`Subfolder ${folder} exists:`, exists);
|
console.log(`${folder} existence check result:`, exists);
|
||||||
|
|
||||||
if (!exists) {
|
if (!exists) {
|
||||||
console.log('Creating subfolder:', folderPath);
|
console.log(`Creating ${folder} folder...`);
|
||||||
|
try {
|
||||||
await this.webdav.createDirectory(folderPath, { recursive: true });
|
await this.webdav.createDirectory(folderPath, { recursive: true });
|
||||||
console.log(`Subfolder ${folder} created successfully`);
|
console.log(`${folder} folder created successfully`);
|
||||||
|
} catch (createError) {
|
||||||
|
console.error(`Failed to create ${folder} folder:`, {
|
||||||
|
error: createError instanceof Error ? createError.message : createError,
|
||||||
|
response: createError.response?.data,
|
||||||
|
status: createError.response?.status
|
||||||
|
});
|
||||||
|
throw createError;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(`Error checking/creating ${folder} folder:`, {
|
console.error(`Error processing ${folder} folder:`, {
|
||||||
path: folderPath,
|
path: folderPath,
|
||||||
error: error instanceof Error ? error.message : error,
|
error: error instanceof Error ? error.message : error,
|
||||||
fullError: error
|
response: error.response?.data,
|
||||||
|
status: error.response?.status,
|
||||||
|
headers: error.response?.headers
|
||||||
});
|
});
|
||||||
throw new Error(`Failed to initialize ${folder} folder`);
|
throw new Error(`Failed to initialize ${folder} folder: ${error.message || 'Unknown error'}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.log('All folders initialized successfully');
|
|
||||||
|
console.log('\n=== Folder initialization completed ===');
|
||||||
|
|
||||||
|
// Verify final structure
|
||||||
|
try {
|
||||||
|
console.log('\nVerifying folder structure...');
|
||||||
|
const contents = await this.webdav.getDirectoryContents(userBasePath, { deep: true });
|
||||||
|
console.log('Final folder structure:', contents);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Error verifying folder structure:', {
|
||||||
|
error: error instanceof Error ? error.message : error,
|
||||||
|
response: error.response?.data,
|
||||||
|
status: error.response?.status
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async saveNote(username: string, content: string, category: string = 'Notes', date: Date = new Date()) {
|
async saveNote(username: string, content: string, category: string = 'Notes', date: Date = new Date()) {
|
||||||
@ -206,3 +282,4 @@ export class NextCloudService {
|
|||||||
return new Date(year, month - 1, day);
|
return new Date(year, month - 1, day);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user