| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- 'use server';
- import { prisma } from '@/lib/prisma';
- export async function uploadFile(file: File, userId: string) {
- try {
- const bytes = await file.arrayBuffer();
- const buffer = Buffer.from(bytes);
- const uploadedFile = await prisma.file.create({
- data: {
- filename: file.name,
- mimetype: file.type,
- size: file.size,
- data: buffer,
- userId: userId,
- },
- });
- return {
- id: uploadedFile.id,
- filename: uploadedFile.filename,
- mimetype: uploadedFile.mimetype,
- size: uploadedFile.size,
- userId: uploadedFile.userId,
- createdAt: uploadedFile.createdAt.toISOString(),
- updatedAt: uploadedFile.updatedAt.toISOString(),
- };
- } catch (error) {
- console.error('Error uploading file:', error);
- throw new Error('Failed to upload file');
- }
- }
- export async function getFileById(id: string) {
- try {
- const file = await prisma.file.findUnique({
- where: { id },
- });
- if (!file) {
- return { success: false, error: 'File not found' };
- }
- return { success: true, data: file };
- } catch (error) {
- console.error('Error fetching file:', error);
- return { success: false, error: 'Failed to fetch file' };
- }
- }
|