mirror of
https://github.com/langgenius/dify.git
synced 2026-11-12 06:11:12 +08:00
57 lines
2.3 KiB
TypeScript
57 lines
2.3 KiB
TypeScript
import type { DataSourceNotionPage } from '@/models/common'
|
|
import { DataSourceType } from '@/models/datasets'
|
|
import { DatasourceType } from '@/models/pipeline'
|
|
|
|
// data_source_info is written by the data source that imported the document, so its keys are
|
|
// read defensively for each source type.
|
|
type DocumentSourceInfo = Record<string, unknown> | null | undefined
|
|
|
|
const isRecord = (value: unknown): value is Record<string, unknown> =>
|
|
typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
|
|
const getString = (record: Record<string, unknown> | undefined, key: string) => {
|
|
const value = record?.[key]
|
|
return typeof value === 'string' ? value : undefined
|
|
}
|
|
|
|
/** Whether a document source has a file reference supported by the download API. */
|
|
export const isDownloadableFileDocument = (
|
|
sourceType: string | null | undefined,
|
|
info: DocumentSourceInfo,
|
|
) => {
|
|
if (sourceType === DataSourceType.FILE) return true
|
|
if (sourceType !== DatasourceType.localFile) return false
|
|
|
|
return Boolean(
|
|
getString(info ?? undefined, 'real_file_id') ||
|
|
getString(info ?? undefined, 'related_id') ||
|
|
getString(info ?? undefined, 'reference'),
|
|
)
|
|
}
|
|
|
|
const isNotionPageIcon = (
|
|
value: unknown,
|
|
): value is NonNullable<DataSourceNotionPage['page_icon']> =>
|
|
isRecord(value) && typeof value.type === 'string'
|
|
|
|
const toNotionIcon = (value: unknown) =>
|
|
typeof value === 'string' || isNotionPageIcon(value) ? value : undefined
|
|
|
|
/** The file extension of a legacy uploaded-file document. */
|
|
export const getUploadFileExtension = (info: DocumentSourceInfo) =>
|
|
isRecord(info?.upload_file) ? getString(info.upload_file, 'extension') : undefined
|
|
|
|
/** The file extension recorded by a pipeline local-file data source. */
|
|
export const getLocalFileExtension = (info: DocumentSourceInfo) =>
|
|
getString(info ?? undefined, 'extension')
|
|
|
|
/** The file name recorded by a pipeline online-drive data source. */
|
|
export const getOnlineDriveFileName = (info: DocumentSourceInfo) =>
|
|
getString(info ?? undefined, 'name')
|
|
|
|
/** The page icon of a Notion import, or of a pipeline online-document page. */
|
|
export const getNotionPageIcon = (info: DocumentSourceInfo, fromPipeline: boolean) => {
|
|
if (!fromPipeline) return toNotionIcon(info?.notion_page_icon)
|
|
return isRecord(info?.page) ? toNotionIcon(info.page.page_icon) : undefined
|
|
}
|