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

66 lines
1.9 KiB
JavaScript
Raw Normal View History

/* eslint-disable */
import s3 from '@auth0/s3';
import path from 'path';
import mongoose from 'mongoose';
import User from '../models/user';
import Project from '../models/project';
2017-12-27 18:04:52 +01: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 03:50:45 +01: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 18:29:36 +01:00
const CHUNK = 100;
Project.count({})
2017-12-27 18:22:12 +01:00
.exec().then((numProjects) => {
console.log(numProjects);
2017-12-27 18:18:23 +01:00
let index = 0;
async.whilst(
() => {
return index < numProjects;
},
(whilstCb) => {
2017-12-27 18:19:53 +01:00
Project.find({}).skip(index).limit(CHUNK).exec((err, projects) => {
async.eachSeries(projects, (project, cb) => {
2017-12-27 18:29:36 +01:00
console.log(project.name);
2017-12-27 18:19:53 +01: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 18:19:53 +01:00
project.save((err, newProject) => {
console.log(`updated file ${file.url}`);
fileCb();
});
} else {
2017-12-27 18:18:23 +01:00
fileCb();
2017-12-27 18:19:53 +01:00
}
}, () => {
cb();
});
2017-12-27 18:29:36 +01:00
}, () => {
index += CHUNK;
whilstCb();
2017-12-27 18:18:23 +01:00
});
2017-12-27 04:17:53 +01:00
});
2017-12-27 18:18:23 +01:00
},
() => {
console.log('finished processing all documents.')
process.exit(0);
}
);
});