Created
March 22, 2017 09:31
from lithium-import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var path = require('path'); | |
var nconf = require('nconf'); | |
var async = require('async'); | |
var url = require('url'); | |
var _convert = require('./node_modules/nodebb-plugin-import/node_modules/bbcode-to-markdown'); | |
nconf.file({ file: path.join(__dirname, './config.json') }); | |
var dbType = nconf.get('database'); | |
var productionDbConfig = nconf.get(dbType); | |
nconf.set(dbType, productionDbConfig); | |
var db = require('./src/database'); | |
function replaceLiImages (content) { | |
content = content || ''; | |
content = content.replace(/(http.*)?\/t\d*\/image\/serverpage\/image-id\/(\w+)\/image-size\/.*\?/ig, '/uploads/_imported_images/$2\?'); | |
content = content.replace(/(http.*)?\/legacyfs\/online\/avatars\/(.*)/ig, '/uploads/_imported_images/legacy_avatars/$2\?'); | |
content = content.replace(/<li-image[^>]+id='?"?([^'"\s>]+)'?"?/ig, '<li-image src="/uploads/_imported_images/$1"'); | |
content = content.replace(/li-image/ig, 'img'); | |
return content; | |
} | |
function convert (content) { | |
content = _convert.bbcodeToMarkdown(replaceLiImages(content)); | |
content = content.replace(/(\[.*?\])\((.+?)\)/g, function (match, m1, m2, offset, string) { return m1 + '(' + url.resolve(m2, '') + ')'; }); | |
return content; | |
} | |
// find the first (default) img src in a string | |
var _findImgsRE = /<img[^>]+src='?"?([^'"\s>]+)'?"?\s*.*\/?>/gi; | |
function findImgSrc (str, options) { | |
options = options || {first: true, last: false, index: 0}; | |
var results = _findImgsRE.exec(str || ''); | |
if(results) { | |
return results[options.first ? 1 : options.last ? results.length - 1 : options.index + 1]; | |
} | |
} | |
db.init(function() { | |
console.log('db.ready'); | |
var batch = require('./src/batch'); | |
async.series([ | |
function (next) { | |
batch.processSortedSet('topics:tid', function (ids, next) { | |
async.each(ids, function (id, next) { | |
async.waterfall([ | |
function (next) { | |
db.getObject('topic:' + id, next); | |
}, | |
function (topic, next) { | |
var thumb = findImgSrc(replaceLiImages(topic._imported_content)); | |
if (!thumb) { | |
return next(null, topic); | |
} | |
db.setObjectField('topic:' + topic.tid, 'thumb', thumb, function (err) { | |
next(err, topic); | |
}); | |
}, | |
function (topic, next) { | |
var content = convert(topic._imported_content); | |
db.setObjectField('post:' + topic.mainPid, 'content', content, function (err) { | |
next(err, topic); | |
}); | |
} | |
], next); | |
}, next); | |
}, {}, function (err) { | |
if (!err) console.log('TOPICS ALL DONE!'); | |
next(err); | |
}); | |
}, | |
function (next) { | |
batch.processSortedSet('posts:pid', function (ids, next) { | |
async.each(ids, function (id, next) { | |
async.waterfall([ | |
function (next) { | |
db.getObject('post:' + id, next); | |
}, | |
function (post, next) { | |
if (!post._imported_content) { | |
return next(); | |
} | |
var content = convert(post._imported_content); | |
db.setObjectField('post:' + id, 'content', content, function (err) { | |
next(err, post); | |
}); | |
} | |
], next); | |
}, next); | |
}, {}, function (err) { | |
if (!err) console.log('POSTS ALL DONE!'); | |
next(err); | |
}); | |
} | |
], function (err) { | |
if (!err) console.log('ALL DONE!'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment