Skip to content

Instantly share code, notes, and snippets.

@itayw
Created August 6, 2019 09:32
Show Gist options
  • Save itayw/d930ed5b89562814e2be3bf7413d9886 to your computer and use it in GitHub Desktop.
Save itayw/d930ed5b89562814e2be3bf7413d9886 to your computer and use it in GitHub Desktop.
ledorot
const DESTINATION = '/home/itay/dest';
const SOURCE = '/run/user/1000/gvfs/mtp:host=%5Busb%3A001%2C013%5D/Internal shared storage/DCIM';
const { promisify } = require('util');
const { resolve, extname } = require('path');
const fs = require('fs');
const md5File = require('md5-file/promise');
const mkdirp = require('mkdirp');
const readdir = promisify(fs.readdir);
const stat = promisify(fs.stat);
const exists = promisify(fs.exists);
const copyfile = promisify(fs.copyFile);
const unlink = promisify(fs.unlink);
async function getFiles(dir) {
const subdirs = await readdir(dir);
const files = await Promise.all(subdirs.map(async (subdir) => {
const res = resolve(dir, subdir);
const stats = await stat(res);
const isDir = stats.isDirectory();
if (isDir) {
return getFiles(res);
}
else {
const source_filename = res;
const create_time = new Date(stats.ctime);
const target_folder = `${DESTINATION}/${create_time.getFullYear()}-${(create_time.getMonth() + 1).toString().padStart(2, '0')}`;
const target_folder_exists = await exists(target_folder);
if (!target_folder_exists) {
await mkdirp(target_folder);
}
const extension = extname(res);
const hash = await md5File(res);
const new_filename = `${target_folder}/${hash}${extension}`;
const file_exist = await exists(new_filename);
console.log(source_filename, new_filename);
return { source_filename, create_time, target_folder, hash, new_filename, file_exist };
}
}));
return files.reduce((a, f) => a.concat(f), []);
}
async function copyFiles(files) {
const total_files = files.length;
let existing_files = 0;
let copied_files = 0;
let corrupted_files = 0;
for (let file in files) {
file = files[file];
if (!file.file_exist) {
await copyfile(file.source_filename, file.new_filename);
copied_files++;
const new_hash = await md5File(file.new_filename);
if (new_hash !== file.hash) {
corrupted_files++;
unlink(file.new_filename);
}
else {
//unlink(file.source_filename);
}
}
else {
existing_files++;
}
}
return { files, results: { total_files, existing_files, copied_files, corrupted_files } };
}
async function summarize({ files, results }) {
return { files, results };
}
getFiles(SOURCE)
.then(copyFiles)
.then(summarize)
.then(({ files, results }) => console.log(results))
.catch(e => console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment