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) {
|
export async function createDocumentServer(collectionId: string, data: any) {
|
||||||
|
try {
|
||||||
const client = getAdminClient();
|
const client = getAdminClient();
|
||||||
const databases = new Databases(client);
|
const databases = new Databases(client);
|
||||||
const result = await databases.createDocument(DATABASE_ID, collectionId, ID.unique(), data);
|
const result = await databases.createDocument(DATABASE_ID, collectionId, ID.unique(), data);
|
||||||
revalidatePath('/');
|
revalidatePath('/');
|
||||||
return result;
|
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) {
|
export async function updateDocumentServer(collectionId: string, documentId: string, data: any) {
|
||||||
|
try {
|
||||||
const client = getAdminClient();
|
const client = getAdminClient();
|
||||||
const databases = new Databases(client);
|
const databases = new Databases(client);
|
||||||
const result = await databases.updateDocument(DATABASE_ID, collectionId, documentId, data);
|
const result = await databases.updateDocument(DATABASE_ID, collectionId, documentId, data);
|
||||||
revalidatePath('/');
|
revalidatePath('/');
|
||||||
return result;
|
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) {
|
export async function deleteDocumentServer(collectionId: string, documentId: string) {
|
||||||
|
try {
|
||||||
const client = getAdminClient();
|
const client = getAdminClient();
|
||||||
const databases = new Databases(client);
|
const databases = new Databases(client);
|
||||||
const result = await databases.deleteDocument(DATABASE_ID, collectionId, documentId);
|
const result = await databases.deleteDocument(DATABASE_ID, collectionId, documentId);
|
||||||
revalidatePath('/');
|
revalidatePath('/');
|
||||||
return result;
|
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) {
|
export async function uploadMediaServer(formData: FormData) {
|
||||||
|
try {
|
||||||
const client = getAdminClient();
|
const client = getAdminClient();
|
||||||
const storage = new Storage(client);
|
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 buffer = Buffer.from(await file.arrayBuffer());
|
||||||
// Nota: node-appwrite requiere un objeto con buffer y filename
|
|
||||||
const result = await storage.createFile('media', ID.unique(), {
|
const result = await storage.createFile('media', ID.unique(), {
|
||||||
buffer: buffer,
|
buffer: buffer,
|
||||||
filename: file.name,
|
filename: file.name,
|
||||||
size: file.size,
|
size: file.size,
|
||||||
mimeType: file.type
|
mimeType: file.type
|
||||||
} as any);
|
} as any);
|
||||||
return result;
|
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