Skip to content

A full-featured download manager.

2.7 KB
Raw Blame
import type { EngineAdapter } from '@core/engine/engine-adapter'
import type { EventBus } from '@core/events/event-bus'
import { hasLegacyImport } from '@core/legacy-import/legacy-task-policy'
import { getLogger } from '@core/logger'
import type { MotrixDatabase } from '@core/session/motrix-database'
import type { TaskManager } from '@core/task/task-manager'
import { AppError, ErrorCode } from '@shared/errors'
import { formatFileSelection } from '@shared/lib/file-selection-ranges'
import { Events } from '@shared/protocol/events'
import { setSelectedFilesPayloadSchema } from '@shared/schemas/task-file-selection'
import type { DownloadTask } from '@shared/types/task'

const log = getLogger('SetSelectedFilesHandler')

interface Deps {
  authorizeTaskWrite?: (task: DownloadTask) => Promise<void>
  taskManager: Pick<TaskManager, 'getById' | 'set'>
  engine: Pick<EngineAdapter, 'changeOption' | 'getTaskFiles'>
  db: Pick<MotrixDatabase, 'replaceTaskFiles'>
  eventBus: Pick<EventBus, 'emit'>
  runTaskMutation: <T>(
    taskIds: readonly string[],
    operation: () => Promise<T>
  ) => Promise<T>
}

export function createSetSelectedFilesHandler(deps: Deps) {
  return async function setSelectedFiles(rawPayload: unknown): Promise<void> {
    const parsed = setSelectedFilesPayloadSchema.safeParse(rawPayload)
    if (!parsed.success) {
      throw new AppError(
        ErrorCode.InvalidSelection,
        parsed.error.issues[0]?.message ?? 'Invalid file selection'
      )
    }
    const { taskId, indices } = parsed.data

    await deps.runTaskMutation([taskId], async () => {
      const task = deps.taskManager.getById(taskId)
      if (!task) {
        throw new AppError(ErrorCode.TaskNotFound, `Task ${taskId} not found`)
      }

      if (hasLegacyImport(task))
        throw new AppError(
          ErrorCode.InvalidSelection,
          'legacyImport.activationRequired'
        )
      await deps.authorizeTaskWrite?.(task)
      await deps.engine.changeOption(task.engineTaskId, {
        'select-file': formatRange(indices),
      })

      const live = await deps.engine.getTaskFiles(task.engineTaskId)
      deps.db.replaceTaskFiles(
        taskId,
        live.map((file) => ({
          fileIndex: file.index,
          path: file.path,
          size: file.size,
          selected: file.selected,
        }))
      )

      if (task.bt) {
        deps.taskManager.set(taskId, {
          ...task,
          bt: { ...task.bt, selectedFiles: indices },
        })
      }
      deps.eventBus.emit(Events.TaskFilesUpdated, { taskId })
      log.info({ taskId, count: indices.length }, 'selected files updated')
    })
  }
}

export function formatRange(indices: number[]): string {
  return formatFileSelection(indices, 0)
}