google-photos-migrate
Version:
A tool to fix EXIF data and recover filenames from a Google Photos takeout.
64 lines (63 loc) • 2.53 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.findMetaFile = findMetaFile;
const fs_extra_1 = require("fs-extra");
const path_1 = require("path");
const langs_1 = require("../config/langs");
async function findMetaFile(mediaPath, ext, migCtx) {
const title = (0, path_1.basename)(mediaPath);
// Most json files can be matched by indexed titles
const indexedJson = migCtx.titleJsonMap.get(title);
if (indexedJson) {
const sameFolder = indexedJson.filter((jsonPath) => (0, path_1.dirname)(jsonPath) === (0, path_1.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 langs_1.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 (0, fs_extra_1.pathExists)(potPath))) {
continue;
}
return potPath;
}
return null;
}