google-photos-migrate
Version:
A tool to fix EXIF data and recover filenames from a Google Photos takeout.
54 lines (53 loc) • 2.39 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.restructureAndProcess = restructureAndProcess;
const promises_1 = require("fs/promises");
const path_1 = require("path");
const langs_1 = require("../config/langs");
const migrate_flat_1 = require("./migrate-flat");
async function* _restructureAndProcess(folders, processingAlbums, // true for Albums, false for Photos
migCtx) {
for (const folder of folders) {
processingAlbums && migCtx.log(`Processing album ${folder}...`);
let albumName = processingAlbums ? (0, path_1.basename)(folder) : 'Photos';
for (const untitledName of langs_1.untitledDirs) {
if (albumName.startsWith(`${untitledName}(`)) {
albumName = untitledName;
}
}
const outDir = `${migCtx.outputDir}/${albumName}`;
const errDir = `${migCtx.errorDir}/${albumName}`;
await (0, promises_1.mkdir)(outDir, { recursive: true });
await (0, promises_1.mkdir)(errDir, { recursive: true });
yield* (0, migrate_flat_1.migrateDirFlatGen)({
...migCtx,
inputDir: folder,
outputDir: outDir,
errorDir: errDir,
});
}
}
async function* restructureAndProcess(sourceDir, migCtx) {
// before
// $rootdir/My Album 1/*
// $rootdir/My Album 2/*
// $rootdir/Photos from 2008/*
// after
// $rootdir/AlbumsProcessed/My Album 1/*
// $rootdir/AlbumsProcessed/My Album 2/*
// $rootdir/PhotosProcessed/*
const dirents = await (0, promises_1.readdir)(sourceDir, { withFileTypes: true });
const allDirs = dirents.filter((f) => f.isDirectory());
// move the "Photos from $YEAR" directories to Photos/
migCtx.log('Processing photos...');
const photosFromDirs = new Set(allDirs
.filter((f) => f.name === 'Photos' || f.name.startsWith('Photos from '))
.map((f) => (0, path_1.join)(f.parentPath, f.name)));
yield* _restructureAndProcess([...photosFromDirs], false, migCtx);
// move everythingg else to Albums/, so we end up with two top level folders
migCtx.log('Processing albums...');
const albumDirs = allDirs
.filter((f) => !photosFromDirs.has((0, path_1.join)(f.parentPath, f.name)))
.map((f) => (0, path_1.join)(f.parentPath, f.name));
yield* _restructureAndProcess(albumDirs, true, migCtx);
}