Skip to content

Instantly share code, notes, and snippets.

@akhoury
Last active October 4, 2016 19:41
Show Gist options
  • Save akhoury/5462695d5b4d7b21534f725ba499697f to your computer and use it in GitHub Desktop.
Save akhoury/5462695d5b4d7b21534f725ba499697f to your computer and use it in GitHub Desktop.
var path = require('path');
var nconf = require('nconf');
var async = require('async');
nconf.file({ file: path.join(__dirname, './config.json') });
var db = require('./src/database');
var S3_BASE_URL = 'https://nodebb-assests.s3-us-west-2.amazonaws.com/';
var S3_PROD_URL = 'https://venue-forum-prod.s3-us-west-2.amazonaws.com/';
var ENVS = {
prod: S3_PROD_URL,
dev: S3_BASE_URL + 'DEV',
qa: S3_BASE_URL + 'QA',
uat: S3_BASE_URL + 'UAT'
}
var S3_URL = '';
var SAVE = false;
var VERBOSE = false;
process.argv.forEach(function (val, index) {
val = val.toLowerCase();
if (val == "--save" || val == "-s") {
SAVE = true;
}
if (val == "--verbose" || val == "-v") {
VERBOSE = true;
}
if (ENVS[val]) {
S3_URL = ENVS[val];
}
});
if (!S3_URL) {
throw 'You must choose which funimation-env you are in, out of these 4 [prod, dev, qa, uat] i.e. `node batch-point-locals-to-s3.js dev --save --verbose`'
}
if (!SAVE) {
console.warn('You did not include the `--save` argument, which means this is a demo run and will not write anything to the database');
VERBOSE = true;
}
var POSTS_REGEXP = /\/uploads\/_imported_attachments\//g;
var POSTS_REPLACE_WITH = S3_URL + '/_imported_attachments/'
var USERS_REGEXP = /\/uploads\/_imported_profiles\//g;
var USERS_REPLACE_WITH = S3_URL + '/_imported_profiles/'
if (db.client) {
onDatabaseInit();
} else {
db.init(onDatabaseInit)
}
function onDatabaseInit () {
var batch = require('./src/batch');
async.series([
processAllPosts,
processAllUsers
], function(err) {
if (err) throw err;
console.log('\n\n\nALL DONE!\n\n');
if (!SAVE) {
console.warn('You did not include the `--save` argument, '
+ 'which means this is was demo run and did not write anything to the database, '
+ 'check the logs, make sure everything looks good and all new URLs work, '
+ 'then run `node batch-point-locals-to-s3.js dev --save --verbose` \n\n'
);
}
process.exit(0);
});
function processAllPosts (next) {
batch.processSortedSet('posts:pid', function(pids, next) {
async.eachSeries(pids, function(pid, next) {
db.getObject('post:' + pid, function(err, post) {
if (err) return next(err);
var content = post.content;
if (!content || !POSTS_REGEXP.test(content)) {
return next();
}
if (VERBOSE) {
console.log('replacing post pid:' + post.pid + ' content'
, '\n\n\n\n\n\n\n<<<<<<<< from >>>>>>>> \n\n' + content
, '\n\n\n\n<<<<<<<< to >>>>>>>> \n\n' + content.replace(POSTS_REGEXP, POSTS_REPLACE_WITH)
);
}
content = content.replace(POSTS_REGEXP, POSTS_REPLACE_WITH);
if (SAVE) {
db.setObjectField('post:' + pid, 'content', content, next);
} else {
next();
}
});
}, next);
}, {}, function(err) {
if (err) {
return next(err);
}
console.log('\n\nreplacing posts content done');
next();
});
}
function processAllUsers (next) {
batch.processSortedSet('users:joindate', function(uids, next) {
async.eachSeries(uids, function(uid, next) {
db.getObject('user:' + uid, function(err, user) {
if (err) return next(err);
var picture = user.picture || '';
var uploadedpicture = user.uploadedpicture || '';
if (!USERS_REGEXP.test(picture)) {
return next();
}
if (VERBOSE) {
console.log('\nreplacing user uid:' + user.uid + ' picture and uploadedpicture'
, '\n\n\n\n\n\n\n<<<<<<<< from >>>>>>>> \n\n' + picture
, '\n\n\n\n<<<<<<<< to >>>>>>>> \n\n' + picture.replace(USERS_REGEXP, USERS_REPLACE_WITH)
);
}
picture = picture.replace(USERS_REGEXP, USERS_REPLACE_WITH);
uploadedpicture = uploadedpicture.replace(USERS_REGEXP, USERS_REPLACE_WITH);
if (SAVE) {
db.setObjectField('user:' + uid, 'picture', picture, next);
} else {
next();
}
});
}, next);
}, {}, function(err) {
if (err) {
return next(err);
}
console.log('\n\nreplacing users picture and uploadedpicture done');
next();
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment