{
  "name": "OneDrive File Manager",
  "nodes": [
    {
      "parameters": {
        "url": "https://graph.microsoft.com/v1.0/me/drive/root/delta?$top=500",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "microsoftOneDriveOAuth2Api",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        -224,
        80
      ],
      "id": "f184d7f8-2fc6-4d86-ae42-a8d6325acc5b",
      "name": "Get All OneDrive Files",
      "credentials": {
        "microsoftOneDriveOAuth2Api": {
          "id": "p0anfxc8QtmkZQsS",
          "name": "Microsoft Drive account"
        }
      }
    },
    {
      "parameters": {
        "path": "onedrive-files",
        "responseMode": "responseNode",
        "options": {}
      },
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        -448,
        80
      ],
      "id": "6c6a729e-f726-4a85-9cd2-7338d6ed753e",
      "name": "GET Files Webhook1",
      "webhookId": "onedrive-files"
    },
    {
      "parameters": {
        "jsCode": "// The delta API returns { value: [...items], '@odata.nextLink': '...' }\nconst response = $input.first().json;\nconst allItems = response.value || [];\n\n// Categorize mime types\nconst typeMap = {\n  // Documents\n  'application/pdf': 'pdf',\n  'application/msword': 'doc',\n  'application/vnd.openxmlformats-officedocument.wordprocessingml.document': 'doc',\n  'text/plain': 'text',\n  'application/rtf': 'doc',\n  // Spreadsheets\n  'application/vnd.ms-excel': 'sheet',\n  'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet': 'sheet',\n  'text/csv': 'sheet',\n  // Presentations\n  'application/vnd.ms-powerpoint': 'slide',\n  'application/vnd.openxmlformats-officedocument.presentationml.presentation': 'slide',\n  // Images\n  'image/jpeg': 'image',\n  'image/png': 'image',\n  'image/gif': 'image',\n  'image/webp': 'image',\n  'image/svg+xml': 'image',\n  'image/bmp': 'image',\n  'image/tiff': 'image',\n  // Videos\n  'video/mp4': 'video',\n  'video/quicktime': 'video',\n  'video/x-msvideo': 'video',\n  'video/webm': 'video',\n  'video/x-matroska': 'video',\n  // Audio\n  'audio/mpeg': 'audio',\n  'audio/wav': 'audio',\n  'audio/x-wav': 'audio',\n  'audio/ogg': 'audio',\n  'audio/flac': 'audio',\n  'audio/aac': 'audio',\n  // Code / other\n  'application/json': 'code',\n  'application/javascript': 'code',\n  'text/html': 'code',\n  'text/css': 'code',\n  'application/zip': 'archive',\n  'application/x-rar-compressed': 'archive',\n  'application/x-7z-compressed': 'archive'\n};\n\n// Helper to guess type from file extension if mime is unknown\nfunction guessTypeFromName(name) {\n  if (!name) return 'other';\n  const ext = name.split('.').pop().toLowerCase();\n  const extMap = {\n    pdf: 'pdf', doc: 'doc', docx: 'doc', txt: 'text', rtf: 'doc',\n    xls: 'sheet', xlsx: 'sheet', csv: 'sheet',\n    ppt: 'slide', pptx: 'slide',\n    jpg: 'image', jpeg: 'image', png: 'image', gif: 'image', webp: 'image', svg: 'image', bmp: 'image',\n    mp4: 'video', mov: 'video', avi: 'video', mkv: 'video', webm: 'video',\n    mp3: 'audio', wav: 'audio', ogg: 'audio', flac: 'audio', aac: 'audio', m4a: 'audio',\n    js: 'code', ts: 'code', py: 'code', html: 'code', css: 'code', json: 'code',\n    zip: 'archive', rar: 'archive', '7z': 'archive'\n  };\n  return extMap[ext] || 'other';\n}\n\n// Separate files and folders\nconst files = [];\nconst folders = [];\n\nfor (const item of allItems) {\n  // Skip the root item itself\n  if (item.root) continue;\n\n  if (item.folder) {\n    folders.push({\n      id: item.id,\n      name: item.name,\n      type: 'folder',\n      childCount: item.folder.childCount || 0,\n      url: item.webUrl || '',\n      path: item.parentReference?.path?.replace('/drive/root:', '') || '/',\n      created: item.createdDateTime || '',\n      modified: item.lastModifiedDateTime || ''\n    });\n  } else if (item.file) {\n    const mimeType = item.file.mimeType || '';\n    const fileType = typeMap[mimeType] || guessTypeFromName(item.name);\n\n    files.push({\n      id: item.id,\n      name: item.name,\n      type: fileType,\n      mimeType: mimeType,\n      url: item.webUrl || '',\n      downloadUrl: item['@microsoft.graph.downloadUrl'] || '',\n      size: item.size || 0,\n      path: item.parentReference?.path?.replace('/drive/root:', '') || '/',\n      created: item.createdDateTime || '',\n      modified: item.lastModifiedDateTime || ''\n    });\n  }\n}\n\n// Sort files by modified date (newest first)\nfiles.sort((a, b) => new Date(b.modified) - new Date(a.modified));\n\n// Check if there are more pages\nconst nextLink = response['@odata.nextLink'] || null;\n\nreturn [\n  {\n    json: {\n      success: true,\n      fileCount: files.length,\n      folderCount: folders.length,\n      hasMore: !!nextLink,\n      nextLink: nextLink,\n      files: files,\n      folders: folders\n    }\n  }\n];"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        0,
        80
      ],
      "id": "90c9168e-00bb-4cda-9ee8-529b9dd28d09",
      "name": "Format Files1"
    },
    {
      "parameters": {
        "respondWith": "allIncomingItems",
        "options": {
          "responseHeaders": {
            "entries": [
              {
                "name": "Access-Control-Allow-Origin",
                "value": "*"
              },
              {
                "name": "Content-Type",
                "value": "application/json"
              }
            ]
          }
        }
      },
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        224,
        80
      ],
      "id": "1f61e215-1fdc-4762-8086-3cc01288a9f0",
      "name": "Respond to Webhook1"
    }
  ],
  "pinData": {},
  "connections": {
    "Get All OneDrive Files": {
      "main": [
        [
          {
            "node": "Format Files1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GET Files Webhook1": {
      "main": [
        [
          {
            "node": "Get All OneDrive Files",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Files1": {
      "main": [
        [
          {
            "node": "Respond to Webhook1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": true,
  "settings": {
    "executionOrder": "v1",
    "availableInMCP": false
  },
  "versionId": "c6bf6f7e-600a-4d35-93ec-2640946cbaf0",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "de60ded3609ed675dd3435a79fde884038d2348f816754f1f978c92a10e2da20"
  },
  "id": "-FcxLR9cEZ3Y80j036eHV",
  "tags": []
}