Skip to content

Commit 3e96627

Browse files
authored
Merge pull request #20 from which-ecosystem/s3-reuploads
Improve reuploads and add script
2 parents 5462174 + 4194a35 commit 3e96627

File tree

7 files changed

+74
-13
lines changed

7 files changed

+74
-13
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scripts

hooks/fetchImages.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { HookContext } from '@feathersjs/feathers';
22
import Bluebird from 'bluebird';
33
import _ from 'lodash';
4+
import Debug from 'debug';
45

6+
const debug = Debug('s3-reuploads');
57

68
export default (paths: string[]) => async (context: HookContext): Promise<HookContext> => {
79
const {
@@ -19,10 +21,13 @@ export default (paths: string[]) => async (context: HookContext): Promise<HookCo
1921

2022
// If image is not from our s3, fetch it!
2123
if (!fileService.isS3url(url)) {
24+
debug('Found non-s3 url!');
2225
const filePath = await fileService.downloadFile(url);
2326
const s3Path = fileService.generateS3Path(user?.username);
2427
const s3Url = await fileService.uploadFileToS3(filePath, s3Path);
25-
return model.findOneAndUpdate({ _id: result._id }, { [path]: s3Url });
28+
await model.findOneAndUpdate({ _id: result._id }, { $set: { [path]: s3Url } });
29+
debug(`Fetched and updated: from ${url} to ${s3Url}`);
30+
return s3Url;
2631
}
2732
return url;
2833
});

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"@types/axios": "^0.14.0",
2424
"@types/bluebird": "^3.5.32",
2525
"@types/cors": "^2.8.6",
26+
"@types/debug": "^4.1.5",
2627
"@types/lodash": "^4.14.155",
2728
"@types/mongoose": "^5.7.23",
2829
"@types/node": "^14.0.27",
@@ -33,6 +34,7 @@
3334
"axios": "^0.19.2",
3435
"bluebird": "^3.7.2",
3536
"cors": "^2.8.5",
37+
"debug": "^4.1.1",
3638
"feathers-hooks-common": "^5.0.3",
3739
"feathers-mongoose": "^8.3.0",
3840
"lodash": "^4.17.15",

scripts/migrateImages.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import mongoose from 'mongoose';
2+
import bluebird from 'bluebird';
3+
import _ from 'lodash';
4+
import {
5+
User,
6+
Poll,
7+
Vote,
8+
Feedback
9+
} from 'which-types';
10+
11+
import app from '../app';
12+
app.service('files').setup(app);
13+
14+
const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which';
15+
16+
mongoose.connect(MONGODB_URL, {
17+
useNewUrlParser: true,
18+
useUnifiedTopology: true,
19+
useCreateIndex: true,
20+
useFindAndModify: false,
21+
family: 4 // Use IPv4, skip trying IPv6
22+
});
23+
24+
const patchPoll = (poll: Poll): Promise<Poll> => {
25+
console.log(`Patching poll of user ${poll.author.username}`)
26+
return app.service('polls').patch(poll._id.toString(), {}, { user: poll.author, authenticated: true });
27+
};
28+
29+
const patchUser = (user: User): Promise<User> => {
30+
console.log(`Patching user ${user.username}`)
31+
return app.service('users').patch(user._id.toString(), {}, { user, authenticated: true });
32+
};
33+
34+
const update = async () => {
35+
const users = app.service('users').find();
36+
37+
await bluebird.mapSeries(users, async (user: User) => {
38+
await patchUser(user);
39+
const polls = await app.service('polls').find({ query: { authorId: user._id }});
40+
await bluebird.mapSeries(polls, (poll: Poll) => patchPoll(poll));
41+
return;
42+
});
43+
};
44+
45+
update();
46+

populateDb.ts renamed to scripts/populateDb.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import {
88
Feedback
99
} from 'which-types';
1010

11-
import app from './app';
11+
import app from '../app';
12+
app.service('files').setup(app);
1213

1314
const MONGODB_URL = process.env.MONGODB_URI || 'mongodb://localhost:27017/which';
1415

@@ -20,7 +21,7 @@ mongoose.connect(MONGODB_URL, {
2021
family: 4 // Use IPv4, skip trying IPv6
2122
});
2223

23-
const POLLS_AMOUNT = 20;
24+
const POLLS_AMOUNT = 5;
2425

2526
const imageUrls: string[] = [
2627
// eslint-disable max-len
@@ -31,14 +32,14 @@ const imageUrls: string[] = [
3132
];
3233

3334
const names: string[] = [
34-
'Emma',
35-
'Elise',
36-
'Jack',
37-
'Oliver',
38-
'Jamie',
39-
'Adam',
40-
'Jordan',
41-
'William'
35+
'emma',
36+
'elise',
37+
'jack',
38+
'oliver',
39+
'jamie',
40+
'adam',
41+
'jordan',
42+
'william'
4243
];
4344

4445
const choices = [
@@ -101,5 +102,5 @@ const populate = async () => {
101102
});
102103
};
103104

104-
populate().finally(mongoose.disconnect);
105+
populate();
105106

services/polls/polls.hooks.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export default {
5555
},
5656
after: {
5757
all: convertPoll,
58-
create: fetchImages(['contents.left.url', 'contents.right.url'])
58+
create: fetchImages(['contents.left.url', 'contents.right.url']),
59+
patch: fetchImages(['contents.left.url', 'contents.right.url'])
5960
}
6061
};
6162

0 commit comments

Comments
 (0)