SHOW:
|
|
- or go back to the newest paste.
1 | #!/usr/bin/env node | |
2 | 'use strict'; | |
3 | var mongo = require('mongodb'); | |
4 | ||
5 | var cachedDb; | |
6 | var cachedClient; | |
7 | ||
8 | var posts; | |
9 | var threads; | |
10 | var references; | |
11 | ||
12 | exports.fixPostings = function(reference, callback) { | |
13 | ||
14 | var path = '/.media/' + reference.sha256; | |
15 | ||
16 | if (reference.extension) { | |
17 | path += '.' + reference.extension; | |
18 | } | |
19 | ||
20 | var match = { | |
21 | 'files.path' : path | |
22 | }; | |
23 | ||
24 | var update = { | |
25 | $set : { | |
26 | 'files.$[file].thumb' : path | |
27 | } | |
28 | }; | |
29 | ||
30 | var ops = { | |
31 | arrayFilters : [ { | |
32 | 'file.path' : path | |
33 | } ] | |
34 | }; | |
35 | ||
36 | posts.updateOne(match, update, ops, function(error) { | |
37 | ||
38 | if (error) { | |
39 | callback(error); | |
40 | } else { | |
41 | threads.updateOne(match, update, ops, callback); | |
42 | } | |
43 | ||
44 | }); | |
45 | ||
46 | }; | |
47 | ||
48 | function startIteration(callback, lastId) { | |
49 | ||
50 | references.find({ | |
51 | sha256 : { | |
52 | $exists : true | |
53 | }, | |
54 | hasThumb : { | |
55 | $ne : true | |
56 | }, | |
57 | _id : lastId ? { | |
58 | $gt : lastId | |
59 | } : { | |
60 | $exists : true | |
61 | } | |
62 | }).sort({ | |
63 | _id : 1 | |
64 | }).limit(1).toArray(function(error, foundReferences) { | |
65 | ||
66 | if (error || !foundReferences.length) { | |
67 | return callback(error); | |
68 | } | |
69 | ||
70 | exports.fixPostings(foundReferences[0], function(error, sha256) { | |
71 | ||
72 | if (error) { | |
73 | callback(error); | |
74 | } else { | |
75 | - | startIteration(callback, foundReferences[0]); |
75 | + | startIteration(callback, foundReferences[0]._id); |
76 | } | |
77 | ||
78 | }); | |
79 | }); | |
80 | ||
81 | } | |
82 | ||
83 | function connect(connectString, dbToUse, callback, attempts) { | |
84 | ||
85 | attempts = attempts || 0; | |
86 | ||
87 | mongo.MongoClient.connect(connectString, { | |
88 | useNewUrlParser : true, | |
89 | useUnifiedTopology : true | |
90 | }, function connectedDb(error, client) { | |
91 | ||
92 | if (error) { | |
93 | ||
94 | if (attempts > 9) { | |
95 | callback(error); | |
96 | } else { | |
97 | ||
98 | console.log(error); | |
99 | console.log('Retrying in 10 seconds'); | |
100 | ||
101 | setTimeout(function() { | |
102 | connect(connectString, dbToUse, callback, ++attempts); | |
103 | }, 10000); | |
104 | } | |
105 | ||
106 | } else { | |
107 | ||
108 | cachedClient = client; | |
109 | cachedDb = client.db(dbToUse); | |
110 | ||
111 | posts = cachedDb.collection('posts'); | |
112 | threads = cachedDb.collection('threads'); | |
113 | references = cachedDb.collection('uploadReferences'); | |
114 | ||
115 | startIteration(callback); | |
116 | } | |
117 | ||
118 | }); | |
119 | ||
120 | } | |
121 | ||
122 | exports.init = function(callback) { | |
123 | ||
124 | var dbSettings = require('./settings/db.json'); | |
125 | ||
126 | ||
127 | ||
128 | var connectString = 'mongodb://'; | |
129 | ||
130 | if (dbSettings.user) { | |
131 | connectString += dbSettings.user + ':' + dbSettings.password + '@'; | |
132 | } | |
133 | ||
134 | connectString += dbSettings.address + ':'; | |
135 | connectString += dbSettings.port + '/' + dbSettings.db; | |
136 | ||
137 | if (dbSettings.ssl) { | |
138 | connectString += '?ssl=true'; | |
139 | } | |
140 | ||
141 | connect(connectString, dbSettings.db, callback); | |
142 | ||
143 | }; | |
144 | ||
145 | exports.init(function(error) { | |
146 | ||
147 | if (error) { | |
148 | console.log(error); | |
149 | } | |
150 | ||
151 | cachedClient.close(); | |
152 | ||
153 | }); |