Ajustes de estructura
This commit is contained in:
@@ -251,9 +251,31 @@ export function registerUploadRecordImageTool(server) {
|
|||||||
);
|
);
|
||||||
if (validationError) return validationError;
|
if (validationError) return validationError;
|
||||||
|
|
||||||
const credentials = await getSessionCredentials(extra.sessionId);
|
const projectSlug = path.basename(resolveCurrentProjectDir());
|
||||||
|
let result;
|
||||||
|
|
||||||
// Step 1: Delete old upload
|
try {
|
||||||
|
// Paso 1: borrar upload viejo via Python (incluye sync de borrado a produccion).
|
||||||
|
await pythonPost("/api/cms/delete-upload", {
|
||||||
|
project: projectSlug,
|
||||||
|
uploadId,
|
||||||
|
table: tableName,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Paso 2: subir el nuevo via Python (incluye sync a produccion + truncado local).
|
||||||
|
result = await pythonPost("/api/cms/upload-to-field", {
|
||||||
|
project: projectSlug,
|
||||||
|
table: tableName,
|
||||||
|
num: recordId,
|
||||||
|
field: fieldName,
|
||||||
|
imageUrl: imageUrl,
|
||||||
|
});
|
||||||
|
} catch (pyErr) {
|
||||||
|
// Fallback al flujo directo PHP solo cuando el Python no es accesible.
|
||||||
|
// En este path queda el bug original (sin sync a produccion en modo test),
|
||||||
|
// pero es preferible a fallar el tool entero.
|
||||||
|
console.error(`[replace_record_image] Python server failed (${pyErr.message}), falling back to PHP direct`);
|
||||||
|
const credentials = await getSessionCredentials(extra.sessionId);
|
||||||
await mcpPost(
|
await mcpPost(
|
||||||
credentials,
|
credentials,
|
||||||
"deleteRecordUpload",
|
"deleteRecordUpload",
|
||||||
@@ -261,15 +283,10 @@ export function registerUploadRecordImageTool(server) {
|
|||||||
credentials.token,
|
credentials.token,
|
||||||
credentials.tokenHash
|
credentials.tokenHash
|
||||||
);
|
);
|
||||||
|
|
||||||
// Step 2: Upload new image
|
|
||||||
// Si la URL es local, descargar y enviar base64 (el servidor remoto no
|
|
||||||
// puede descargarla en modo produccion).
|
|
||||||
const localFile = await resolveLocalImageAsBase64(imageUrl);
|
const localFile = await resolveLocalImageAsBase64(imageUrl);
|
||||||
const uploadPayload = localFile
|
const uploadPayload = localFile
|
||||||
? { tableName, recordId, fieldName, alt, fileBase64: localFile.fileBase64, fileName: localFile.fileName }
|
? { tableName, recordId, fieldName, alt, fileBase64: localFile.fileBase64, fileName: localFile.fileName }
|
||||||
: { tableName, recordId, fieldName, imageUrl, alt };
|
: { tableName, recordId, fieldName, imageUrl, alt };
|
||||||
|
|
||||||
const response = await mcpPost(
|
const response = await mcpPost(
|
||||||
credentials,
|
credentials,
|
||||||
"uploadRecordImage",
|
"uploadRecordImage",
|
||||||
@@ -277,10 +294,17 @@ export function registerUploadRecordImageTool(server) {
|
|||||||
credentials.token,
|
credentials.token,
|
||||||
credentials.tokenHash
|
credentials.tokenHash
|
||||||
);
|
);
|
||||||
|
|
||||||
const apiError = handleApiResponse(response.data, 'replace_record_image');
|
const apiError = handleApiResponse(response.data, 'replace_record_image');
|
||||||
if (apiError) return apiError;
|
if (apiError) return apiError;
|
||||||
|
result = { success: true, data: response.data };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!result?.success && !result?.data?.success) {
|
||||||
|
const errMsg = result?.error || result?.data?.error || "Unknown error";
|
||||||
|
return { content: [{ type: "text", text: JSON.stringify({ error: errMsg }, null, 2) }], isError: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
const replaceData = result.data || result;
|
||||||
return {
|
return {
|
||||||
content: [{
|
content: [{
|
||||||
type: "text",
|
type: "text",
|
||||||
@@ -291,7 +315,7 @@ export function registerUploadRecordImageTool(server) {
|
|||||||
recordId,
|
recordId,
|
||||||
fieldName,
|
fieldName,
|
||||||
replacedUploadId: uploadId,
|
replacedUploadId: uploadId,
|
||||||
...response.data
|
...replaceData,
|
||||||
}, null, 2)
|
}, null, 2)
|
||||||
}],
|
}],
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user