Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for single-file uploads #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 96 additions & 42 deletions nodes/FormTrigger/FormTrigger.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,39 @@ import {

import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
IWebhookResponseData,
} from 'n8n-workflow';

import fs from 'fs';

import formidable from 'formidable';

const defaultJS = `$(document).on('submit','#n8n-form',function(e){
var formData = new FormData($("#n8n-form").get(0));
$.post({
url: '#',
data: formData,
contentType: false,
processData: false,
success: function(result) {
var resp = jQuery.parseJSON(result);
if (resp.status === 'ok') {
$("#status").attr('class', 'alert alert-success');
$("#status").show();
$('#status-text').text('Form has been submitted.');
} else {
$("#status").attr('class', 'alert alert-danger');
$("#status").show();
$('#status-text').text('Something went wrong.');
}
},
});
return false;
});`;

export class FormTrigger implements INodeType {
description: INodeTypeDescription = {
displayName: 'Form Trigger',
Expand Down Expand Up @@ -146,6 +174,10 @@ export class FormTrigger implements INodeType {
name: 'Email',
value: 'email',
},
{
name: 'File',
value: 'file',
},
{
name: 'Hidden',
value: 'hidden',
Expand Down Expand Up @@ -230,6 +262,13 @@ export class FormTrigger implements INodeType {
type: 'string',
description: 'URL for custom CSS, For an example see "https://joffcom.github.io/style.css"',
},
{
displayName: 'Detailed Body',
name: 'detailedBody',
type: 'boolean',
default: false,
description: 'Whether to just output the form data (if False) or more information such as headers and query params (if True) in JSON data',
},
{
displayName: 'Form ID',
name: 'formId',
Expand All @@ -247,21 +286,7 @@ export class FormTrigger implements INodeType {
{
displayName: 'Javascript',
name: 'javascript',
default: `$(document).on('submit','#n8n-form',function(e){
$.post('#', $('#n8n-form').serialize(), function(result) {
var resp = jQuery.parseJSON(result);
if (resp.status === 'ok') {
$("#status").attr('class', 'alert alert-success');
$("#status").show();
$('#status-text').text('Form has been submitted.');
} else {
$("#status").attr('class', 'alert alert-danger');
$("#status").show();
$('#status-text').text('Something went wrong.');
}
});
return false;
});`,
default: defaultJS, // eslint-disable-line n8n-nodes-base/node-param-default-missing
type: 'string',
typeOptions: {
alwaysOpenEditWindow: true,
Expand Down Expand Up @@ -289,9 +314,11 @@ return false;

async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const webhookName = this.getWebhookName();
const req = this.getRequestObject();
const resp = this.getResponseObject();
const options = this.getNodeParameter('options', 0) as IDataObject;

if (webhookName === 'displayForm') {
const options = this.getNodeParameter('options', 0) as IDataObject;
const submitLabel = options.submitLabel ? options.submitLabel : 'Submit';
const cssFile = options.cssFile ? options.cssFile : 'https://joffcom.github.io/style.css';
const pageTitle = this.getNodeParameter('pageTitle', 0) as string;
Expand Down Expand Up @@ -327,30 +354,12 @@ return false;
}
}

const defaultJS = `$(document).on('submit','#n8n-form',function(e){
$.post('#', $('#n8n-form').serialize(), function(result) {
var resp = jQuery.parseJSON(result);
if (resp.status === 'ok') {
$("#status").attr('class', 'alert alert-success');
$("#status").show();
$('#status-text').text('Form has been submitted.');
} else {
$("#status").attr('class', 'alert alert-danger');
$("#status").show();
$('#status-text').text('Something went wrong.');
}
});
return false;
});`;

const javascript = options.javascript ? options.javascript : defaultJS;
const formName = options.formName ? options.formName : 'n8n-form';
const formId = options.formId ? options.formId : 'n8n-form';
const jQuery = options.jQuery ? options.jQuery : 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js';
const bootstrapCss = options.bootstrap ? options.bootstrap : 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css';


const res = this.getResponseObject();
const testForm = `<html>
<head>
<title>${pageTitle}</title>
Expand Down Expand Up @@ -384,19 +393,64 @@ return false;
</div>
</body>
</html>`;
res.status(200).send(testForm).end();
resp.status(200).send(testForm).end();
return {
noWebhookResponse: true,
};
}

const bodyData = this.getBodyData();
const form = new formidable.IncomingForm({ multiples: true });
return new Promise((resolve, reject) => {
form.parse(req, async (err, data, files) => {
const returnItem: INodeExecutionData = {
binary: {},
json: options.detailedBody ? {
headers: this.getHeaderData(),
params: this.getParamsData(),
query: this.getQueryData(),
body: data,
} : data,
};

let count = 0;
// now process the files
for (const xfile of Object.keys(files)) {
const processFiles: formidable.File[] = [];
let multiFile = false;
if (Array.isArray(files[xfile])) {
processFiles.push(...files[xfile] as formidable.File[]);
multiFile = true;
} else {
processFiles.push(files[xfile] as formidable.File);
}

let fileCount = 0;
for (const file of processFiles) {
let binaryPropertyName = xfile;
if (binaryPropertyName.endsWith('[]')) {
binaryPropertyName = binaryPropertyName.slice(0, -2);
}
if (multiFile === true) {
binaryPropertyName += fileCount++;
}
if (options.binaryPropertyName) {
binaryPropertyName = `${options.binaryPropertyName}${count}`;
}

const fileJson = file.toJSON() as unknown as IDataObject;
const fileContent = await fs.promises.readFile(file.path);

returnItem.binary![binaryPropertyName] = await this.helpers.prepareBinaryData(Buffer.from(fileContent), fileJson.name as string, fileJson.type as string);

count += 1;
}
}

return {
webhookResponse: '{"status": "ok"}',
workflowData: [
this.helpers.returnJsonArray(bodyData),
],
};
resolve({
webhookResponse: '{"status": "ok"}',
workflowData: [[returnItem,],],
});
});
});
}
}
36 changes: 36 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
},
"devDependencies": {
"@types/express": "^4.17.6",
"@types/formidable": "^1.0.31",
"@types/request-promise-native": "~1.0.15",
"@typescript-eslint/parser": "^5.29.0",
"eslint-plugin-n8n-nodes-base": "^1.5.4",
Expand All @@ -45,5 +46,8 @@
"prettier": "^2.7.1",
"tslint": "^6.1.2",
"typescript": "~4.6.0"
},
"dependencies": {
"formidable": "^1.2.1"
}
}