google-photos-migrate
Version:
A tool to fix EXIF data and recover filenames from a Google Photos takeout.
61 lines (60 loc) • 2.38 kB
JavaScript
import { pathExists } from 'fs-extra';
import { basename, dirname } from 'path';
import { editedSuffices } from '../config/langs.js';
export async function findMetaFile(mediaPath, ext, migCtx) {
const title = basename(mediaPath);
// Most json files can be matched by indexed titles
const indexedJson = migCtx.titleJsonMap.get(title);
if (indexedJson) {
const sameFolder = indexedJson.filter((jsonPath) => dirname(jsonPath) === dirname(mediaPath));
if (sameFolder.length === 1) {
return sameFolder[0];
}
}
// Otherwise, try (from most to least significant)
const potPaths = new Set();
const pushWithPotExt = (base, potExt) => {
const potBases = [];
// <name>(.<ext|extAlias>)?.json
potBases.push(`${base}${potExt}`);
// Stolen from https://github.com/mattwilson1024/google-photos-exif/blob/master/src/helpers/get-companion-json-path-for-media-file.ts
const nameCounterMatch = base.match(/(?<name>.*)(?<counter>\(\d+\))$/);
const name = nameCounterMatch?.groups?.['name'];
const counter = nameCounterMatch?.groups?.['counter'];
if (name !== undefined && counter !== undefined) {
// <file>(.<ext|extAlias>)?(n).json
potBases.push(`${name}${potExt}${counter}`);
}
// <file>(_n-?|_n?|_?)(.<ext|extAlias>)?.json
if (base.endsWith('_n-') || base.endsWith('_n') || base.endsWith('_')) {
potBases.push(`${base.slice(0, -1)}${potExt}`);
}
for (const potBase of potBases) {
potPaths.add(`${potBase}.json`);
}
};
let base = mediaPath.slice(0, mediaPath.length - ext.suffix.length);
for (const suffix of editedSuffices) {
const prevLen = base.length;
base = base.replace(`-${suffix}`, '');
if (prevLen !== base.length)
break;
}
const potExts = [];
// <name>.<ext>.json
potExts.push(ext.suffix);
// <name>.<extAlias>.json
potExts.push(...(ext.aliases ?? []).map((a) => (typeof a === 'string' ? a : a.suffix)));
// <name>.json
potExts.push('');
for (const potExt of potExts) {
pushWithPotExt(base, potExt);
}
for (const potPath of potPaths) {
if (!(await pathExists(potPath))) {
continue;
}
return potPath;
}
return null;
}