/** * Imports the old app.photoreport.pt into an organization (accounts, squads, events, photos, storefront). Read-only on the old * side; photo files are copied. Safe to run again. * * LEGACY_MYSQL_URL=mysql://user:pass@host:3306/db \ * pnpm --filter @photoreport/api import-legacy -- --org-slug photoreport --owner-email you@example.pt \ * --photos-root /srv/old-sftpgo/data --photos-layout "{username}/{folder}" --dry-run * * Start with --dry-run: it counts what would come over and lists event folders it can't find (fix the layout). * --only users,squads,events,photos,shop limits the parts. The MySQL URL comes from the environment, never from argv. */ import { parseArgs } from "node:util"; import pino from "pino"; import { loadConfig } from "../src/config.js"; import { createDb } from "../src/db.js"; import { IngestService } from "../src/ingest/service.js"; import { ALL_PARTS, type ImportPart, LegacyImporter } from "../src/legacy/importer.js"; import { MysqlLegacySource } from "../src/legacy/mysql-source.js"; import { createRemoteBroadcaster } from "../src/realtime.js"; import { LocalStorage, UrlSigner } from "../src/storage/storage.js"; const { values } = parseArgs({ args: process.argv.slice(2).filter((a) => a !== "--"), options: { "org-slug": { type: "string" }, "owner-email": { type: "string" }, "photos-root": { type: "string" }, "photos-layout": { type: "string", default: "{username}/{folder}" }, only: { type: "string" }, "dry-run": { type: "boolean", default: false }, }, }); const url = process.env.LEGACY_MYSQL_URL; if (!url || !values["org-slug"] || !values["owner-email"]) { console.error("Set LEGACY_MYSQL_URL and pass --org-slug and --owner-email (photos without a known owner go to this user)"); process.exit(1); } const parts = values.only ? (values.only.split(",").map((s) => s.trim()) as ImportPart[]) : ALL_PARTS; if (parts.some((p) => !ALL_PARTS.includes(p))) { console.error(`--only takes: ${ALL_PARTS.join(",")}`); process.exit(1); } const config = loadConfig(); const log = pino({ level: config.LOG_LEVEL, name: "import-legacy" }); const db = createDb(config.DATABASE_URL); const storage = new LocalStorage(config.STORAGE_ROOT); const broadcaster = createRemoteBroadcaster(config.DATABASE_URL); const ingest = new IngestService({ db, storage, signer: new UrlSigner(config.JWT_SECRET, config.FILE_URL_TTL_SEC), broadcaster, ingestRoot: config.INGEST_ROOT, log }); const source = MysqlLegacySource.create(url, values["photos-root"] ? { root: values["photos-root"], layout: values["photos-layout"]! } : null); try { const org = await db.organization.findUnique({ where: { slug: values["org-slug"] } }); const owner = await db.user.findUnique({ where: { email: values["owner-email"].trim().toLowerCase() } }); if (!org || !owner) throw new Error("Organization or owner not found (run create-owner first)"); const report = await new LegacyImporter({ db, storage, ingest, source, log }).run(org.id, { parts, dryRun: values["dry-run"], fallbackUserId: owner.id }); console.log(JSON.stringify({ dryRun: values["dry-run"], ...report, photos: { ...report.photos, missingFolders: report.photos.missingFolders.slice(0, 50) } }, null, 2)); if (report.photos.missingFolders.length > 50) console.log(`... and ${report.photos.missingFolders.length - 50} more folders not found`); } finally { await source.close(); await db.$disconnect(); process.exit(0); }