n8n-nodes-nextcloud-deck
Version:
n8n Node für die Integration mit Nextcloud Deck - AI Agent Tool Support
97 lines (96 loc) • 4.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteAttachment = exports.updateAttachment = exports.createAttachment = exports.getAttachment = exports.getAttachments = void 0;
const api_1 = require("../helpers/api");
const form_data_1 = __importDefault(require("form-data"));
async function getAttachments(boardId, stackId, cardId) {
const endpoint = `/boards/${boardId}/stacks/${stackId}/cards/${cardId}/attachments`;
console.log(`DEBUG: Requesting attachments from: ${endpoint}`);
try {
const response = await api_1.nextcloudDeckApiRequest.call(this, 'GET', endpoint);
console.log(`DEBUG: Attachments API response:`, JSON.stringify(response, null, 2));
if (Array.isArray(response)) {
return response;
}
else if (response && typeof response === 'object' && 'data' in response) {
const data = response.data;
if (Array.isArray(data)) {
return data;
}
}
return response;
}
catch (error) {
console.error(`DEBUG: Error fetching attachments:`, error);
throw error;
}
}
exports.getAttachments = getAttachments;
async function getAttachment(boardId, stackId, cardId, attachmentId) {
const attachments = await getAttachments.call(this, boardId, stackId, cardId);
const attachment = attachments.find(a => a.id === attachmentId);
if (!attachment) {
throw new Error(`Anhang ${attachmentId} nicht gefunden`);
}
return attachment;
}
exports.getAttachment = getAttachment;
async function createAttachment(boardId, stackId, cardId, attachmentData, itemIndex = 0) {
const endpoint = `/boards/${boardId}/stacks/${stackId}/cards/${cardId}/attachments`;
console.log(`DEBUG: Creating attachment with data:`, attachmentData);
const formData = new form_data_1.default();
formData.append('type', attachmentData.type);
if (attachmentData.type === 'file') {
formData.append('data', attachmentData.data);
}
else {
const items = this.getInputData();
const item = items[itemIndex];
let filename = 'attachment.txt';
let fileBuffer;
let contentType = 'text/plain';
if (item.binary && Object.keys(item.binary).length > 0) {
const binaryKey = Object.keys(item.binary)[0];
const binaryData = item.binary[binaryKey];
filename = binaryData.fileName || 'attachment';
contentType = binaryData.mimeType || 'application/octet-stream';
const binaryDataBuffer = await this.helpers.getBinaryDataBuffer(itemIndex, binaryKey);
fileBuffer = binaryDataBuffer;
console.log(`DEBUG: Using binary data from key '${binaryKey}', filename: ${filename}, contentType: ${contentType}, size: ${fileBuffer.length}`);
}
else {
const content = attachmentData.data || 'Anhang-Inhalt';
fileBuffer = Buffer.from(content, 'utf8');
filename = 'attachment.txt';
contentType = 'text/plain';
console.log(`DEBUG: No binary data found, using text content: "${content}"`);
}
formData.append('file', fileBuffer, {
filename: filename,
contentType: contentType,
});
}
console.log(`DEBUG: FormData prepared for endpoint: ${endpoint}`);
const response = await api_1.nextcloudDeckFileUploadRequest.call(this, 'POST', endpoint, formData);
console.log(`DEBUG: Create attachment response:`, JSON.stringify(response, null, 2));
return response;
}
exports.createAttachment = createAttachment;
async function updateAttachment(boardId, stackId, cardId, attachmentData) {
const endpoint = `/boards/${boardId}/stacks/${stackId}/cards/${cardId}/attachments/${attachmentData.id}`;
const body = {};
if (attachmentData.data)
body.data = attachmentData.data;
const response = await api_1.nextcloudDeckApiRequest.call(this, 'PUT', endpoint, body);
return response;
}
exports.updateAttachment = updateAttachment;
async function deleteAttachment(boardId, stackId, cardId, attachmentId) {
const endpoint = `/boards/${boardId}/stacks/${stackId}/cards/${cardId}/attachments/${attachmentId}`;
await api_1.nextcloudDeckApiRequest.call(this, 'DELETE', endpoint);
return { success: true };
}
exports.deleteAttachment = deleteAttachment;