{
  "name": "HoL — Nextcloud L4: File email attachments",
  "nodes": [
    {
      "name": "Email Trigger (IMAP)",
      "type": "n8n-nodes-base.emailReadImap",
      "typeVersion": 2,
      "position": [-460, 0],
      "parameters": {
        "format": "resolved",
        "options": {
          "allowUnauthorizedCerts": false,
          "forceReconnect": 60
        }
      }
    },
    {
      "name": "Has an attachment?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [-220, 0],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "version": 2
          },
          "conditions": [
            {
              "id": "v1",
              "leftValue": "={{ Object.keys($binary ?? {}).length }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "gt"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "name": "One item per attachment",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [20, -100],
      "parameters": {
        "jsCode": "const out = [];\n\nfor (const item of $input.all()) {\n  for (const key of Object.keys(item.binary ?? {})) {\n    out.push({\n      json: { ...item.json, sourceBinaryProperty: key },\n      binary: { attachment: item.binary[key] },\n    });\n  }\n}\n\nreturn out;"
      }
    },
    {
      "name": "Upload to Nextcloud",
      "type": "n8n-nodes-base.nextCloud",
      "typeVersion": 1,
      "position": [260, -100],
      "parameters": {
        "resource": "file",
        "operation": "upload",
        "path": "=/Team Files/Incoming/{{ $now.toFormat('yyyy-LL-dd') }}-{{ $binary.attachment.fileName }}",
        "binaryDataUpload": true,
        "binaryPropertyName": "attachment"
      }
    },
    {
      "name": "No attachment",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [20, 120],
      "parameters": {}
    }
  ],
  "connections": {
    "Email Trigger (IMAP)": {
      "main": [
        [
          {
            "node": "Has an attachment?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has an attachment?": {
      "main": [
        [
          {
            "node": "One item per attachment",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No attachment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "One item per attachment": {
      "main": [
        [
          {
            "node": "Upload to Nextcloud",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
