|
| 1 | +import * as fs from 'node:fs' |
| 2 | +import * as path from 'node:path' |
| 3 | +import { createHash } from 'node:crypto' |
| 4 | +import { getPaths } from '@siastorage/node-adapters' |
| 5 | +import { daemonCommand, ensureDaemonRunning } from '../daemon/supervisor' |
| 6 | +import { createDaemonClient } from '../lib/appServiceClient' |
| 7 | +import { c, formatBytes } from '../lib/format' |
| 8 | +import { normalizePath } from '../lib/normalizePath' |
| 9 | + |
| 10 | +type ImportOpts = { |
| 11 | + dryRun?: boolean |
| 12 | + skipExisting?: boolean |
| 13 | +} |
| 14 | + |
| 15 | +export async function importCommand( |
| 16 | + dataDir: string, |
| 17 | + localPath: string, |
| 18 | + remoteDir: string | undefined, |
| 19 | + opts: ImportOpts, |
| 20 | +) { |
| 21 | + const absPath = path.resolve(localPath) |
| 22 | + |
| 23 | + const stat = fs.statSync(absPath, { throwIfNoEntry: false }) |
| 24 | + if (!stat) { |
| 25 | + console.error(`Path not found: ${absPath}`) |
| 26 | + process.exit(1) |
| 27 | + } |
| 28 | + if (!stat.isDirectory()) { |
| 29 | + console.error(`Not a directory: ${absPath}`) |
| 30 | + console.error('Use "sia add" for single files.') |
| 31 | + process.exit(1) |
| 32 | + } |
| 33 | + |
| 34 | + const baseName = remoteDir ? normalizePath(remoteDir) : path.basename(absPath) |
| 35 | + const files = walkDirectory(absPath) |
| 36 | + |
| 37 | + if (files.length === 0) { |
| 38 | + console.log('No files found.') |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + const totalSize = files.reduce((sum, f) => sum + f.size, 0) |
| 43 | + |
| 44 | + if (opts.dryRun) { |
| 45 | + console.log( |
| 46 | + `Would import ${files.length} files (${formatBytes(totalSize)}) into ${baseName}/\n`, |
| 47 | + ) |
| 48 | + for (const f of files) { |
| 49 | + const remotePath = path.join(baseName, f.relativePath) |
| 50 | + console.log(` ${remotePath} ${c.dim(`(${formatBytes(f.size)})`)}`) |
| 51 | + } |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + const p = getPaths(dataDir) |
| 56 | + await ensureDaemonRunning(p) |
| 57 | + const app = createDaemonClient(p.sockPath) |
| 58 | + |
| 59 | + let imported = 0 |
| 60 | + let skipped = 0 |
| 61 | + let importedBytes = 0 |
| 62 | + |
| 63 | + console.log(`Importing from ${absPath} -> ${baseName}/\n`) |
| 64 | + |
| 65 | + for (const file of files) { |
| 66 | + const relDir = path.dirname(file.relativePath) |
| 67 | + const directory = relDir === '.' ? baseName : path.join(baseName, relDir) |
| 68 | + const displayPath = path.join(baseName, file.relativePath) |
| 69 | + |
| 70 | + if (opts.skipExisting) { |
| 71 | + const data = fs.readFileSync(file.absolutePath) |
| 72 | + const hash = createHash('sha256').update(data).digest('hex') |
| 73 | + const existing = await app.files.getByContentHash(hash) |
| 74 | + if (existing) { |
| 75 | + skipped++ |
| 76 | + console.log( |
| 77 | + ` ${c.dim(`[${pad(imported + skipped, files.length)}/${files.length}]`)} ${c.dim(displayPath)} ${c.dim('(skipped)')}`, |
| 78 | + ) |
| 79 | + continue |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const result = (await daemonCommand(p, 'upload', { |
| 84 | + path: file.absolutePath, |
| 85 | + directory, |
| 86 | + })) as { id: string; name: string; size: number; type: string } |
| 87 | + |
| 88 | + imported++ |
| 89 | + importedBytes += result.size |
| 90 | + const typeBadge = result.type.split('/')[1] ?? result.type |
| 91 | + console.log( |
| 92 | + ` ${c.dim(`[${pad(imported + skipped, files.length)}/${files.length}]`)} ${displayPath} ${c.dim(typeBadge)} ${c.dim(`(${formatBytes(result.size)})`)}`, |
| 93 | + ) |
| 94 | + } |
| 95 | + |
| 96 | + console.log( |
| 97 | + `\nImported ${imported} files (${formatBytes(importedBytes)})` + |
| 98 | + (skipped > 0 ? `, skipped ${skipped}` : ''), |
| 99 | + ) |
| 100 | +} |
| 101 | + |
| 102 | +type FileEntry = { |
| 103 | + absolutePath: string |
| 104 | + relativePath: string |
| 105 | + size: number |
| 106 | +} |
| 107 | + |
| 108 | +function walkDirectory(dir: string, base: string = ''): FileEntry[] { |
| 109 | + const entries: FileEntry[] = [] |
| 110 | + const items = fs.readdirSync(dir, { withFileTypes: true }) |
| 111 | + |
| 112 | + for (const item of items) { |
| 113 | + const absPath = path.join(dir, item.name) |
| 114 | + const relPath = base ? path.join(base, item.name) : item.name |
| 115 | + |
| 116 | + if (item.isDirectory()) { |
| 117 | + entries.push(...walkDirectory(absPath, relPath)) |
| 118 | + } else if (item.isFile()) { |
| 119 | + const stat = fs.statSync(absPath) |
| 120 | + entries.push({ absolutePath: absPath, relativePath: relPath, size: stat.size }) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return entries |
| 125 | +} |
| 126 | + |
| 127 | +function pad(n: number, total: number): string { |
| 128 | + return String(n).padStart(String(total).length, ' ') |
| 129 | +} |
0 commit comments