Skip to content

Commit

Permalink
Merge pull request #7428 from RocketChat/run-avatar-migration-on-startup
Browse files Browse the repository at this point in the history
Run avatar migration on startup
  • Loading branch information
rodrigok authored Jul 6, 2017
2 parents a3ce28a + 47dad0d commit a3b64c0
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 188 deletions.
1 change: 1 addition & 0 deletions packages/rocketchat-file/file.server.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ RocketChatFile.GridFS = class {
this.store = new Grid(db, mongo);
this.findOneSync = Meteor.wrapAsync(this.store.collection(this.name).findOne.bind(this.store.collection(this.name)));
this.removeSync = Meteor.wrapAsync(this.store.remove.bind(this.store));
this.countSync = Meteor.wrapAsync(this.store._col.count.bind(this.store._col));
this.getFileSync = Meteor.wrapAsync(this.getFile.bind(this));
}

Expand Down
2 changes: 1 addition & 1 deletion packages/rocketchat-migrations/migrations.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ Migrations._migrateTo = function(version, rerun) {

if (rerun) {
log.info('Rerunning version ' + version);
migrate('up', version);
migrate('up', this._findIndexByVersion(version));
log.info('Finished migrating.');
unlock();
return true;
Expand Down
188 changes: 1 addition & 187 deletions server/startup/migrations/v097.js
Original file line number Diff line number Diff line change
@@ -1,192 +1,6 @@
import fs from 'fs';
import path from 'path';

function log(...args) {
console.log('[AVATAR]', ...args);
}

function logError(...args) {
console.error('[AVATAR]', ...args);
}

function insertAvatar({ details, avatarsFileStore, stream, callback = () => {} }) {
return new Promise((resolve) => {
Meteor.defer(() => {
Meteor.runAsUser('rocket.cat', () => {
avatarsFileStore.insert(details, stream, (err) => {
if (err) {
logError({err});
resolve();
} else {
Meteor.setTimeout(() => {
callback();
}, 200);
}
resolve();
});
});
});
});
}

function batch(arr, limit, fn) {
if (!arr.length) {
return Promise.resolve();
}
return Promise.all(arr.splice(0, limit).map((item) => {
return fn(item);
})).then(() => { return batch(arr, limit, fn); });
}

RocketChat.Migrations.add({
version: 97,
up() {
log('Migrating avatars. This might take a while.');

const query = {
$or: [{
's3.path': {
$exists: true
}
}, {
'googleCloudStorage.path': {
$exists: true
}
}]
};

RocketChat.models.Uploads.find(query).forEach((record) => {
if (record.s3) {
RocketChat.models.Uploads.model.direct.update({_id: record._id}, {
$set: {
'store': 'AmazonS3:Uploads',
AmazonS3: {
path: record.s3.path + record._id
}
},
$unset: {
s3: 1
}
}, {multi: true});
} else {
RocketChat.models.Uploads.model.direct.update({_id: record._id}, {
$set: {
store: 'GoogleCloudStorage:Uploads',
GoogleStorage: {
path: record.googleCloudStorage.path + record._id
}
},
$unset: {
googleCloudStorage: 1
}
}, {multi: true});
}
});

RocketChat.models.Uploads.model.direct.update({
store: 'fileSystem'
}, {
$set: {
store: 'FileSystem:Uploads'
}
}, {
multi: true
});
RocketChat.models.Uploads.model.direct.update({
store: 'rocketchat_uploads'
}, {
$set: {
store: 'GridFS:Uploads'
}
}, {
multi: true
});

const avatarOrigins = [
'upload',
'gravatar',
'facebook',
'twitter',
'github',
'google',
'url',
'gitlab',
'linkedin'
];

const avatarsPath = RocketChat.models.Settings.findOne({_id: 'Accounts_AvatarStorePath'}).value;
const avatarStoreType = RocketChat.models.Settings.findOne({_id: 'Accounts_AvatarStoreType'}).value;

Meteor.setTimeout(function() {
const avatarsFileStore = FileUpload.getStore('Avatars');

const oldAvatarGridFS = new RocketChatFile.GridFS({
name: 'avatars'
});

const users = RocketChat.models.Users.find({avatarOrigin: {$in: avatarOrigins}}, {avatarOrigin: 1, username: 1}).fetch();

const usersTotal = users.length;

log('Total users to migrate avatars ->', usersTotal);

let current = 0;

batch(users, 300, (user) => {
const id = `${ user.username }.jpg`;

const gridFSAvatar = oldAvatarGridFS.getFileWithReadStream(id);

log('Migrating', ++current, 'of', usersTotal);

if (gridFSAvatar) {
const details = {
userId: user._id,
type: gridFSAvatar.contentType,
size: gridFSAvatar.length
};

return insertAvatar({
details,
avatarsFileStore,
stream: gridFSAvatar.readStream,
callback() {
oldAvatarGridFS.deleteFile(id);
}
});
}
if (avatarStoreType === 'FileSystem' && avatarsPath && avatarsPath.trim()) {
try {
const filePath = path.join(avatarsPath, id);
const stat = fs.statSync(filePath);
if (stat && stat.isFile()) {
const details = {
userId: user._id,
type: 'image/jpeg',
size: stat.size
};
return insertAvatar({
details,
avatarsFileStore,
stream: fs.createReadStream(filePath),
callback() {
fs.unlinkSync(filePath);
}
});
}
} catch (e) {
logError('Error migrating old avatar', e);
return Promise.resolve();
}
}
}).then(() => {
const avatarsFiles = new Mongo.Collection('avatars.files');
const avatarsChunks = new Mongo.Collection('avatars.chunks');
avatarsFiles.rawCollection().drop();
avatarsChunks.rawCollection().drop();
});
}, 1000);
RocketChat.models.Settings.remove({_id: 'Accounts_AvatarStoreType'});
RocketChat.models.Settings.remove({_id: 'Accounts_AvatarStorePath'});
// Migration moved to 099.js to fix a bug
}
});
Loading

0 comments on commit a3b64c0

Please sign in to comment.