# ---------------------------------------------------------------------------
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ---------------------------------------------------------------------------
apiVersion: camel.apache.org/v1
kind: Kamelet
metadata:
  name: dropbox-source
  annotations:
    camel.apache.org/kamelet.support.level: "Stable"
    camel.apache.org/catalog.version: "4.12.0-SNAPSHOT"
    camel.apache.org/kamelet.icon: "data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iVVRGLTgiIHN0YW5kYWxvbmU9Im5vIj8+Cjxzdmcgd2lkdGg9IjQzcHgiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDQzIDQwIiB2ZXJzaW9uPSIxLjEiIGhlaWdodD0iNDBweCI+CiA8cGF0aCBkPSJtMTIuNSAwbC0xMi41IDguMSA4LjcgNyAxMi41LTcuOC04LjctNy4zem0tMTIuNSAyMS45bDEyLjUgOC4yIDguNy03LjMtMTIuNS03LjctOC43IDYuOHptMjEuMiAwLjlsOC44IDcuMyAxMi40LTguMS04LjYtNi45LTEyLjYgNy43em0yMS4yLTE0LjdsLTEyLjQtOC4xLTguOCA3LjMgMTIuNiA3LjggOC42LTd6bS0yMS4xIDE2LjNsLTguOCA3LjMtMy43LTIuNXYyLjhsMTIuNSA3LjUgMTIuNS03LjV2LTIuOGwtMy44IDIuNS04LjctNy4zeiIgZmlsbD0iIzAwN0VFNSIvPgo8L3N2Zz4K"
    camel.apache.org/provider: "Apache Software Foundation"
    camel.apache.org/kamelet.group: "Dropbox"
    camel.apache.org/kamelet.namespace: "Cloud"
  labels:
    camel.apache.org/kamelet.type: "source"
spec:
  definition:
    title: "Dropbox Source"
    description: |-
      Consume Files from Dropbox.
    required:
      - period
      - accessToken
      - clientIdentifier
      - remotePath
      - query
    type: object
    properties:
      period:
        title: Period between Polls
        description: The interval between fetches to the Dropbox remote path in milliseconds
        type: integer
        default: 10000
      accessToken:
        title: Dropbox Access Token
        description: The access Token to use to access Dropbox
        type: string
        format: password
        x-descriptors:
        - urn:camel:group:credentials
      clientIdentifier:
        title: Client Identifier
        description: Dropbox App client Identifier
        type: string
        x-descriptors:
        - urn:camel:group:credentials
      remotePath:
        title: Remote Path
        description: Original file or folder to work with
        type: string
      query:
        title: Queries
        description: A space-separated list of sub-strings to search for. A file matches only if it contains all the sub-strings. If this option is not set, all files will be matched.
        type: string
  dependencies:
    - "camel:dropbox"
    - "camel:kamelet"
    - "camel:core"
    - "camel:jsonpath"
    - "camel:timer"
  template:
    from:
      uri: "timer:dropbox-stream"
      parameters:
        period: "{{period}}"
      steps:
      - to:
          uri: "dropbox:search"
          parameters:
            accessToken: "{{accessToken}}"
            remotePath: "{{remotePath}}"
            clientIdentifier: "{{clientIdentifier}}"
            query: "{{query}}"
      - split:
          jsonpath: "$.*"
          steps:
          - setProperty:
              name: dropboxFileName
              simple: ${body.metadata.pathDisplay}
          - toD: "dropbox:get?accessToken={{accessToken}}&clientIdentifier={{clientIdentifier}}&remotePath=${exchangeProperty.dropboxFileName}"
          - to: "kamelet:sink"
          - toD: "dropbox:del?accessToken={{accessToken}}&clientIdentifier={{clientIdentifier}}&remotePath=${exchangeProperty.dropboxFileName}"

