2017-04-06 18:34:14 +00:00
|
|
|
/* eslint-disable */
|
|
|
|
import s3 from 's3';
|
|
|
|
import path from 'path';
|
|
|
|
import mongoose from 'mongoose';
|
|
|
|
import User from '../models/user';
|
|
|
|
import Project from '../models/project';
|
2018-01-08 18:51:35 +00:00
|
|
|
import async from 'async';
|
2017-04-06 18:34:14 +00:00
|
|
|
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);
|
|
|
|
});
|
|
|
|
|
|
|
|
let client = s3.createClient({
|
|
|
|
maxAsyncS3: 20,
|
|
|
|
s3RetryCount: 3,
|
|
|
|
s3RetryDelay: 1000,
|
2017-12-13 19:56:24 +00:00
|
|
|
multipartUploadThreshold: 20971520, // this is the default (20 MB)
|
|
|
|
multipartUploadSize: 15728640, // this is the default (15 MB)
|
2017-04-06 18:34:14 +00:00
|
|
|
s3Options: {
|
|
|
|
accessKeyId: `${process.env.AWS_ACCESS_KEY}`,
|
|
|
|
secretAccessKey: `${process.env.AWS_SECRET_KEY}`,
|
2017-04-13 18:39:03 +00:00
|
|
|
region: `${process.env.AWS_REGION}`
|
2017-04-06 18:34:14 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-01-08 18:51:35 +00:00
|
|
|
const CHUNK = 100;
|
|
|
|
Project.count({}).exec().then((numProjects) => {
|
|
|
|
console.log(numProjects);
|
|
|
|
let index = 0;
|
2018-01-08 19:26:51 +00:00
|
|
|
async.whilst(
|
|
|
|
() => {
|
|
|
|
return index < numProjects;
|
|
|
|
},
|
|
|
|
(whilstCb) => {
|
|
|
|
Project.find({}).skip(index).limit(CHUNK).exec((err, projects) => {
|
|
|
|
async.eachSeries(projects, (project, cb) => {
|
|
|
|
if (!project.user) {
|
|
|
|
cb();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const userId = project.user.valueOf();
|
|
|
|
console.log(project.name);
|
|
|
|
async.eachSeries(project.files, (file, fileCb) => {
|
|
|
|
if (file.url && file.url.includes(process.env.S3_BUCKET) && !file.url.includes(userId)) {
|
2018-01-08 19:39:33 +00:00
|
|
|
console.log(file.url);
|
2018-01-08 19:41:27 +00:00
|
|
|
console.log(userId);
|
2018-01-08 19:26:51 +00:00
|
|
|
const key = file.url.split('/').pop();
|
|
|
|
console.log(key);
|
|
|
|
const params = {
|
|
|
|
Bucket: `${process.env.S3_BUCKET}`,
|
|
|
|
CopySource: `${process.env.S3_BUCKET}/${key}`,
|
|
|
|
Key: `${userId}/${key}`
|
|
|
|
};
|
|
|
|
try {
|
|
|
|
client.moveObject(params)
|
2018-01-08 20:01:43 +00:00
|
|
|
.on('error', (err) => {
|
|
|
|
console.log(err);
|
|
|
|
file.url = (process.env.S3_BUCKET_URL_BASE ||
|
|
|
|
`https://s3-${process.env.AWS_REGION}.amazonaws.com/${process.env.S3_BUCKET}`) + `/${userId}/${key}`;
|
|
|
|
project.save((err, savedProject) => {
|
|
|
|
console.log(`updated file ${key}`);
|
|
|
|
fileCb();
|
|
|
|
});
|
2018-01-08 19:26:51 +00:00
|
|
|
})
|
|
|
|
.on('end', () => {
|
|
|
|
file.url = (process.env.S3_BUCKET_URL_BASE ||
|
|
|
|
`https://s3-${process.env.AWS_REGION}.amazonaws.com/${process.env.S3_BUCKET}`) + `/${userId}/${key}`;
|
|
|
|
project.save((err, savedProject) => {
|
|
|
|
console.log(`updated file ${key}`);
|
|
|
|
fileCb();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch(e) {
|
2018-01-08 20:01:43 +00:00
|
|
|
console.log(e);
|
2018-01-08 19:34:56 +00:00
|
|
|
fileCb();
|
2018-01-08 19:26:51 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fileCb();
|
|
|
|
}
|
|
|
|
}, () => {
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}, () => {
|
|
|
|
index += CHUNK;
|
|
|
|
whilstCb();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
console.log('finished processing all documents.');
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
);
|
2017-12-13 19:56:24 +00:00
|
|
|
});
|