Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 12 additions & 1 deletion lib/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand Down
29 changes: 19 additions & 10 deletions lib/transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

/**
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down