Avoid creating circular dependencies in drive folders (#10193)

Signed-off-by: Artem Savchenko <armisav@gmail.com>
This commit is contained in:
Artyom Savchenko
2025-11-06 23:04:31 +04:00
committed by GitHub
parent ca83fa264b
commit e10a05b42e
2 changed files with 35 additions and 19 deletions
@@ -15,20 +15,18 @@
<script lang="ts">
import { Ref } from '@hcengineering/core'
import type { Drive, Folder, Resource } from '@hcengineering/drive'
import presentation, { Card, getClient, SpaceSelector } from '@hcengineering/presentation'
import presentation, { Card, SpaceSelector } from '@hcengineering/presentation'
import view from '@hcengineering/view'
import { ObjectBox } from '@hcengineering/view-resources'
import { createEventDispatcher } from 'svelte'
import drive from '../plugin'
import { moveResources } from '../utils'
import { findAllChildren, moveResources } from '../utils'
import ResourcePresenter from './ResourcePresenter.svelte'
export let value: Resource
const client = getClient()
const hierarchy = client.getHierarchy()
const dispatch = createEventDispatcher()
let space: Ref<Drive> = value.space as Ref<Drive>
@@ -42,21 +40,7 @@
$: void updateChildren(value)
async function updateChildren (resource: Resource): Promise<void> {
children = await findChildren(resource)
}
async function findChildren (resource: Resource): Promise<Array<Ref<Folder>>> {
if (hierarchy.isDerived(resource._class, drive.class.Folder)) {
const children = await client.findAll(
drive.class.Folder,
{ space: resource.space, path: resource._id as Ref<Folder> },
{ projection: { _id: 1 } }
)
return children.map((p) => p._id)
}
return []
children = await findAllChildren(resource)
}
$: canSave = space !== value.space || parent !== value.parent
+32
View File
@@ -169,6 +169,38 @@ export async function resolveParents (object: Resource): Promise<Doc[]> {
return parents.reverse()
}
export async function findAllChildren (resource: Resource, maxDepth: number = 10): Promise<Array<Ref<Folder>>> {
const client = getClient()
const hierarchy = client.getHierarchy()
if (!hierarchy.isDerived(resource._class, drive.class.Folder)) {
return []
}
const allChildren: Array<Ref<Folder>> = []
let currentLevel: Array<Ref<Folder>> = [resource._id as Ref<Folder>]
let depth = 0
while (currentLevel.length > 0 && depth < maxDepth) {
const children = await client.findAll(
drive.class.Folder,
{ space: resource.space, parent: { $in: currentLevel } },
{ projection: { _id: 1 } }
)
if (children.length === 0) {
break
}
const childIds = children.map((p) => p._id)
allChildren.push(...childIds)
currentLevel = childIds
depth++
}
return allChildren
}
export async function getUploadOptionsByFragment (space: Ref<Drive>, fragment: string): Promise<FileUploadOptions> {
const [, _id, _class] = decodeURIComponent(fragment).split('|')
if (_class === drive.class.Folder) {