125 lines
4.7 KiB
TypeScript
125 lines
4.7 KiB
TypeScript
'use server'
|
|
|
|
import { Client, Databases, Storage, ID } from 'node-appwrite';
|
|
import { APPWRITE_ALMEIRA_CONFIG, databaseId } from '@/lib/appwrite';
|
|
import { revalidatePath } from 'next/cache';
|
|
import { InputFile } from 'node-appwrite/file';
|
|
|
|
const ENDPOINT = process.env.NEXT_PUBLIC_APPWRITE_ENDPOINT || 'https://appwrite.almeira.com.co/v1';
|
|
const PROJECT_ID = process.env.NEXT_PUBLIC_APPWRITE_PROJECT_ID || '69b84df5003bd8957c5c';
|
|
const DATABASE_ID = databaseId;
|
|
|
|
function getAdminClient() {
|
|
return new Client()
|
|
.setEndpoint(ENDPOINT)
|
|
.setProject(PROJECT_ID)
|
|
.setKey(process.env.APPWRITE_API_KEY || '');
|
|
}
|
|
|
|
export async function createDocumentServer(collectionId: string, data: any) {
|
|
try {
|
|
const client = getAdminClient();
|
|
const databases = new Databases(client);
|
|
const result = await databases.createDocument(DATABASE_ID, collectionId, ID.unique(), data);
|
|
revalidatePath('/');
|
|
return { success: true, data: result };
|
|
} catch (error: any) {
|
|
console.error('Error creating document:', error);
|
|
return { success: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
export async function updateDocumentServer(collectionId: string, documentId: string, data: any) {
|
|
try {
|
|
const client = getAdminClient();
|
|
const databases = new Databases(client);
|
|
const result = await databases.updateDocument(DATABASE_ID, collectionId, documentId, data);
|
|
revalidatePath('/');
|
|
return { success: true, data: result };
|
|
} catch (error: any) {
|
|
console.error('Error updating document:', error);
|
|
return { success: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
export async function deleteDocumentServer(collectionId: string, documentId: string, entityAlias?: string) {
|
|
try {
|
|
const client = getAdminClient();
|
|
const databases = new Databases(client);
|
|
const result = await databases.deleteDocument(DATABASE_ID, collectionId, documentId);
|
|
|
|
// Si hay un alias de entidad, podemos revalidar su ruta específica
|
|
if (entityAlias) {
|
|
revalidatePath(`/dashboard/${entityAlias}`);
|
|
}
|
|
|
|
revalidatePath('/');
|
|
return { success: true, data: result };
|
|
} catch (error: any) {
|
|
console.error('Error deleting document:', error);
|
|
return { success: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
export async function uploadMediaServer(formData: FormData) {
|
|
try {
|
|
const client = getAdminClient();
|
|
const storage = new Storage(client);
|
|
const files = formData.getAll('files') as File[];
|
|
|
|
if (!files || files.length === 0) {
|
|
throw new Error('No se encontraron archivos para subir');
|
|
}
|
|
|
|
const results = [];
|
|
for (const file of files) {
|
|
const buffer = Buffer.from(await file.arrayBuffer());
|
|
const inputFile = InputFile.fromBuffer(buffer, file.name);
|
|
const result = await storage.createFile(APPWRITE_ALMEIRA_CONFIG.mediaBucketId, ID.unique(), inputFile);
|
|
results.push(result);
|
|
}
|
|
|
|
revalidatePath('/dashboard/media');
|
|
return { success: true, count: results.length, data: results };
|
|
} catch (error: any) {
|
|
console.error('Error uploading media:', error);
|
|
return { success: false, error: error.message };
|
|
}
|
|
}
|
|
|
|
export async function uploadMediaDocumentServer(formData: FormData) {
|
|
try {
|
|
const client = getAdminClient();
|
|
const storage = new Storage(client);
|
|
const databases = new Databases(client);
|
|
const files = formData.getAll('files') as File[];
|
|
|
|
if (!files || files.length === 0) {
|
|
throw new Error('No se encontraron archivos para subir');
|
|
}
|
|
|
|
const results = [];
|
|
for (const file of files) {
|
|
const buffer = Buffer.from(await file.arrayBuffer());
|
|
const inputFile = InputFile.fromBuffer(buffer, file.name);
|
|
const uploadedFile = await storage.createFile(APPWRITE_ALMEIRA_CONFIG.mediaBucketId, ID.unique(), inputFile);
|
|
|
|
const doc = await databases.createDocument(DATABASE_ID, 'media', ID.unique(), {
|
|
file_id: uploadedFile.$id,
|
|
bucket_id: APPWRITE_ALMEIRA_CONFIG.mediaBucketId,
|
|
titulo: file.name,
|
|
alt_text: file.name.split('.')[0],
|
|
tipo: file.type.startsWith('image/') ? 'imagen' : 'video'
|
|
});
|
|
results.push(doc);
|
|
}
|
|
|
|
revalidatePath('/dashboard/media');
|
|
revalidatePath('/');
|
|
return { success: true, count: results.length, data: results };
|
|
} catch (error: any) {
|
|
console.error('Error uploading media and creating document:', error);
|
|
return { success: false, error: error.message };
|
|
}
|
|
}
|