|
| 1 | +import { IExecuteFunctions, IDataObject, NodeOperationError } from 'n8n-workflow'; |
| 2 | +import { EntityHandler } from './EntityHandler'; |
| 3 | +import { espoApiRequest, espoApiRequestAllItems } from '../GenericFunctions'; |
| 4 | + |
| 5 | +export class AttachmentHandler implements EntityHandler { |
| 6 | + async create(this: IExecuteFunctions, index: number): Promise<IDataObject> { |
| 7 | + // This method will be used for "upload" operation mapped to create |
| 8 | + const binaryPropertyName = this.getNodeParameter('binaryPropertyName', index) as string; |
| 9 | + const item = this.getInputData()[index]; |
| 10 | + if (!item.binary || !item.binary[binaryPropertyName]) { |
| 11 | + throw new NodeOperationError(this.getNode(), `No binary data property "${binaryPropertyName}" exists on item!`); |
| 12 | + } |
| 13 | + const binary = item.binary[binaryPropertyName]; |
| 14 | + |
| 15 | + const role = (this.getNodeParameter('role', index) as string) || 'Attachment'; |
| 16 | + const relatedType = (this.getNodeParameter('relatedType', index) as string) || 'Document'; |
| 17 | + const field = (this.getNodeParameter('field', index) as string) || 'file'; |
| 18 | + const additionalFields = this.getNodeParameter('additionalFields', index, {}) as IDataObject; |
| 19 | + |
| 20 | + const nameFromBinary = binary.fileName || (additionalFields.name as string) || 'file'; |
| 21 | + // Try to use binary mime, fallback to guess by extension, then default |
| 22 | + const guessMimeByExt = (fileName?: string): string | undefined => { |
| 23 | + if (!fileName) return undefined; |
| 24 | + const ext = fileName.split('.').pop()?.toLowerCase(); |
| 25 | + switch (ext) { |
| 26 | + case 'pdf': return 'application/pdf'; |
| 27 | + case 'txt': return 'text/plain'; |
| 28 | + case 'csv': return 'text/csv'; |
| 29 | + case 'json': return 'application/json'; |
| 30 | + case 'jpg': |
| 31 | + case 'jpeg': return 'image/jpeg'; |
| 32 | + case 'png': return 'image/png'; |
| 33 | + case 'gif': return 'image/gif'; |
| 34 | + case 'doc': return 'application/msword'; |
| 35 | + case 'docx': return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; |
| 36 | + case 'xls': return 'application/vnd.ms-excel'; |
| 37 | + case 'xlsx': return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; |
| 38 | + default: return undefined; |
| 39 | + } |
| 40 | + }; |
| 41 | + const typeFromBinary = binary.mimeType || (additionalFields.type as string) || guessMimeByExt(nameFromBinary) || 'application/octet-stream'; |
| 42 | + // Ensure size is a number; if not present, compute from base64 length |
| 43 | + const sizeFromBinary = typeof binary.fileSize !== 'undefined' |
| 44 | + ? Number(binary.fileSize) |
| 45 | + : (typeof binary.data === 'string' ? Buffer.from(binary.data, 'base64').length : undefined); |
| 46 | + |
| 47 | + // Compose data: URI |
| 48 | + const dataUri = `data:${typeFromBinary};base64,${binary.data}`; |
| 49 | + |
| 50 | + const body: IDataObject = { |
| 51 | + role, |
| 52 | + relatedType, |
| 53 | + field, |
| 54 | + name: nameFromBinary, |
| 55 | + type: typeFromBinary, |
| 56 | + file: dataUri, |
| 57 | + ...additionalFields, |
| 58 | + }; |
| 59 | + if (typeof sizeFromBinary === 'number' && !isNaN(sizeFromBinary)) { |
| 60 | + body.size = sizeFromBinary; |
| 61 | + } |
| 62 | + |
| 63 | + const response = await espoApiRequest.call(this, 'POST', '/Attachment', body); |
| 64 | + return response as IDataObject; |
| 65 | + } |
| 66 | + |
| 67 | + async get(this: IExecuteFunctions, index: number): Promise<IDataObject> { |
| 68 | + const id = this.getNodeParameter('attachmentId', index) as string; |
| 69 | + const response = await espoApiRequest.call(this, 'GET', `/Attachment/${id}`); |
| 70 | + return response as IDataObject; |
| 71 | + } |
| 72 | + |
| 73 | + async update(this: IExecuteFunctions, index: number): Promise<IDataObject> { |
| 74 | + // Read a parameter to avoid unused parameter warning |
| 75 | + void this.getNodeParameter('attachmentId', index, ''); |
| 76 | + throw new NodeOperationError(this.getNode(), 'Update operation is not supported for Attachment'); |
| 77 | + } |
| 78 | + |
| 79 | + async delete(this: IExecuteFunctions, index: number): Promise<IDataObject> { |
| 80 | + const id = this.getNodeParameter('attachmentId', index) as string; |
| 81 | + await espoApiRequest.call(this, 'DELETE', `/Attachment/${id}`); |
| 82 | + return { success: true, entityType: 'attachment', id } as IDataObject; |
| 83 | + } |
| 84 | + |
| 85 | + async getAll(this: IExecuteFunctions, index: number): Promise<IDataObject[]> { |
| 86 | + // Not typically used for attachments but provide minimal implementation |
| 87 | + const returnAll = this.getNodeParameter('returnAll', index, false) as boolean; |
| 88 | + const qs: IDataObject = {}; |
| 89 | + if (returnAll) { |
| 90 | + return await espoApiRequestAllItems.call(this, 'GET', '/Attachment', {}, qs); |
| 91 | + } else { |
| 92 | + const limit = this.getNodeParameter('limit', index, 50) as number; |
| 93 | + qs.maxSize = limit; |
| 94 | + const response = await espoApiRequest.call(this, 'GET', '/Attachment', {}, qs); |
| 95 | + return response.list as IDataObject[]; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Custom: Download operation - not part of EntityHandler interface, so handled in node execute routing or via getAll mapping |
| 100 | + async download(this: IExecuteFunctions, index: number): Promise<IDataObject> { |
| 101 | + throw new NodeOperationError(this.getNode(), 'Attachment download is temporarily disabled'); |
| 102 | + } |
| 103 | +} |
0 commit comments