Skip to content
Permalink
Browse files

Adds method to resolve case of files

  • Loading branch information
davidmerfield committed Dec 16, 2019
1 parent 1c434c7 commit 5dfda243a94cdd2d5e4e6a1c8255f82aacd6593e
Showing with 69 additions and 20 deletions.
  1. +69 −20 app/clients/dropbox/routes/writeExistingContents.js
@@ -5,6 +5,7 @@ var sync = require("sync");
var upload = require("../util/upload");
var join = require("path").join;
var Metadata = require("metadata");
var Path = require('path');

module.exports = function(req, res, next) {
var walk, walked, queue, localFolder, dropboxFolder, token;
@@ -46,38 +47,86 @@ module.exports = function(req, res, next) {
});
};

// Takes a file or folder whose name is not fully
// lowercase and to make it lowercase. For example:
//
// /foo/BaR.jpg --> /foo/bar.jpg
//
// This function is more complicated because we don't
// want to clobber /foo/bar/baz.jpg if it already exists
// so we run down a list of options. Returns the
// name of the lowercased file.
function ensureLowerCase (directory, name, callback){

var currentPath, parsedPath, names;

if (name === name.toLowerCase()) return callback(null, name);

if (directory !== directory.toLowerCase()) return callback(new Error('Directory must be lowercase'));

currentPath = join(directory, name);

parsedPath = Path.parse(currentPath.toLowerCase());

names = [
name.toLowerCase(),
parsedPath.name + ' (conflict)' + parsedPath.ext,
];

for (var i=1;i<100;i++) {
names.push(parsedPath.name + ' (conflict ' + i + ')' + parsedPath.ext);
}

async.eachSeries(names, function(paths, next){
fs.move(currentPath, join(directory, name), function(err){
if (err) return next();
callback(null, name);
});
}, function(){
callback(new Error("Ran out of candidates to lowercase path"));
});
}

function walker(blogID, localFolder, dropboxFolder, queue) {
return function walk(path, callback) {
debug("iterating", path);

fs.readdir(localFolder + path, function(err, contents) {
async.eachSeries(
contents,
function(name, next) {
function handleItem (name, next) {
debug(". ", join(localFolder, path, name));

fs.stat(join(localFolder, path, name), function(err, stat) {
// We lowercase the local path to the file because
// when the Dropbox client syncs, it writes lowercase
// files only. Without this step, you end up with duplicated
// files and folders, one case-preserved, one lowercase
ensureLowerCase(join(localFolder, path), name, function(err, name){
if (err) return next(err);

if (stat.isDirectory()) {
return walk(join(path, name), next);
}

// PRESERVE CASE ISING CALLS TO BLOT"S METADATA MODEL
Metadata.get(blogID, join(path, name), function(
err,
casePreservedName
) {
queue.push({
source: join(localFolder, path, name),
destination: join(
dropboxFolder,
path,
casePreservedName || name
)
fs.stat(join(localFolder, path, name), function(err, stat) {
if (err) return next(err);

if (stat.isDirectory()) {
return walk(join(path, name), next);
}

// Fetches case-preserved name of the file
Metadata.get(blogID, join(path, name), function(
err,
casePreservedName
) {
queue.push({
source: join(localFolder, path, name),
destination: join(
dropboxFolder,
path,
casePreservedName || name
)
});

next();
});

next();
});
});
},

0 comments on commit 5dfda24

Please sign in to comment.
You can’t perform that action at this time.