UNPKG

google-photos-migrate

Version:

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

53 lines (52 loc) 2.01 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.saveToDir = saveToDir; const fs_extra_1 = require("fs-extra"); const promises_1 = require("fs/promises"); const path_1 = require("path"); const sanitize_filename_1 = __importDefault(require("sanitize-filename")); async function _saveToDir(file, destDir, saveBase, doMove = false, duplicateIndex = 0) { const saveDir = (0, path_1.resolve)(destDir, duplicateIndex > 0 ? `duplicates-${duplicateIndex}` : '.'); await (0, promises_1.mkdir)(saveDir, { recursive: true }); const savePath = (0, path_1.resolve)(saveDir, saveBase); const exists = await (0, fs_extra_1.pathExists)(savePath); if (exists) { return _saveToDir(file, destDir, saveBase, doMove, duplicateIndex + 1); } if (doMove) { await (0, fs_extra_1.move)(file, savePath); } else { await (0, promises_1.copyFile)(file, savePath); } return savePath; } /** Copies or moves a file to dir, saves duplicates in subfolders and returns the new path. * Atomic within this app, sanitizes filenames. */ async function saveToDir(file, destDir, migCtx, move = false, saveBase) { saveBase = saveBase ?? (0, path_1.basename)(file); let sanitized = (0, sanitize_filename_1.default)(saveBase, { replacement: '_' }); if (sanitized === '') { sanitized = migCtx.renameEmpty; } if (saveBase != sanitized) { migCtx.warnLog(`Sanitized file: ${file}` + ` (New filename: ${sanitized})`); } const lcBase = saveBase.toLowerCase(); let lock; while ((lock = migCtx.migrationLocks.get(lcBase))) { await lock; } lock = _saveToDir(file, destDir, sanitized, move); migCtx.migrationLocks.set(lcBase, lock); try { return await lock; } finally { migCtx.migrationLocks.delete(lcBase); } }