2024-01-03 03:08:16 -08:00
import type { IExecuteFunctions , INodeExecutionData , INodeProperties } from 'n8n-workflow' ;
import { NodeOperationError } from 'n8n-workflow' ;
import type { JsonToBinaryOptions } from '@utils/binary' ;
import { createBinaryFromJson } from '@utils/binary' ;
import { encodeDecodeOptions } from '@utils/descriptions' ;
import { updateDisplayOptions } from '@utils/utilities' ;
export const properties : INodeProperties [ ] = [
{
displayName : 'Base64 Input Field' ,
name : 'sourceProperty' ,
type : 'string' ,
default : '' ,
required : true ,
placeholder : 'e.g data' ,
requiresDataPath : 'single' ,
description :
"The name of the input field that contains the base64 string to convert to a file. Use dot-notation for deep fields (e.g. 'level1.level2.currentKey')." ,
} ,
{
displayName : 'Put Output File in Field' ,
name : 'binaryPropertyName' ,
type : 'string' ,
default : 'data' ,
required : true ,
placeholder : 'e.g data' ,
hint : 'The name of the output binary field to put the file in' ,
} ,
{
displayName : 'Options' ,
name : 'options' ,
type : 'collection' ,
2024-07-29 05:27:23 -07:00
placeholder : 'Add option' ,
2024-01-03 03:08:16 -08:00
default : { } ,
options : [
{
displayName : 'Add Byte Order Mark (BOM)' ,
description :
'Whether to add special marker at the start of your text file. This marker helps some programs understand how to read the file correctly.' ,
name : 'addBOM' ,
displayOptions : {
show : {
encoding : [ 'utf8' , 'cesu8' , 'ucs2' ] ,
} ,
} ,
type : 'boolean' ,
default : false ,
} ,
{
displayName : 'Data Is Base64' ,
name : 'dataIsBase64' ,
type : 'boolean' ,
default : true ,
description : 'Whether the data is already base64 encoded' ,
2024-02-13 06:52:37 -08:00
displayOptions : {
show : {
'@version' : [ 1 ] ,
} ,
} ,
2024-01-03 03:08:16 -08:00
} ,
{
displayName : 'Encoding' ,
name : 'encoding' ,
type : 'options' ,
options : encodeDecodeOptions ,
default : 'utf8' ,
description : 'Choose the character set to use to encode the data' ,
displayOptions : {
hide : {
dataIsBase64 : [ true ] ,
2024-02-13 06:52:37 -08:00
'@version' : [ { _cnd : { gt : 1 } } ] ,
2024-01-03 03:08:16 -08:00
} ,
} ,
} ,
{
displayName : 'File Name' ,
name : 'fileName' ,
type : 'string' ,
default : '' ,
placeholder : 'e.g. myFile' ,
description : 'Name of the output file' ,
} ,
{
displayName : 'MIME Type' ,
name : 'mimeType' ,
type : 'string' ,
default : '' ,
placeholder : 'e.g text/plain' ,
description :
'The MIME type of the output file. <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types" target="_blank">Common MIME types</a>.' ,
} ,
] ,
} ,
] ;
const displayOptions = {
show : {
operation : [ 'toBinary' ] ,
} ,
} ;
export const description = updateDisplayOptions ( displayOptions , properties ) ;
export async function execute ( this : IExecuteFunctions , items : INodeExecutionData [ ] ) {
const returnData : INodeExecutionData [ ] = [ ] ;
2024-02-13 06:52:37 -08:00
const nodeVersion = this . getNode ( ) . typeVersion ;
2024-01-03 03:08:16 -08:00
for ( let i = 0 ; i < items . length ; i ++ ) {
try {
const options = this . getNodeParameter ( 'options' , i , { } ) ;
const binaryPropertyName = this . getNodeParameter ( 'binaryPropertyName' , i , 'data' ) ;
const sourceProperty = this . getNodeParameter ( 'sourceProperty' , i ) as string ;
2024-02-13 06:52:37 -08:00
let dataIsBase64 = true ;
if ( nodeVersion === 1 ) {
dataIsBase64 = options . dataIsBase64 !== false ;
}
2024-01-03 03:08:16 -08:00
const jsonToBinaryOptions : JsonToBinaryOptions = {
sourceKey : sourceProperty ,
fileName : options.fileName as string ,
mimeType : options.mimeType as string ,
2024-02-13 06:52:37 -08:00
dataIsBase64 ,
2024-01-03 03:08:16 -08:00
encoding : options.encoding as string ,
addBOM : options.addBOM as boolean ,
itemIndex : i ,
} ;
const binaryData = await createBinaryFromJson . call ( this , items [ i ] . json , jsonToBinaryOptions ) ;
const newItem : INodeExecutionData = {
json : { } ,
binary : {
[ binaryPropertyName ] : binaryData ,
} ,
pairedItem : { item : i } ,
} ;
returnData . push ( newItem ) ;
} catch ( error ) {
2024-08-30 00:59:30 -07:00
if ( this . continueOnFail ( ) ) {
2024-01-03 03:08:16 -08:00
returnData . push ( {
json : {
error : error.message ,
} ,
pairedItem : {
item : i ,
} ,
} ) ;
continue ;
}
throw new NodeOperationError ( this . getNode ( ) , error , { itemIndex : i } ) ;
}
}
return returnData ;
}