UNPKG

google-photos-migrate

Version:

A tool to fix EXIF data and recover filenames from a Google Photos takeout.

35 lines (34 loc) 1.24 kB
import { readFile } from 'fs/promises'; import { extname } from 'path'; import { editedSuffices } from '../config/langs.js'; import { walkDir } from '../fs/walk-dir.js'; const MAX_BASE_LENGTH = 51; export async function indexJsonFiles(googleDir) { const titleJsonMap = new Map(); for await (const jsonPath of walkDir(googleDir)) { if (!jsonPath.endsWith('.json')) continue; let title; try { const data = JSON.parse((await readFile(jsonPath)).toString()); title = data.title; } catch (e) { } if (typeof title !== 'string') continue; const potTitles = new Set(); const ext = extname(title); const woExt = title.slice(0, -ext.length); const maxWoExt = MAX_BASE_LENGTH - ext.length; potTitles.add(woExt.slice(0, maxWoExt) + ext); for (const suffix of editedSuffices) { potTitles.add((woExt + `-${suffix}`).slice(0, maxWoExt) + ext); } for (const potTitle of potTitles) { const jsonPaths = titleJsonMap.get(potTitle) ?? []; jsonPaths.push(jsonPath); titleJsonMap.set(potTitle, jsonPaths); } } return titleJsonMap; }