carnet panel3
This commit is contained in:
parent
49895eb166
commit
cd58f16e5f
@ -1,72 +0,0 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
import { getServerSession } from 'next-auth';
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import { authOptions } from '@/app/api/auth/[...nextauth]/route';
|
||||
import { createClient } from 'webdav';
|
||||
|
||||
// Use a single PrismaClient instance
|
||||
declare global {
|
||||
var prisma: PrismaClient | undefined;
|
||||
}
|
||||
|
||||
const prisma = global.prisma || new PrismaClient();
|
||||
if (process.env.NODE_ENV !== 'production') global.prisma = prisma;
|
||||
|
||||
export async function GET(
|
||||
request: Request,
|
||||
{ params }: { params: { id: string } }
|
||||
) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions);
|
||||
if (!session?.user?.id) {
|
||||
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
|
||||
}
|
||||
|
||||
// Get WebDAV credentials
|
||||
const credentials = await prisma.webDAVCredentials.findUnique({
|
||||
where: { userId: session.user.id },
|
||||
});
|
||||
|
||||
if (!credentials) {
|
||||
console.error('No WebDAV credentials found for user:', session.user.id);
|
||||
return NextResponse.json({ error: 'No WebDAV credentials found' }, { status: 404 });
|
||||
}
|
||||
|
||||
// Initialize WebDAV client
|
||||
const baseURL = process.env.NEXTCLOUD_URL;
|
||||
if (!baseURL) {
|
||||
throw new Error('NEXTCLOUD_URL environment variable is not set');
|
||||
}
|
||||
|
||||
// Remove trailing slash if present
|
||||
const normalizedBaseURL = baseURL.endsWith('/') ? baseURL.slice(0, -1) : baseURL;
|
||||
|
||||
const client = createClient(`${normalizedBaseURL}/remote.php/dav`, {
|
||||
username: credentials.username,
|
||||
password: credentials.password,
|
||||
authType: 'password',
|
||||
});
|
||||
|
||||
try {
|
||||
// Get the file content
|
||||
const content = await client.getFileContents(params.id);
|
||||
const textContent = content.toString('utf-8');
|
||||
|
||||
return NextResponse.json({ content: textContent });
|
||||
} catch (error) {
|
||||
console.error('Error fetching file content:', error);
|
||||
if (error instanceof Error) {
|
||||
console.error('Error details:', error.message);
|
||||
console.error('Error stack:', error.stack);
|
||||
}
|
||||
return NextResponse.json({ error: 'Failed to fetch file content' }, { status: 500 });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error fetching file:', error);
|
||||
if (error instanceof Error) {
|
||||
console.error('Error details:', error.message);
|
||||
console.error('Error stack:', error.stack);
|
||||
}
|
||||
return NextResponse.json({ error: 'Failed to fetch file' }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@ -6,12 +6,10 @@ import { Image, FileText, Link, List } from 'lucide-react';
|
||||
interface Note {
|
||||
id: string;
|
||||
title: string;
|
||||
lastModified: string;
|
||||
size: number;
|
||||
type: string;
|
||||
mime: string;
|
||||
etag: string;
|
||||
content?: string;
|
||||
content: string;
|
||||
lastEdited: Date;
|
||||
category?: string;
|
||||
tags?: string[];
|
||||
}
|
||||
|
||||
interface EditorProps {
|
||||
@ -22,33 +20,11 @@ interface EditorProps {
|
||||
export const Editor: React.FC<EditorProps> = ({ note, onSave }) => {
|
||||
const [title, setTitle] = useState(note?.title || '');
|
||||
const [content, setContent] = useState(note?.content || '');
|
||||
const [loading, setLoading] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const fetchNoteContent = async () => {
|
||||
if (note?.id) {
|
||||
try {
|
||||
setLoading(true);
|
||||
const response = await fetch(`/api/nextcloud/files/${encodeURIComponent(note.id)}`);
|
||||
if (!response.ok) {
|
||||
throw new Error('Failed to fetch note content');
|
||||
}
|
||||
const data = await response.json();
|
||||
setContent(data.content || '');
|
||||
} catch (err) {
|
||||
console.error('Error fetching note content:', err);
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if (note) {
|
||||
setTitle(note.title);
|
||||
fetchNoteContent();
|
||||
} else {
|
||||
setTitle('');
|
||||
setContent('');
|
||||
setContent(note.content);
|
||||
}
|
||||
}, [note]);
|
||||
|
||||
@ -63,9 +39,12 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave }) => {
|
||||
const handleSave = () => {
|
||||
if (note?.id) {
|
||||
onSave?.({
|
||||
...note,
|
||||
id: note.id,
|
||||
title,
|
||||
content
|
||||
content,
|
||||
lastEdited: new Date(),
|
||||
category: note.category,
|
||||
tags: note.tags
|
||||
});
|
||||
}
|
||||
};
|
||||
@ -106,18 +85,12 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave }) => {
|
||||
|
||||
{/* Editor Area */}
|
||||
<div className="flex-1 p-4">
|
||||
{loading ? (
|
||||
<div className="flex items-center justify-center h-full">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-primary"></div>
|
||||
</div>
|
||||
) : (
|
||||
<textarea
|
||||
value={content}
|
||||
onChange={handleContentChange}
|
||||
placeholder="Ecrire..."
|
||||
className="w-full h-full resize-none focus:outline-none bg-transparent text-carnet-text-primary placeholder-carnet-text-muted"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user