/ opencodehub / src / lib / sync-storage.ts
Swadhin Biswas feat(api): add runner, analytics, packages, and security services · 3/6/2026
28f7954
155 lines · 4.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import { logger } from "@/lib/logger";
import { spawn } from "child_process";
import path from "path";
import fs from "fs/promises";
import { getStorageConfig } from "./storage";

const RCLONE_BIN = "rclone";
const DEFAULT_CONFIG_PATH = path.join(process.cwd(), "data", "rclone", "rclone.conf");

export interface SyncOptions {
    dryRun?: boolean;
    delete?: boolean; // --delete-during
    verbose?: boolean;
}

export interface SyncResult {
    success: boolean;
    output: string;
    error?: string;
}

/**
 * Get the path to the rclone config file
 */
function getConfigPath(): string {
    return process.env.RCLONE_CONFIG_PATH || DEFAULT_CONFIG_PATH;
}

/**
 * Ensure rclone config directory exists
 */
export async function ensureRcloneConfig(): Promise<void> {
    const configPath = getConfigPath();
    const dir = path.dirname(configPath);
    try {
        await fs.mkdir(dir, { recursive: true });
        // If config doesn't exist, create an empty one
        try {
            await fs.access(configPath);
        } catch {
            await fs.writeFile(configPath, "# Rclone configuration file\n");
        }
    } catch (err) {
        logger.error("Failed to ensure rclone config:", err);
    }
}

/**
 * Run rclone sync from local to remote
 * @param sourcePath Local path (relative to project root or absolute)
 * @param remoteDest Remote destination (remote:path)
 */
export async function syncStorage(
    sourcePath: string,
    remoteDest: string,
    options: SyncOptions = {}
): Promise<SyncResult> {
    const configPath = getConfigPath();

    // Resolve source path
    const resolvedSource = path.isAbsolute(sourcePath)
        ? sourcePath
        : path.join(process.cwd(), sourcePath);

    const args = [
        "sync",
        resolvedSource,
        remoteDest,
        "--config", configPath,
        "--progress",
    ];

    if (options.dryRun) args.push("--dry-run");
    if (options.delete !== false) args.push("--delete-during"); // Default to delete
    if (options.verbose) args.push("-v");

    return runRclone(args);
}

/**
 * Run rclone sync from remote to local
 */
export async function restoreStorage(
    remoteSource: string,
    destPath: string,
    options: SyncOptions = {}
): Promise<SyncResult> {
    const configPath = getConfigPath();

    // Resolve dest path
    const resolvedDest = path.isAbsolute(destPath)
        ? destPath
        : path.join(process.cwd(), destPath);

    const args = [
        "sync",
        remoteSource,
        resolvedDest,
        "--config", configPath,
        "--progress",
    ];

    if (options.dryRun) args.push("--dry-run");
    if (options.delete !== false) args.push("--delete-during");
    if (options.verbose) args.push("-v");

    return runRclone(args);
}

/**
 * List configured remotes
 */
export async function listRemotes(): Promise<string[]> {
    const configPath = getConfigPath();
    const result = await runRclone(["listremotes", "--config", configPath]);

    if (result.success) {
        return result.output
            .split("\n")
            .map(line => line.trim())
            .filter(Boolean)
            .map(remote => remote.replace(":", ""));
    }
    return [];
}

/**
 * Internal helper to run rclone command
 */
async function runRclone(args: string[]): Promise<SyncResult> {
    logger.info(`[Rclone] Running: rclone ${args.join(" ")}`);

    return new Promise((resolve) => {
        const proc = spawn(RCLONE_BIN, args);
        let output = "";
        let error = "";

        proc.stdout.on("data", (data) => { output += data.toString(); });
        proc.stderr.on("data", (data) => { error += data.toString(); });

        proc.on("close", (code) => {
            if (code === 0) {
                resolve({ success: true, output });
            } else {
                logger.error(`[Rclone] Failed with code ${code}: ${error}`);
                resolve({ success: false, output, error: error || "Process failed" });
            }
        });

        proc.on("error", (err) => {
            resolve({ success: false, output: "", error: `Failed to spawn rclone: ${err.message}` });
        });
    });
}