fix: standardized server action return types and fixed media upload types
This commit is contained in:
parent
8a6db9df86
commit
3abaa7602f
@ -15,39 +15,70 @@ function getAdminClient() {
|
||||
}
|
||||
|
||||
export async function createDocumentServer(collectionId: string, data: any) {
|
||||
const client = getAdminClient();
|
||||
const databases = new Databases(client);
|
||||
const result = await databases.createDocument(DATABASE_ID, collectionId, ID.unique(), data);
|
||||
revalidatePath('/');
|
||||
return result;
|
||||
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) {
|
||||
const client = getAdminClient();
|
||||
const databases = new Databases(client);
|
||||
const result = await databases.updateDocument(DATABASE_ID, collectionId, documentId, data);
|
||||
revalidatePath('/');
|
||||
return result;
|
||||
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) {
|
||||
const client = getAdminClient();
|
||||
const databases = new Databases(client);
|
||||
const result = await databases.deleteDocument(DATABASE_ID, collectionId, documentId);
|
||||
revalidatePath('/');
|
||||
return result;
|
||||
try {
|
||||
const client = getAdminClient();
|
||||
const databases = new Databases(client);
|
||||
const result = await databases.deleteDocument(DATABASE_ID, collectionId, documentId);
|
||||
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(file: File) {
|
||||
const client = getAdminClient();
|
||||
const storage = new Storage(client);
|
||||
const buffer = Buffer.from(await file.arrayBuffer());
|
||||
// Nota: node-appwrite requiere un objeto con buffer y filename
|
||||
const result = await storage.createFile('media', ID.unique(), {
|
||||
buffer: buffer,
|
||||
filename: file.name,
|
||||
size: file.size,
|
||||
mimeType: file.type
|
||||
} as any);
|
||||
return result;
|
||||
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 result = await storage.createFile('media', ID.unique(), {
|
||||
buffer: buffer,
|
||||
filename: file.name,
|
||||
size: file.size,
|
||||
mimeType: file.type
|
||||
} as any);
|
||||
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 };
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user