Created
January 28, 2012 05:35
-
-
Save timoxley/1692870 to your computer and use it in GitHub Desktop.
figure out how to write file from src/dir file/dir status
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// figures out how to write the file based on source, destination | |
// eg | |
// source = a/f.jpg | |
// dest = /tmp | |
// result = /tmp/f.jpg | |
// | |
// source = a/f.jpg | |
// dest = tmp/g.jpg | |
// result = tmp/g.jpg | |
function getWritePathFromSource(source, destination, callback) { | |
var path = require('path') | |
var fs = require('fs') | |
path.exists(destination, function(err, exists) { | |
if (exists) { | |
fs.stat(destination, function(stat) { | |
stat.isFile(function(err, isFile) { | |
if (isFile) { | |
callback(null, path.normalize(destination)) | |
} else { | |
var filename = path.basename(source) | |
callback(null, path.join(destination,filename)) | |
} | |
}) | |
}) | |
} else { | |
if (path.extname(destination) === '') { | |
var filename = path.basename(source) | |
callback(null, path.join(destination, filename)) | |
} else { | |
callback(null, path.normalize(destination)) | |
} | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment