aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Sokolov <eug-vs@keemail.me>2020-08-12 21:17:39 +0300
committerGitHub <noreply@github.com>2020-08-12 21:17:39 +0300
commit3e966272b855b4c167582abf21439429d11c2c8b (patch)
tree0425766e8fa6bad616f687e660e2469e914c6a98
parent5462174ca076aef53d06b92372141c665c923ea3 (diff)
parent4194a35550bb962563f447b91811759f0d291f75 (diff)
downloadwhich-api-3e966272b855b4c167582abf21439429d11c2c8b.tar.gz
Merge pull request #20 from which-ecosystem/s3-reuploads
Improve reuploads and add script
-rw-r--r--.eslintignore1
-rw-r--r--hooks/fetchImages.ts7
-rw-r--r--package-lock.json5
-rw-r--r--package.json2
-rw-r--r--scripts/migrateImages.ts46
-rw-r--r--scripts/populateDb.ts (renamed from populateDb.ts)23
-rw-r--r--services/polls/polls.hooks.ts3
7 files changed, 74 insertions, 13 deletions
diff --git a/.eslintignore b/.eslintignore
new file mode 100644
index 0000000..b744996
--- /dev/null
+++ b/.eslintignore
@@ -0,0 +1 @@
+scripts
diff --git a/hooks/fetchImages.ts b/hooks/fetchImages.ts
index 44aac6c..a7eb30c 100644
--- a/hooks/fetchImages.ts
+++ b/hooks/fetchImages.ts
@@ -1,7 +1,9 @@
import { HookContext } from '@feathersjs/feathers';
import Bluebird from 'bluebird';
import _ from 'lodash';
+import Debug from 'debug';
+const debug = Debug('s3-reuploads');
export default (paths: string[]) => async (context: HookContext): Promise<HookContext> => {
const {
@@ -19,10 +21,13 @@ export default (paths: string[]) => async (context: HookContext): Promise<HookCo
// If image is not from our s3, fetch it!
if (!fileService.isS3url(url)) {
+ debug('Found non-s3 url!');
const filePath = await fileService.downloadFile(url);
const s3Path = fileService.generateS3Path(user?.username);
const s3Url = await fileService.uploadFileToS3(filePath, s3Path);
- return model.findOneAndUpdate({ _id: result._id }, { [path]: s3Url });
+ await model.findOneAndUpdate({ _id: result._id }, { $set: { [path]: s3Url } });
+ debug(`Fetched and updated: from ${url} to ${s3Url}`);
+ return s3Url;
}
return url;
});
diff --git a/package-lock.json b/package-lock.json
index 12eb4b0..c28c048 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -226,6 +226,11 @@
"@types/express": "*"
}
},
+ "@types/debug": {
+ "version": "4.1.5",
+ "resolved": "https://registry.npmjs.org/@types/debug/-/debug-4.1.5.tgz",
+ "integrity": "sha512-Q1y515GcOdTHgagaVFhHnIFQ38ygs/kmxdNpvpou+raI9UO3YZcHDngBSYKQklcKlvA7iuQlmIKbzvmxcOE9CQ=="
+ },
"@types/engine.io": {
"version": "3.1.4",
"resolved": "https://registry.npmjs.org/@types/engine.io/-/engine.io-3.1.4.tgz",
diff --git a/package.json b/package.json
index 272965e..2a7ecf3 100644
--- a/package.json
+++ b/package.json
@@ -23,6 +23,7 @@
"@types/axios": "^0.14.0",
"@types/bluebird": "^3.5.32",
"@types/cors": "^2.8.6",
+ "@types/debug": "^4.1.5",
"@types/lodash": "^4.14.155",
"@types/mongoose": "^5.7.23",
"@types/node": "^14.0.27",
@@ -33,6 +34,7 @@
"axios": "^0.19.2",
"bluebird": "^3.7.2",
"cors": "^2.8.5",
+ "debug": "^4.1.1",
"feathers-hooks-common": "^5.0.3",
"feathers-mongoose": "^8.3.0",
"lodash": "^4.17.15",
diff --git a/scripts/migrateImages.ts b/scripts/migrateImages.ts
new file mode 100644
index 0000000..771880e
--- /dev/null
+++ b/scripts/migrateImages.ts
@@ -0,0 +1,46 @@
+import mongoose from 'mongoose';
+import bluebird from 'bluebird';
+import _ from 'lodash';
+import {
+ User,
+ Poll,
+ Vote,
+ Feedback
+} from 'which-types';
+
+import app from '../app';
+app.service('files').setup(app);
+
+const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which';
+
+mongoose.connect(MONGODB_URL, {
+ useNewUrlParser: true,
+ useUnifiedTopology: true,
+ useCreateIndex: true,
+ useFindAndModify: false,
+ family: 4 // Use IPv4, skip trying IPv6
+});
+
+const patchPoll = (poll: Poll): Promise<Poll> => {
+ console.log(`Patching poll of user ${poll.author.username}`)
+ return app.service('polls').patch(poll._id.toString(), {}, { user: poll.author, authenticated: true });
+};
+
+const patchUser = (user: User): Promise<User> => {
+ console.log(`Patching user ${user.username}`)
+ return app.service('users').patch(user._id.toString(), {}, { user, authenticated: true });
+};
+
+const update = async () => {
+ const users = app.service('users').find();
+
+ await bluebird.mapSeries(users, async (user: User) => {
+ await patchUser(user);
+ const polls = await app.service('polls').find({ query: { authorId: user._id }});
+ await bluebird.mapSeries(polls, (poll: Poll) => patchPoll(poll));
+ return;
+ });
+};
+
+update();
+
diff --git a/populateDb.ts b/scripts/populateDb.ts
index 1565f44..b7d83c0 100644
--- a/populateDb.ts
+++ b/scripts/populateDb.ts
@@ -8,7 +8,8 @@ import {
Feedback
} from 'which-types';
-import app from './app';
+import app from '../app';
+app.service('files').setup(app);
const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which';
@@ -20,7 +21,7 @@ mongoose.connect(MONGODB_URL, {
family: 4 // Use IPv4, skip trying IPv6
});
-const POLLS_AMOUNT = 20;
+const POLLS_AMOUNT = 5;
const imageUrls: string[] = [
// eslint-disable max-len
@@ -31,14 +32,14 @@ const imageUrls: string[] = [
];
const names: string[] = [
- 'Emma',
- 'Elise',
- 'Jack',
- 'Oliver',
- 'Jamie',
- 'Adam',
- 'Jordan',
- 'William'
+ 'emma',
+ 'elise',
+ 'jack',
+ 'oliver',
+ 'jamie',
+ 'adam',
+ 'jordan',
+ 'william'
];
const choices = [
@@ -101,5 +102,5 @@ const populate = async () => {
});
};
-populate().finally(mongoose.disconnect);
+populate();
diff --git a/services/polls/polls.hooks.ts b/services/polls/polls.hooks.ts
index 7a5b1da..7853c54 100644
--- a/services/polls/polls.hooks.ts
+++ b/services/polls/polls.hooks.ts
@@ -55,7 +55,8 @@ export default {
},
after: {
all: convertPoll,
- create: fetchImages(['contents.left.url', 'contents.right.url'])
+ create: fetchImages(['contents.left.url', 'contents.right.url']),
+ patch: fetchImages(['contents.left.url', 'contents.right.url'])
}
};