import { WebDAV } from 'webdav'; import { getSession } from 'next-auth/react'; export class NextCloudService { private webdav: WebDAV; private basePath: string = '/Personal/Carnet'; private subFolders: string[] = ['Journal', 'Santé', 'Notes']; constructor(token: string) { this.webdav = new WebDAV( process.env.NEXTCLOUD_URL + '/remote.php/dav/files', { headers: { Authorization: `Bearer ${token}`, }, } ); } async initializeUserFolders(username: string) { const userBasePath = `${username}${this.basePath}`; // Create base Carnet folder try { const baseExists = await this.webdav.exists(userBasePath); if (!baseExists) { await this.webdav.createDirectory(userBasePath, { recursive: true }); } } catch (error) { console.error('Error checking/creating base folder:', error); throw new Error('Failed to initialize base folder'); } // Create subfolders for (const folder of this.subFolders) { const folderPath = `${userBasePath}/${folder}`; try { const exists = await this.webdav.exists(folderPath); if (!exists) { await this.webdav.createDirectory(folderPath, { recursive: true }); } } catch (error) { console.error(`Error checking/creating ${folder} folder:`, error); throw new Error(`Failed to initialize ${folder} folder`); } } } async saveNote(username: string, content: string, category: string = 'Notes', date: Date = new Date()) { if (!this.subFolders.includes(category)) { throw new Error(`Invalid category: ${category}`); } const fileName = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}.md`; const filePath = `${username}${this.basePath}/${category}/${fileName}`; await this.initializeUserFolders(username); await this.webdav.putFileContents(filePath, content, { overwrite: true }); return { fileName, category }; } async getNote(username: string, category: string, date: Date) { if (!this.subFolders.includes(category)) { throw new Error(`Invalid category: ${category}`); } const fileName = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}.md`; const filePath = `${username}${this.basePath}/${category}/${fileName}`; try { const content = await this.webdav.getFileContents(filePath, { format: 'text' }); return content; } catch (error) { if (error.response?.status === 404) { return null; } throw error; } } async listNotes(username: string, category?: string) { await this.initializeUserFolders(username); const userPath = `${username}${this.basePath}`; const results = []; try { if (category) { if (!this.subFolders.includes(category)) { throw new Error(`Invalid category: ${category}`); } const folderPath = `${userPath}/${category}`; const files = await this.webdav.getDirectoryContents(folderPath); results.push(...this.processFiles(files, category)); } else { for (const folder of this.subFolders) { const folderPath = `${userPath}/${folder}`; try { const files = await this.webdav.getDirectoryContents(folderPath); results.push(...this.processFiles(files, folder)); } catch (error) { if (error.response?.status !== 404) { throw error; } } } } return results; } catch (error) { if (error.response?.status === 404) { return []; } throw error; } } private processFiles(files: any[], category: string) { return files .filter(file => file.basename.endsWith('.md')) .map(file => ({ date: this.fileNameToDate(file.basename), name: file.basename, path: file.filename, category })); } private fileNameToDate(fileName: string): Date { const [year, month, day] = fileName.split('.')[0].split('-').map(Number); return new Date(year, month - 1, day); } }