Skip to content

Commit 4b1f49c

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 287b3fa + af66abe commit 4b1f49c

3 files changed

Lines changed: 34 additions & 10 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ $ npm start config.js
6262
* `masterKey`: Parse master key.
6363
* `serverURL`: The URL for the Parse server (default: http://api.parse.com/1).
6464
This is used to with `applicationId` and `masterKey` to get the schema and fetch all files/objects.
65+
* `renameFiles` (boolean): Whether or not to rename Parse hosted files.
66+
This removes the "tfss-" or legacy Parse filename prefix before saving with the new file adapter.
6567
* `renameInDatabase` (boolean): Whether or not to rename files in MongoDB.
6668
* `mongoURL`: MongoDB connection url.
6769
Direct access to the database is needed because Parse SDK doesn't allow direct writing to file fields.

lib/questions.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,24 @@ function questions(config) {
3434
{name: 'All files', value: 'all'}
3535
],
3636
when: (['parseOnly','parseServerOnly', 'all'].indexOf(config.filesToTransfer) == -1)
37+
}, {
38+
type: 'confirm',
39+
name: 'renameFiles',
40+
message: 'Rename Parse hosted file names?',
41+
default: false,
42+
when: function(answers) {
43+
return config.renameFiles == undefined &&
44+
(answers.filesToTransfer == 'all' || config.filesToTransfer == 'all' ||
45+
config.filesToTransfer == 'parseOnly' || answers.filesToTransfer == 'parseOnly');
46+
}
3747
}, {
3848
type: 'confirm',
3949
name: 'renameInDatabase',
4050
message: 'Rename Parse hosted files in the database after transfer?',
4151
default: false,
4252
when: function(answers) {
43-
return !config.renameInDatabase &&
53+
return config.renameInDatabase == undefined &&
54+
(answers.renameFiles || config.renameFiles) &&
4455
(answers.filesToTransfer == 'all' || config.filesToTransfer == 'all' ||
4556
config.filesToTransfer == 'parseOnly' || answers.filesToTransfer == 'parseOnly');
4657
}

lib/transfer.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var MongoClient = require('mongodb').MongoClient;
99

1010
// regex that matches old legacy Parse hosted files
1111
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}-");
12+
var migratedFilePrefix = 'mfp_';
1213

1314
var db, config;
1415

@@ -102,18 +103,28 @@ function _requestErrorHandler(error, response) {
102103
* @return {String}
103104
*/
104105
function _nonParseFileName(fileName) {
106+
function _createNewFileName(fileName) {
107+
if (!config.renameFiles) {
108+
function _nonParseFileName(fileName) {
105109

106110
// Don't change name.
107111
return fileName;
108112

109-
if (fileName.indexOf('tfss-') === 0) {
110-
return fileName.replace('tfss-', '');
111-
} else if (legacyFilesPrefixRegex.test(fileName)) {
112-
var newPrefix = crypto.randomBytes(32/2).toString('hex');
113-
return newPrefix + fileName.replace(legacyFilesPrefixRegex, '');
114-
} else {
115113
return fileName;
116114
}
115+
if (_isParseHostedFile(fileName)) {
116+
fileName = fileName.replace('tfss-', '');
117+
var newPrefix = crypto.randomBytes(32/2).toString('hex');
118+
fileName = newPrefix + fileName.replace(legacyFilesPrefixRegex, '');
119+
}
120+
return migratedFilePrefix + fileName;
121+
}
122+
123+
function _isParseHostedFile(fileName) {
124+
if (fileName.indexOf('tfss-') === 0 || legacyFilesPrefixRegex.test(fileName)) {
125+
return true;
126+
}
127+
return false;
117128
}
118129

119130
/**
@@ -127,7 +138,7 @@ function _processFiles(files, handler) {
127138
return new Promise(function(resolve, reject) {
128139
async.eachOfLimit(files, asyncLimit, function(file, index, callback) {
129140
process.stdout.write('Processing '+(index+1)+'/'+files.length+'\r');
130-
file.newFileName = _nonParseFileName(file.fileName);
141+
file.newFileName = _createNewFileName(file.fileName);
131142
if (_shouldTransferFile(file)) {
132143
_transferFile(file).then(callback, callback);
133144
} else {
@@ -175,12 +186,12 @@ function _shouldTransferFile(file) {
175186
return true;
176187
} else if (
177188
config.filesToTransfer == 'parseOnly' &&
178-
file.fileName != file.newFileName
189+
_isParseHostedFile(file.fileName)
179190
) {
180191
return true;
181192
} else if (
182193
config.filesToTransfer == 'parseServerOnly' &&
183-
file.fileName == file.newFileName
194+
!_isParseHostedFile(file.fileName)
184195
) {
185196
return true;
186197
}

0 commit comments

Comments
 (0)