diff --git a/README.md b/README.md index 86b103b..3b54ae6 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,8 @@ $ npm start config.js * `masterKey`: Parse master key. * `serverURL`: The URL for the Parse server (default: http://api.parse.com/1). This is used to with `applicationId` and `masterKey` to get the schema and fetch all files/objects. +* `renameFiles` (boolean): Whether or not to rename Parse hosted files. +This removes the "tfss-" or legacy Parse filename prefix before saving with the new file adapter. * `renameInDatabase` (boolean): Whether or not to rename files in MongoDB. * `mongoURL`: MongoDB connection url. Direct access to the database is needed because Parse SDK doesn't allow direct writing to file fields. diff --git a/lib/questions.js b/lib/questions.js index 51a09e9..44d92ad 100644 --- a/lib/questions.js +++ b/lib/questions.js @@ -34,13 +34,24 @@ function questions(config) { {name: 'All files', value: 'all'} ], when: (['parseOnly','parseServerOnly', 'all'].indexOf(config.filesToTransfer) == -1) + }, { + type: 'confirm', + name: 'renameFiles', + message: 'Rename Parse hosted file names?', + default: false, + when: function(answers) { + return config.renameFiles == undefined && + (answers.filesToTransfer == 'all' || config.filesToTransfer == 'all' || + config.filesToTransfer == 'parseOnly' || answers.filesToTransfer == 'parseOnly'); + } }, { type: 'confirm', name: 'renameInDatabase', message: 'Rename Parse hosted files in the database after transfer?', default: false, when: function(answers) { - return !config.renameInDatabase && + return config.renameInDatabase == undefined && + (answers.renameFiles || config.renameFiles) && (answers.filesToTransfer == 'all' || config.filesToTransfer == 'all' || config.filesToTransfer == 'parseOnly' || answers.filesToTransfer == 'parseOnly'); } diff --git a/lib/transfer.js b/lib/transfer.js index 6793a51..8318533 100644 --- a/lib/transfer.js +++ b/lib/transfer.js @@ -8,6 +8,7 @@ var MongoClient = require('mongodb').MongoClient; // regex that matches old legacy Parse hosted files var legacyFilesPrefixRegex = new RegExp("^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}-"); +var migratedFilePrefix = 'mfp_'; var db, config; @@ -90,15 +91,23 @@ function _requestErrorHandler(error, response) { * @param {String} fileName * @return {String} */ -function _nonParseFileName(fileName) { - if (fileName.indexOf('tfss-') === 0) { - return fileName.replace('tfss-', ''); - } else if (legacyFilesPrefixRegex.test(fileName)) { - var newPrefix = crypto.randomBytes(32/2).toString('hex'); - return newPrefix + fileName.replace(legacyFilesPrefixRegex, ''); - } else { +function _createNewFileName(fileName) { + if (!config.renameFiles) { return fileName; } + if (_isParseHostedFile(fileName)) { + fileName = fileName.replace('tfss-', ''); + var newPrefix = crypto.randomBytes(32/2).toString('hex'); + fileName = newPrefix + fileName.replace(legacyFilesPrefixRegex, ''); + } + return migratedFilePrefix + fileName; +} + +function _isParseHostedFile(fileName) { + if (fileName.indexOf('tfss-') === 0 || legacyFilesPrefixRegex.test(fileName)) { + return true; + } + return false; } /** @@ -112,7 +121,7 @@ function _processFiles(files, handler) { return new Promise(function(resolve, reject) { async.eachOfLimit(files, asyncLimit, function(file, index, callback) { process.stdout.write('Processing '+(index+1)+'/'+files.length+'\r'); - file.newFileName = _nonParseFileName(file.fileName); + file.newFileName = _createNewFileName(file.fileName); if (_shouldTransferFile(file)) { _transferFile(file).then(callback, callback); } else { @@ -160,12 +169,12 @@ function _shouldTransferFile(file) { return true; } else if ( config.filesToTransfer == 'parseOnly' && - file.fileName != file.newFileName + _isParseHostedFile(file.fileName) ) { return true; } else if ( config.filesToTransfer == 'parseServerOnly' && - file.fileName == file.newFileName + !_isParseHostedFile(file.fileName) ) { return true; }