{
  "name": "wf-ai-shutterstock-metadata-generator",
  "nodes": [
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -80,
        48
      ],
      "id": "f100ba04-437d-425c-b4a4-56178f89af8b",
      "name": "When clicking \u2018Execute workflow\u2019"
    },
    {
      "parameters": {
        "fileSelector": "/data/prompts/shutterstock-metadata-v1.md",
        "options": {
          "dataPropertyName": "prompt"
        }
      },
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1.1,
      "position": [
        144,
        144
      ],
      "id": "b6168292-902e-450e-8568-b82a315e3f7e",
      "name": "Read Prompt"
    },
    {
      "parameters": {
        "fileSelector": "/data/inputs/*.jpg",
        "options": {
          "dataPropertyName": "image"
        }
      },
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1.1,
      "position": [
        144,
        -64
      ],
      "id": "dd3f5b1f-a896-4cab-84bb-826edb133511",
      "name": "Read Images",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "operation": "binaryToPropery",
        "binaryPropertyName": "image",
        "destinationKey": "imageBinary",
        "options": {
          "keepSource": "json"
        }
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1.1,
      "position": [
        640,
        -48
      ],
      "id": "ab578549-1fa8-4c44-a2c9-2cb237310fd0",
      "name": "ExtractImageBinary"
    },
    {
      "parameters": {
        "operation": "text",
        "binaryPropertyName": "prompt",
        "destinationKey": "promptJSON",
        "options": {}
      },
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1.1,
      "position": [
        352,
        144
      ],
      "id": "769261e9-f752-4b55-a0e6-e7d9be3a7bad",
      "name": "ExtractPrompt"
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const imageBinary = $json.imageBinary;\nconst mimeType =$json.mimeType;\nconst fileName = $json.fileName;\nconst promptJSON = $('ExtractPrompt').first().json.promptJSON;\n\nconst payload = {\n  contents: [\n    {\n      parts: [\n        {\n          text: \"Filename: \"+ fileName +\"\\n\\n\" +\n                  promptJSON\n        },\n        {\n          inline_data: {\n            mime_type: mimeType,\n            data: imageBinary\n          }\n        }\n      ]\n    }\n  ],\n  generationConfig: {\n    temperature: 0.2,\n    responseMimeType: \"application/json\"\n  }\n};\n\nreturn payload;\n"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        848,
        -48
      ],
      "id": "a4e58220-dbb5-410c-b2ff-2f72f78c1c6f",
      "name": "Build Payload"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://generativelanguage.googleapis.com/v1beta/models/gemini-flash-latest:generateContent",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ $json }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1088,
        -240
      ],
      "id": "492c2752-8bc6-4ea2-bc2e-42b662fcf4cd",
      "name": "Call Gemini",
      "credentials": {
        "httpQueryAuth": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const candidate = $json.candidates?.[0];\n\nif (!candidate) {\n  throw new Error(\"No candidate returned from Gemini.\");\n}\n\nconst text = $json.candidates[0].content.parts[0].text;\n\n// Parse JSON string into object\nconst metadata = JSON.parse(text);\n\n\nconst result = {\n  fileName: $('ExtractImageBinary').item.json.fileName,\n  ...metadata,\n  ai: {\n    model: $json.modelVersion,\n    responseId: $json.responseId,\n    usage: $json.usageMetadata\n  }\n}\n\nreturn result;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1472,
        -48
      ],
      "id": "8b2f4b3c-0540-424c-bd8d-d9ad342afa25",
      "name": "Parse Response"
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "const metadata = $json\nvar error = 0\nvar descriptionError = \"\"\nvar keywordError = \"\"\nvar categoryError = \"\"\n\nconst VALID_CATEGORIES = [\n  \"Abstract\",\n  \"Animals/Wildlife\",\n  \"Arts\",\n  \"Backgrounds/Textures\",\n  \"Beauty/Fashion\",\n  \"Buildings/Landmarks\",\n  \"Business/Finance\",\n  \"Celebrities\",\n  \"Education\",\n  \"Food and drink\",\n  \"Healthcare/Medical\",\n  \"Holidays\",\n  \"Industrial\",\n  \"Interiors\",\n  \"Miscellaneous\",\n  \"Nature\",\n  \"Objects\",\n  \"Parks/Outdoor\",\n  \"People\",\n  \"Religion\",\n  \"Science\",\n  \"Signs/Symbols\",\n  \"Sports/Recreation\",\n  \"Technology\",\n  \"Transportation\",\n  \"Vintage\"\n];\n\n// -------------------------\n// Description\n// -------------------------\n\nif (!metadata.description) {\n  descriptionError += \"Description is missing.\";\n  error = 1;\n}\n\nconst descriptionLength = metadata.description.length;\n\nif (descriptionLength < 180 || descriptionLength > 250) {\n  descriptionError +=\n    `Description length is ${descriptionLength}. Expected 180-250 characters.`;\n  error = 1;\n}\n\n// -------------------------\n// Keywords\n// -------------------------\n\nif (!Array.isArray(metadata.keywords)) {\n  keywordError += \"Keywords must be an array.\";\n  error = 1;\n}\n\nif (metadata.keywords.length !== 50) {\n  keywordError += \n    `Expected 50 keywords but got ${metadata.keywords.length}.`;\n  error = 1;\n}\n\n// Empty keyword\n\nconst emptyKeyword = metadata.keywords.find(k => !k.trim());\n\nif (emptyKeyword !== undefined) {\n    keywordError += \"Empty keyword detected.\";\n  error = 1;\n}\n\n// Duplicate keyword\n\nconst keywordSet = new Set(\n    metadata.keywords.map(k => k.toLowerCase())\n);\n\nif (keywordSet.size !== metadata.keywords.length) {\n    keywordError += \"Duplicate keywords detected.\";\n  error = 1;\n}\n\n// -------------------------\n// Categories\n// -------------------------\n\nif (!VALID_CATEGORIES.includes(metadata.category1)) {\n    categoryError += \n        `Invalid category1: ${metadata.category1}`;\n    error = 1;\n}\n\nif (!VALID_CATEGORIES.includes(metadata.category2)) {\n    categoryError += \n        `Invalid category2: ${metadata.category2}`;\n    error = 1;\n}\n\nif (metadata.category1 === metadata.category2) {\n    categoryError += \"category1 and category2 cannot be the same.\";\n    error = 1;\n}\n\nvar validation = {};\n\nif (error == 0)\n{\n  validation = {\n          passed: true,\n          descriptionLength,\n          keywordCount: metadata.keywords.length,\n          descriptionError,\n          keywordError,\n          categoryError\n        };\n}\nelse {\n  validation = {\n          passed: false,\n          descriptionLength,\n          keywordCount: metadata.keywords.length,\n          descriptionError,\n          keywordError,\n          categoryError,\n        }\n}\n\n// -------------------------\n// Validation Result\n// -------------------------\n\nconst result = {\n      json: {\n        fileName: metadata.fileName,\n        description: metadata.description,\n        keywords: metadata.keywords.join(\", \"),\n        category1: metadata.category1,\n        category2: metadata.category2,\n        ai: metadata.ai,\n        validation\n      }\n    };\n\n\nreturn result;"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1664,
        -48
      ],
      "id": "28a8b00f-90cd-498f-9d77-8e78d7ee9b40",
      "name": "Validation"
    },
    {
      "parameters": {
        "operation": "xlsx",
        "options": {
          "fileName": "=metadata_{{$now.format(\"yyyyMMdd_HHmmss\")}}.xlsx",
          "headerRow": true
        }
      },
      "type": "n8n-nodes-base.convertToFile",
      "typeVersion": 1.1,
      "position": [
        1824,
        -48
      ],
      "id": "328b913b-5136-4783-9b06-5789f081f4d1",
      "name": "Convert to File"
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "return {\n  json: {\n    candidates: [\n      {\n        content: {\n          parts: [\n            {\n              text: JSON.stringify({\n                description:\n                  \"Exterior view of a modern commercial cooperative building featuring multiple red rolling shutter doors, grey concrete walls, and a corrugated metal roof under a bright blue sky.zzzzzzzzzz\",\n                keywords: [\n                  \"commercial building\",\n                  \"cooperative building\",\n                  \"warehouse\",\n                  \"red shutter\",\n                  \"rolling door\",\n                  \"building exterior\",\n                  \"architecture\",\n                  \"business\",\n                  \"facade\",\n                  \"industrial\",\n                  \"storage\",\n                  \"shopfront\",\n                  \"retail\",\n                  \"property\",\n                  \"concrete wall\",\n                  \"metal roof\",\n                  \"garage door\",\n                  \"construction\",\n                  \"commercial property\",\n                  \"outdoor\",\n                  \"daylight\",\n                  \"blue sky\",\n                  \"village\",\n                  \"indonesia\",\n                  \"koperasi\",\n                  \"modern\",\n                  \"building\",\n                  \"real estate\",\n                  \"commercial\",\n                  \"business premises\",\n                  \"facility\",\n                  \"closed shop\",\n                  \"red doors\",\n                  \"structure\",\n                  \"street\",\n                  \"rural\",\n                  \"enterprise\",\n                  \"development\",\n                  \"empty\",\n                  \"clean\",\n                  \"front view\",\n                  \"metal\",\n                  \"wall\",\n                  \"roof\",\n                  \"security\",\n                  \"investment\",\n                  \"trade\",\n                  \"economy\",\n                  \"architecture photography\",\n                  \"commercial space\"\n                ],\n                category1: \"Buildings/Landmarks\",\n                category2: \"Business/Finance\"\n              })\n            }\n          ]\n        }\n      }\n    ],\n    usageMetadata: {\n      promptTokenCount: 1453,\n      candidatesTokenCount: 372,\n      totalTokenCount: 3876,\n      thoughtsTokenCount: 2051,\n      serviceTier: \"standard\"\n    },\n    modelVersion: \"gemini-3.6-flash\",\n    responseId: \"dummy-response-id-001\"\n  }\n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1072,
        96
      ],
      "id": "50da2988-08ba-4f10-b5e3-dbd86caa523c",
      "name": "Dummy Gemini"
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1296,
        -48
      ],
      "id": "33d67666-a3c9-42ef-a4ab-b75953bff01c",
      "name": "Wait"
    },
    {
      "parameters": {
        "operation": "write",
        "fileName": "=/data/excels/metadata_{{$now.format(\"yyyyMMdd_HHmmss\")}}.xlsx",
        "options": {}
      },
      "type": "n8n-nodes-base.readWriteFile",
      "typeVersion": 1.1,
      "position": [
        2000,
        -48
      ],
      "id": "9c981c28-50db-4ab4-b332-924d15a561bc",
      "name": "Save Excels"
    },
    {
      "parameters": {
        "command": "=mv /data/inputs/* /data/processed/"
      },
      "type": "n8n-nodes-base.executeCommand",
      "typeVersion": 1,
      "position": [
        2160,
        -48
      ],
      "id": "4c78b668-2c0d-4efa-9c38-7e60956d7866",
      "name": "Move Files",
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict",
            "version": 3
          },
          "conditions": [
            {
              "id": "d1fa1bb8-8675-4cb5-a340-3f8643416215",
              "leftValue": "={{ $json.error }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.3,
      "position": [
        384,
        -64
      ],
      "id": "1441bf89-7611-47ed-b25a-c25ad5f38719",
      "name": "If",
      "notesInFlow": true,
      "onError": "continueRegularOutput",
      "notes": "No Images Found!"
    },
    {
      "parameters": {
        "jsCode": "return [\n  {\n    json: {\n      status: \"success\",\n      message: \"No images found in /inputs. Workflow finished.\"\n    }\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        640,
        -192
      ],
      "id": "63ee10b7-bc83-42bc-a282-5f615cf95d79",
      "name": "Images Not Found"
    }
  ],
  "connections": {
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Read Prompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Images": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Prompt": {
      "main": [
        [
          {
            "node": "ExtractPrompt",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ExtractImageBinary": {
      "main": [
        [
          {
            "node": "Build Payload",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "ExtractPrompt": {
      "main": [
        [
          {
            "node": "Read Images",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Build Payload": {
      "main": [
        [
          {
            "node": "Call Gemini",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call Gemini": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse Response": {
      "main": [
        [
          {
            "node": "Validation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validation": {
      "main": [
        [
          {
            "node": "Convert to File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Dummy Gemini": {
      "main": [
        []
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Parse Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to File": {
      "main": [
        [
          {
            "node": "Save Excels",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save Excels": {
      "main": [
        [
          {
            "node": "Move Files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Images Not Found",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "ExtractImageBinary",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "binaryMode": "separate",
    "availableInMCP": false
  },
  "versionId": "3cfe9191-649f-4b9c-b5fc-63d126cf68be",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodeGroups": [],
  "id": "SZ646uNG4S1H40Ll",
  "tags": []
}