{
	"workflow": {
			"id": 1344,
			"name": "Save email attachments to Nextcloud",
			"views": 650,
			"recentViews": 9887,
			"totalViews": 650,
			"createdAt": "2021-11-29T13:59:16.771Z",
			"description": "This workflow will take all emails you put into a certain folder, upload any attachements to Nextcloud, and mark the emails as read (configurable).\n\nAttachements will be saved with automatically generated filenames:\n`2021-01-01_From-Sender-Name_Filename-of-attachement.pdf`\n\nInstructions:\n1. **Allow lodash to be used in n8n** (or rewrite the code...)\n  `NODE_FUNCTION_ALLOW_EXTERNAL=lodash` (environment variable)\n2. Import workflow\n3. Set credentials for Email & Nextcloud nodes\n4. Configure to use correct folder / custom filters\n5. Activate\n\nCustom filter examples:\n- Only unread emails:\n  `Custom Email Config` = `[\"UNSEEN\"]`\n- Filter emails by 'to' address:\n  `Custom Email Config` = `[[\"TO\", \"example+invoices@posteo.de\"]]`",
			"workflow": {
					"nodes": [
							{
									"name": "IMAP Email",
									"type": "n8n-nodes-base.emailReadImap",
									"position": [
											240,
											420
									],
									"parameters": {
											"format": "resolved",
											"mailbox": "Invoices",
											"options": {
													"customEmailConfig": "[\"ALL\"]"
											}
									},
									"typeVersion": 1
							},
							{
									"name": "Nextcloud",
									"type": "n8n-nodes-base.nextCloud",
									"position": [
											940,
											420
									],
									"parameters": {
											"path": "=Documents/Invoices/{{$json[\"date\"]}}_{{$json[\"from\"]}}_{{$binary.file.fileName}}",
											"binaryDataUpload": true,
											"binaryPropertyName": "file"
									},
									"typeVersion": 1
							},
							{
									"name": "Map each attachment",
									"type": "n8n-nodes-base.function",
									"position": [
											620,
											420
									],
									"parameters": {
											"functionCode": "const _ = require('lodash')\n\nconst sanitize = str => _.chain(str)\n  .replace(/[^A-Za-z0-9&.-]/g, '-') // sanitise via whitelist of characters\n  .replace(/-(?=-)/g, '') // remove repeated dashes - https://regexr.com/6ag8h\n  .trim('-') // trim any leading/trailing dashes\n  .truncate({\n    length: 60,\n    omission: '-' // when the string ends with '-', you'll know it was truncated\n  })\n  .value()\n\nconst result = _.flatMap(items.map(item => {\n  //console.log({item})\n\n  // Maps each attachment to a separate item\n  return _.values(item.binary).map(file => {\n    console.log(\"Saving attachement:\", file.fileName, 'from:', ...item.json.from.value)\n    \n    // sanitize filename but exclude extension\n    const filename_parts = file.fileName.split('.')\n    const ext = _.slice(filename_parts, filename_parts.length-1)\n    const filename_main = _.join(_.dropRight(filename_parts), '.')\n    file.fileName = sanitize(filename_main) + '.' + ext\n    \n    return {\n      json: {\n        from: sanitize(item.json.from.value[0].name),\n        date: sanitize(new Date(item.json.date).toISOString().split(\"T\")[0]) // get date part \"2020-01-01\"\n      }, \n      binary: { file }\n    }\n  })\n}))\n\n//console.log(result)\nreturn result"
									},
									"typeVersion": 1
							}
					],
					"connections": {
							"IMAP Email": {
									"main": [
											[
													{
															"node": "Map each attachment",
															"type": "main",
															"index": 0
													}
											]
									]
							},
							"Map each attachment": {
									"main": [
											[
													{
															"node": "Nextcloud",
															"type": "main",
															"index": 0
													}
											]
									]
							}
					}
			},
			"lastUpdatedBy": 11,
			"workflowInfo": {
					"nodeCount": 3,
					"nodeTypes": {
							"n8n-nodes-base.function": {
									"count": 1
							},
							"n8n-nodes-base.nextCloud": {
									"count": 1
							},
							"n8n-nodes-base.emailReadImap": {
									"count": 1
							}
					}
			},
			"user": {
					"username": "tennox"
			},
			"nodes": [
					{
							"id": 10,
							"icon": "fa:inbox",
							"name": "n8n-nodes-base.emailReadImap",
							"defaults": {
									"name": "Email Trigger (IMAP)",
									"color": "#44AA22"
							},
							"iconData": {
									"icon": "inbox",
									"type": "icon"
							},
							"categories": [
									{
											"id": 6,
											"name": "Communication"
									},
									{
											"id": 9,
											"name": "Core Nodes"
									}
							],
							"displayName": "Email Trigger (IMAP)",
							"typeVersion": 2
					},
					{
							"id": 14,
							"icon": "fa:code",
							"name": "n8n-nodes-base.function",
							"defaults": {
									"name": "Function",
									"color": "#FF9922"
							},
							"iconData": {
									"icon": "code",
									"type": "icon"
							},
							"categories": [
									{
											"id": 5,
											"name": "Development"
									},
									{
											"id": 9,
											"name": "Core Nodes"
									}
							],
							"displayName": "Function",
							"typeVersion": 1
					},
					{
							"id": 25,
							"icon": "file:nextcloud.svg",
							"name": "n8n-nodes-base.nextCloud",
							"defaults": {
									"name": "Nextcloud"
							},
							"iconData": {
									"type": "file",
									"fileBuffer": "data:image/svg+xml;base64,[encoded data, 5476 characters]"
							},
							"categories": [
									{
											"id": 3,
											"name": "Data & Storage"
									}
							],
							"displayName": "Nextcloud",
							"typeVersion": 1
					}
			],
			"categories": [
					{
							"id": 2,
							"name": "Sales"
					},
					{
							"id": 8,
							"name": "Finance & Accounting"
					}
			],
			"image": []
	}
}
