Skip to content

Commit af66abe

Browse files
author
Arthur Cinader
authored
Merge pull request #9 from JeremyPlease/fix-file-renaming
Fix Parse file renaming so tfss- files don't become legacy files
2 parents 8c6b03f + d1d5f89 commit af66abe

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

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: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ var MongoClient = require('mongodb').MongoClient;
88

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

1213
var db, config;
1314

@@ -90,15 +91,23 @@ function _requestErrorHandler(error, response) {
9091
* @param {String} fileName
9192
* @return {String}
9293
*/
93-
function _nonParseFileName(fileName) {
94-
if (fileName.indexOf('tfss-') === 0) {
95-
return fileName.replace('tfss-', '');
96-
} else if (legacyFilesPrefixRegex.test(fileName)) {
97-
var newPrefix = crypto.randomBytes(32/2).toString('hex');
98-
return newPrefix + fileName.replace(legacyFilesPrefixRegex, '');
99-
} else {
94+
function _createNewFileName(fileName) {
95+
if (!config.renameFiles) {
10096
return fileName;
10197
}
98+
if (_isParseHostedFile(fileName)) {
99+
fileName = fileName.replace('tfss-', '');
100+
var newPrefix = crypto.randomBytes(32/2).toString('hex');
101+
fileName = newPrefix + fileName.replace(legacyFilesPrefixRegex, '');
102+
}
103+
return migratedFilePrefix + fileName;
104+
}
105+
106+
function _isParseHostedFile(fileName) {
107+
if (fileName.indexOf('tfss-') === 0 || legacyFilesPrefixRegex.test(fileName)) {
108+
return true;
109+
}
110+
return false;
102111
}
103112

104113
/**
@@ -112,7 +121,7 @@ function _processFiles(files, handler) {
112121
return new Promise(function(resolve, reject) {
113122
async.eachOfLimit(files, asyncLimit, function(file, index, callback) {
114123
process.stdout.write('Processing '+(index+1)+'/'+files.length+'\r');
115-
file.newFileName = _nonParseFileName(file.fileName);
124+
file.newFileName = _createNewFileName(file.fileName);
116125
if (_shouldTransferFile(file)) {
117126
_transferFile(file).then(callback, callback);
118127
} else {
@@ -160,12 +169,12 @@ function _shouldTransferFile(file) {
160169
return true;
161170
} else if (
162171
config.filesToTransfer == 'parseOnly' &&
163-
file.fileName != file.newFileName
172+
_isParseHostedFile(file.fileName)
164173
) {
165174
return true;
166175
} else if (
167176
config.filesToTransfer == 'parseServerOnly' &&
168-
file.fileName == file.newFileName
177+
!_isParseHostedFile(file.fileName)
169178
) {
170179
return true;
171180
}

0 commit comments

Comments
 (0)