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 {
|
interface Note {
|
||||||
id: string;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
lastModified: string;
|
content: string;
|
||||||
size: number;
|
lastEdited: Date;
|
||||||
type: string;
|
category?: string;
|
||||||
mime: string;
|
tags?: string[];
|
||||||
etag: string;
|
|
||||||
content?: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EditorProps {
|
interface EditorProps {
|
||||||
@ -22,33 +20,11 @@ interface EditorProps {
|
|||||||
export const Editor: React.FC<EditorProps> = ({ note, onSave }) => {
|
export const Editor: React.FC<EditorProps> = ({ note, onSave }) => {
|
||||||
const [title, setTitle] = useState(note?.title || '');
|
const [title, setTitle] = useState(note?.title || '');
|
||||||
const [content, setContent] = useState(note?.content || '');
|
const [content, setContent] = useState(note?.content || '');
|
||||||
const [loading, setLoading] = useState(false);
|
|
||||||
|
|
||||||
useEffect(() => {
|
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) {
|
if (note) {
|
||||||
setTitle(note.title);
|
setTitle(note.title);
|
||||||
fetchNoteContent();
|
setContent(note.content);
|
||||||
} else {
|
|
||||||
setTitle('');
|
|
||||||
setContent('');
|
|
||||||
}
|
}
|
||||||
}, [note]);
|
}, [note]);
|
||||||
|
|
||||||
@ -63,9 +39,12 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave }) => {
|
|||||||
const handleSave = () => {
|
const handleSave = () => {
|
||||||
if (note?.id) {
|
if (note?.id) {
|
||||||
onSave?.({
|
onSave?.({
|
||||||
...note,
|
id: note.id,
|
||||||
title,
|
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 */}
|
{/* Editor Area */}
|
||||||
<div className="flex-1 p-4">
|
<div className="flex-1 p-4">
|
||||||
{loading ? (
|
<textarea
|
||||||
<div className="flex items-center justify-center h-full">
|
value={content}
|
||||||
<div className="animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-primary"></div>
|
onChange={handleContentChange}
|
||||||
</div>
|
placeholder="Ecrire..."
|
||||||
) : (
|
className="w-full h-full resize-none focus:outline-none bg-transparent text-carnet-text-primary placeholder-carnet-text-muted"
|
||||||
<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>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user