carnet panel3
This commit is contained in:
parent
8bef7bac16
commit
697916f277
72
app/api/nextcloud/files/[id]/route.ts
Normal file
72
app/api/nextcloud/files/[id]/route.ts
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
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,10 +6,12 @@ import { Image, FileText, Link, List } from 'lucide-react';
|
|||||||
interface Note {
|
interface Note {
|
||||||
id: string;
|
id: string;
|
||||||
title: string;
|
title: string;
|
||||||
content: string;
|
lastModified: string;
|
||||||
lastEdited: Date;
|
size: number;
|
||||||
category?: string;
|
type: string;
|
||||||
tags?: string[];
|
mime: string;
|
||||||
|
etag: string;
|
||||||
|
content?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface EditorProps {
|
interface EditorProps {
|
||||||
@ -20,11 +22,33 @@ 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);
|
||||||
setContent(note.content);
|
fetchNoteContent();
|
||||||
|
} else {
|
||||||
|
setTitle('');
|
||||||
|
setContent('');
|
||||||
}
|
}
|
||||||
}, [note]);
|
}, [note]);
|
||||||
|
|
||||||
@ -39,12 +63,9 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave }) => {
|
|||||||
const handleSave = () => {
|
const handleSave = () => {
|
||||||
if (note?.id) {
|
if (note?.id) {
|
||||||
onSave?.({
|
onSave?.({
|
||||||
id: note.id,
|
...note,
|
||||||
title,
|
title,
|
||||||
content,
|
content
|
||||||
lastEdited: new Date(),
|
|
||||||
category: note.category,
|
|
||||||
tags: note.tags
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -85,12 +106,18 @@ export const Editor: React.FC<EditorProps> = ({ note, onSave }) => {
|
|||||||
|
|
||||||
{/* Editor Area */}
|
{/* Editor Area */}
|
||||||
<div className="flex-1 p-4">
|
<div className="flex-1 p-4">
|
||||||
<textarea
|
{loading ? (
|
||||||
value={content}
|
<div className="flex items-center justify-center h-full">
|
||||||
onChange={handleContentChange}
|
<div className="animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-primary"></div>
|
||||||
placeholder="Ecrire..."
|
</div>
|
||||||
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