n8n-nodes-placid
Version:
n8n node to interact with Placid API for creative generation
375 lines • 14.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDynamicLayerConfigurationFields = createDynamicLayerConfigurationFields;
exports.uploadBinaryFile = uploadBinaryFile;
exports.processUnifiedLayers = processUnifiedLayers;
const layerProperties_1 = require("./layerProperties");
const n8n_workflow_1 = require("n8n-workflow");
const config_1 = require("./config");
const multipartUtils_1 = require("./multipartUtils");
function createDynamicLayerConfigurationFields(resourceType, operationType, configModeField, collectionName, itemName, displayName) {
var _a;
const allLayerTypes = (0, layerProperties_1.getAllLayerTypes)();
const propertyFields = [];
const allPropertiesArray = [];
allLayerTypes.forEach(layerType => {
const properties = (0, layerProperties_1.getAllPropertiesForLayerType)(layerType.value, resourceType);
properties.forEach(prop => {
if (prop.value === 'custom') {
return;
}
const key = `${prop.value}|${prop.name}`;
if (!allPropertiesArray.some(p => `${p.value}|${p.name}` === key)) {
const prefixedName = `${layerType.name.toUpperCase()}: ${prop.name}`;
allPropertiesArray.push({
name: prefixedName,
value: prop.value,
description: `${prop.description}`,
});
}
});
});
allPropertiesArray.sort((a, b) => a.name.localeCompare(b.name));
allPropertiesArray.push({
name: 'Custom Property',
value: 'custom',
description: 'Set a custom property name and value (works with any layer type)',
});
if (allPropertiesArray.length > 0) {
propertyFields.push({
displayName: 'Property',
name: 'property',
type: 'options',
options: allPropertiesArray,
default: ((_a = allPropertiesArray[0]) === null || _a === void 0 ? void 0 : _a.value) || '',
description: 'Choose what property to set for this layer',
});
}
const valueFields = [];
const allProperties = new Map();
allLayerTypes.forEach(layerType => {
const properties = (0, layerProperties_1.getAllPropertiesForLayerType)(layerType.value, resourceType);
properties.forEach(prop => {
if (!allProperties.has(prop.value)) {
allProperties.set(prop.value, {
...prop,
layerTypes: [layerType.value]
});
}
else {
allProperties.get(prop.value).layerTypes.push(layerType.value);
}
});
});
allProperties.forEach((propInfo, propValue) => {
var _a;
if (propValue === 'custom') {
return;
}
let fieldType = propInfo.fieldType || 'string';
const placeholder = propInfo.placeholder || '';
const description = propInfo.description;
if (propValue === 'imageArray') {
fieldType = 'string';
}
if (fieldType === 'binary') {
fieldType = 'string';
}
const fieldConfig = {
displayName: propValue === 'value' ? 'Value' : propInfo.name,
name: `${propValue}Value`,
type: fieldType,
default: '',
description,
displayOptions: {
show: {
property: [propValue],
},
},
};
if (fieldType === 'options' && propInfo.options) {
fieldConfig.options = propInfo.options;
fieldConfig.default = ((_a = propInfo.options[0]) === null || _a === void 0 ? void 0 : _a.value) || '';
}
else {
fieldConfig.placeholder = placeholder;
if (propValue === 'imageArray') {
fieldConfig.typeOptions = {
rows: 4,
};
}
if (propInfo.fieldType === 'binary') {
fieldConfig.description = `${description} - Enter the field name containing the binary file data (e.g., "data")`;
fieldConfig.hint = 'The field name from the input data that contains the binary file data';
}
}
valueFields.push(fieldConfig);
});
valueFields.push({
displayName: 'Custom Property Name',
name: 'customPropertyName',
type: 'string',
default: '',
placeholder: 'e.g., lineHeight, letterSpacing, transform',
description: 'The name of the custom property to set',
displayOptions: {
show: {
property: ['custom'],
},
},
}, {
displayName: 'Custom Property Value',
name: 'customPropertyValue',
type: 'string',
default: '',
placeholder: 'e.g., 1.5, 2px, scale(1.2)',
description: 'The value for the custom property',
displayOptions: {
show: {
property: ['custom'],
},
},
});
return [
{
displayName: collectionName,
name: collectionName.toLowerCase(),
type: 'fixedCollection',
default: { [itemName]: [] },
typeOptions: {
multipleValues: true,
},
displayOptions: {
show: {
resource: [resourceType],
operation: [operationType],
[configModeField]: ['simple'],
},
},
options: [
{
name: itemName,
displayName: displayName,
values: [
{
displayName: 'Layer Name',
name: 'layerId',
type: 'options',
description: 'Select the layer to modify from the template',
default: '',
required: true,
typeOptions: {
loadOptionsMethod: 'getTemplateLayers',
loadOptionsDependsOn: ['template_id'],
},
},
...propertyFields,
...valueFields,
],
},
],
},
];
}
async function uploadBinaryFile(executeFunctions, index, fieldName) {
var _a, _b;
const items = executeFunctions.getInputData();
const item = items[index];
if (!item.binary || !item.binary[fieldName]) {
throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `No binary data found for field "${fieldName}"`);
}
const binaryData = item.binary[fieldName];
const fileBuffer = await executeFunctions.helpers.getBinaryDataBuffer(index, fieldName);
let fileName = binaryData.fileName;
if (!fileName) {
const extension = ((_a = binaryData.mimeType) === null || _a === void 0 ? void 0 : _a.split('/')[1]) || 'bin';
fileName = `upload.${extension}`;
}
const { body, boundary } = (0, multipartUtils_1.buildMultipartBody)([
{
key: 'file',
buffer: fileBuffer,
filename: fileName,
contentType: binaryData.mimeType || 'application/octet-stream',
},
]);
try {
const response = await executeFunctions.helpers.httpRequestWithAuthentication.call(executeFunctions, 'placidApi', {
method: 'POST',
url: config_1.PlacidConfig.getRestUrl(config_1.PlacidConfig.ENDPOINTS.MEDIA),
body,
headers: {
'Content-Type': `multipart/form-data; boundary=${boundary}`,
'x-placid-integration': config_1.PlacidConfig.HTTP.HEADERS.PLACID_INTEGRATION,
},
});
let responseData;
try {
responseData = typeof response === 'string' ? JSON.parse(response) : response;
}
catch (error) {
throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Failed to parse upload response: ${error.message}`);
}
if (!responseData.media || !Array.isArray(responseData.media) || responseData.media.length === 0) {
throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), 'Invalid response format from Placid media upload API');
}
return responseData.media[0].file_id;
}
catch (error) {
if (error.response) {
const statusCode = error.response.status;
const errorMessage = ((_b = error.response.data) === null || _b === void 0 ? void 0 : _b.message) || error.response.data || error.message;
throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Media upload failed (${statusCode}): ${errorMessage}`);
}
throw new n8n_workflow_1.NodeOperationError(executeFunctions.getNode(), `Media upload failed: ${error.message}`);
}
}
async function processUnifiedLayers(layers, executeFunctions, index) {
const processedLayers = {};
for (const layer of layers) {
let layerName;
let layerType;
if (layer.layerId && layer.layerId.includes('|')) {
const parts = layer.layerId.split('|');
layerName = parts[0];
layerType = parts[1] || 'unknown';
}
else {
layerName = layer.layerId;
layerType = layer.layerType || 'unknown';
}
if (!layerName)
continue;
void layerType;
if (!processedLayers[layerName]) {
processedLayers[layerName] = {};
}
if (layer.property === 'custom' && layer.customPropertyName && layer.customPropertyValue) {
processedLayers[layerName][layer.customPropertyName] = layer.customPropertyValue;
continue;
}
const propertyValue = layer.property;
let valueToSet = null;
switch (propertyValue) {
case 'text':
valueToSet = layer.textValue;
break;
case 'color':
valueToSet = layer.colorValue;
break;
case 'text_color':
valueToSet = layer.text_colorValue;
break;
case 'alt_text_color':
valueToSet = layer.alt_text_colorValue;
break;
case 'background_color':
valueToSet = layer.background_colorValue;
break;
case 'border_color':
valueToSet = layer.border_colorValue;
break;
case 'font':
valueToSet = layer.fontValue;
break;
case 'alt_font':
valueToSet = layer.alt_fontValue;
break;
case 'fontFamily':
valueToSet = layer.fontFamilyValue;
break;
case 'fontSize':
valueToSet = layer.fontSizeValue;
break;
case 'image':
valueToSet = layer.imageArrayValue && layer.imageArrayValue.length > 0
? layer.imageArrayValue
: layer.imageValue;
break;
case 'imageBinary':
if (layer.imageBinaryValue && executeFunctions && index !== undefined) {
try {
valueToSet = await uploadBinaryFile(executeFunctions, index, layer.imageBinaryValue);
if (valueToSet) {
processedLayers[layerName]['image'] = valueToSet;
}
continue;
}
catch (error) {
throw error;
}
}
break;
case 'imageArray':
if (layer.imageArrayValue) {
if (Array.isArray(layer.imageArrayValue)) {
valueToSet = layer.imageArrayValue.length > 0 ? layer.imageArrayValue : null;
}
else if (typeof layer.imageArrayValue === 'string') {
const imageUrls = layer.imageArrayValue.split('\n')
.map(url => url.trim())
.filter(url => url.length > 0);
valueToSet = imageUrls.length > 0 ? imageUrls : null;
}
}
if (valueToSet !== null && valueToSet !== undefined && valueToSet !== '') {
processedLayers[layerName]['image'] = valueToSet;
}
continue;
case 'video':
valueToSet = layer.videoValue;
break;
case 'videoBinary':
if (layer.videoBinaryValue && executeFunctions && index !== undefined) {
try {
valueToSet = await uploadBinaryFile(executeFunctions, index, layer.videoBinaryValue);
if (valueToSet) {
processedLayers[layerName]['video'] = valueToSet;
}
continue;
}
catch (error) {
throw error;
}
}
break;
case 'opacity':
valueToSet = layer.opacityValue;
break;
case 'rotation':
valueToSet = layer.rotationValue;
break;
case 'border_radius':
case 'borderRadius':
valueToSet = layer.borderRadiusValue;
break;
case 'border_width':
valueToSet = layer.border_widthValue;
break;
case 'url':
valueToSet = layer.urlValue;
break;
case 'svg':
valueToSet = layer.svgValue;
break;
case 'image_viewport':
valueToSet = layer.image_viewportValue;
break;
case 'value':
valueToSet = layer.valueValue;
break;
case 'srt':
valueToSet = layer.srtValue;
break;
default:
const fieldName = `${propertyValue}Value`;
valueToSet = layer[fieldName];
break;
}
if (valueToSet !== null && valueToSet !== undefined && valueToSet !== '') {
processedLayers[layerName][propertyValue] = valueToSet;
}
}
return processedLayers;
}
//# sourceMappingURL=layerUtils.js.map