carnet api
This commit is contained in:
parent
3bbb365f53
commit
50d85967ca
@ -15,6 +15,9 @@ export async function GET(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const category = searchParams.get('category') || 'Notes';
|
||||||
|
|
||||||
const service = await getNextCloudService();
|
const service = await getNextCloudService();
|
||||||
const date = new Date(params.date);
|
const date = new Date(params.date);
|
||||||
|
|
||||||
@ -25,7 +28,7 @@ export async function GET(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const content = await service.getNote(session.user.email, date);
|
const content = await service.getNote(session.user.email, category, date);
|
||||||
|
|
||||||
if (!content) {
|
if (!content) {
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
@ -34,7 +37,7 @@ export async function GET(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json({ content });
|
return NextResponse.json({ content, category });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to get note:', error);
|
console.error('Failed to get note:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
@ -12,8 +12,11 @@ export async function GET(request: Request) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const { searchParams } = new URL(request.url);
|
||||||
|
const category = searchParams.get('category');
|
||||||
|
|
||||||
const service = await getNextCloudService();
|
const service = await getNextCloudService();
|
||||||
const notes = await service.listNotes(session.user.email);
|
const notes = await service.listNotes(session.user.email, category || undefined);
|
||||||
|
|
||||||
return NextResponse.json({ notes });
|
return NextResponse.json({ notes });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -36,15 +39,16 @@ export async function POST(request: Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const service = await getNextCloudService();
|
const service = await getNextCloudService();
|
||||||
const { content, date } = await request.json();
|
const { content, date, category = 'Notes' } = await request.json();
|
||||||
|
|
||||||
const fileName = await service.saveNote(
|
const result = await service.saveNote(
|
||||||
session.user.email,
|
session.user.email,
|
||||||
content,
|
content,
|
||||||
|
category,
|
||||||
date ? new Date(date) : undefined
|
date ? new Date(date) : undefined
|
||||||
);
|
);
|
||||||
|
|
||||||
return NextResponse.json({ fileName });
|
return NextResponse.json(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to save note:', error);
|
console.error('Failed to save note:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import { getSession } from 'next-auth/react';
|
|||||||
export class NextCloudService {
|
export class NextCloudService {
|
||||||
private webdav: WebDAV;
|
private webdav: WebDAV;
|
||||||
private basePath: string = '/Personal/Carnet';
|
private basePath: string = '/Personal/Carnet';
|
||||||
|
private subFolders: string[] = ['Journal', 'Santé', 'Notes'];
|
||||||
|
|
||||||
constructor(token: string) {
|
constructor(token: string) {
|
||||||
this.webdav = new WebDAV(
|
this.webdav = new WebDAV(
|
||||||
@ -16,28 +17,56 @@ export class NextCloudService {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
async ensureCarnetFolder(username: string) {
|
async initializeUserFolders(username: string) {
|
||||||
const userPath = `${username}${this.basePath}`;
|
const userBasePath = `${username}${this.basePath}`;
|
||||||
|
|
||||||
|
// Create base Carnet folder
|
||||||
try {
|
try {
|
||||||
await this.webdav.exists(userPath);
|
const baseExists = await this.webdav.exists(userBasePath);
|
||||||
} catch {
|
if (!baseExists) {
|
||||||
await this.webdav.createDirectory(userPath, { recursive: true });
|
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, date: Date = new Date()) {
|
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 fileName = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}.md`;
|
||||||
const filePath = `${username}${this.basePath}/${fileName}`;
|
const filePath = `${username}${this.basePath}/${category}/${fileName}`;
|
||||||
|
|
||||||
await this.ensureCarnetFolder(username);
|
await this.initializeUserFolders(username);
|
||||||
await this.webdav.putFileContents(filePath, content, { overwrite: true });
|
await this.webdav.putFileContents(filePath, content, { overwrite: true });
|
||||||
|
|
||||||
return fileName;
|
return { fileName, category };
|
||||||
}
|
}
|
||||||
|
|
||||||
async getNote(username: string, date: Date) {
|
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 fileName = `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-${String(date.getDate()).padStart(2, '0')}.md`;
|
||||||
const filePath = `${username}${this.basePath}/${fileName}`;
|
const filePath = `${username}${this.basePath}/${category}/${fileName}`;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const content = await this.webdav.getFileContents(filePath, { format: 'text' });
|
const content = await this.webdav.getFileContents(filePath, { format: 'text' });
|
||||||
@ -50,17 +79,34 @@ export class NextCloudService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async listNotes(username: string) {
|
async listNotes(username: string, category?: string) {
|
||||||
|
await this.initializeUserFolders(username);
|
||||||
|
|
||||||
const userPath = `${username}${this.basePath}`;
|
const userPath = `${username}${this.basePath}`;
|
||||||
|
const results = [];
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const files = await this.webdav.getDirectoryContents(userPath);
|
if (category) {
|
||||||
return files
|
if (!this.subFolders.includes(category)) {
|
||||||
.filter(file => file.basename.endsWith('.md'))
|
throw new Error(`Invalid category: ${category}`);
|
||||||
.map(file => ({
|
}
|
||||||
date: this.fileNameToDate(file.basename),
|
const folderPath = `${userPath}/${category}`;
|
||||||
name: file.basename,
|
const files = await this.webdav.getDirectoryContents(folderPath);
|
||||||
path: file.filename
|
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) {
|
} catch (error) {
|
||||||
if (error.response?.status === 404) {
|
if (error.response?.status === 404) {
|
||||||
return [];
|
return [];
|
||||||
@ -69,6 +115,17 @@ export class NextCloudService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
private fileNameToDate(fileName: string): Date {
|
||||||
const [year, month, day] = fileName.split('.')[0].split('-').map(Number);
|
const [year, month, day] = fileName.split('.')[0].split('-').map(Number);
|
||||||
return new Date(year, month - 1, day);
|
return new Date(year, month - 1, day);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user