p5.js-web-editor/server/migrations/moveBucket.js

66 lines
1.9 KiB
JavaScript
Raw Normal View History

/* eslint-disable */
import s3 from 's3';
import path from 'path';
import mongoose from 'mongoose';
import User from '../models/user';
import Project from '../models/project';
2017-12-27 17:04:52 +00:00
import async from 'async';
require('dotenv').config({path: path.resolve('.env')});
mongoose.connect('mongodb://localhost:27017/p5js-web-editor');
mongoose.connection.on('error', () => {
console.error('MongoDB Connection Error. Please make sure that MongoDB is running.');
process.exit(1);
});
2017-12-27 02:50:45 +00:00
// let client = s3.createClient({
// maxAsyncS3: 20,
// s3RetryCount: 3,
// s3RetryDelay: 1000,
// multipartUploadThreshold: 20971520, // this is the default (20 MB)
// multipartUploadSize: 15728640, // this is the default (15 MB)
// s3Options: {
// accessKeyId: `${process.env.AWS_ACCESS_KEY}`,
// secretAccessKey: `${process.env.AWS_SECRET_KEY}`,
// region: `${process.env.AWS_REGION}`
// },
// });
2017-12-27 17:29:36 +00:00
const CHUNK = 100;
Project.count({})
2017-12-27 17:22:12 +00:00
.exec().then((numProjects) => {
console.log(numProjects);
2017-12-27 17:18:23 +00:00
let index = 0;
async.whilst(
() => {
return index < numProjects;
},
(whilstCb) => {
2017-12-27 17:19:53 +00:00
Project.find({}).skip(index).limit(CHUNK).exec((err, projects) => {
async.eachSeries(projects, (project, cb) => {
2017-12-27 17:29:36 +00:00
console.log(project.name);
2017-12-27 17:19:53 +00:00
async.eachSeries(project.files, (file, fileCb) => {
if (file.url && file.url.includes('s3-us-west-2.amazonaws.com/')) {
file.url = file.url.replace('s3-us-west-2.amazonaws.com/', '');
2017-12-27 17:19:53 +00:00
project.save((err, newProject) => {
console.log(`updated file ${file.url}`);
fileCb();
});
} else {
2017-12-27 17:18:23 +00:00
fileCb();
2017-12-27 17:19:53 +00:00
}
}, () => {
cb();
});
2017-12-27 17:29:36 +00:00
}, () => {
index += CHUNK;
whilstCb();
2017-12-27 17:18:23 +00:00
});
2017-12-27 03:17:53 +00:00
});
2017-12-27 17:18:23 +00:00
},
() => {
console.log('finished processing all documents.')
process.exit(0);
}
);
});