Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.8 KB
Raw Blame
import type { OnlineDriveFile } from '@/models/pipeline'
import type { OnlineDriveData } from '@/types/pipeline'
import { OnlineDriveFileType } from '@/models/pipeline'

const isFile = (type: 'file' | 'folder'): boolean => {
  return type === 'file'
}

export const isBucketListInitiation = (
  data: OnlineDriveData[],
  prefix: string[],
  bucket: string,
): boolean => {
  if (bucket || prefix.length > 0) return false
  const hasBucket = data.every((item) => !!item.bucket)
  return (
    hasBucket &&
    (data.length > 1 || (data.length === 1 && !!data[0]!.bucket && data[0]!.files.length === 0))
  )
}

export const convertOnlineDriveData = (
  data: OnlineDriveData[],
  prefix: string[],
  bucket: string,
): {
  fileList: OnlineDriveFile[]
  isTruncated: boolean
  nextPageParameters: Record<string, unknown>
  hasBucket: boolean
} => {
  const fileList: OnlineDriveFile[] = []
  let isTruncated = false
  let nextPageParameters: Record<string, unknown> = {}
  let hasBucket = false

  if (data.length === 0) return { fileList, isTruncated, nextPageParameters, hasBucket }

  if (isBucketListInitiation(data, prefix, bucket)) {
    data.forEach((item) => {
      fileList.push({
        id: item.bucket,
        name: item.bucket,
        type: OnlineDriveFileType.bucket,
      })
    })
    hasBucket = true
  } else {
    data[0]!.files.forEach((file) => {
      const { id, name, size, type } = file
      const isFileType = isFile(type)
      fileList.push({
        id,
        name,
        size: isFileType ? size : undefined,
        type: isFileType ? OnlineDriveFileType.file : OnlineDriveFileType.folder,
      })
    })
    isTruncated = data[0]!.is_truncated ?? false
    nextPageParameters = data[0]!.next_page_parameters ?? {}
    hasBucket = !!data[0]!.bucket
  }
  return { fileList, isTruncated, nextPageParameters, hasBucket }
}