Advertisement
Guest User

Untitled

a guest
Feb 12th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  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. var images = [ 'png', 'jpg', 'jpeg', 'bmp', 'gif', 'svg' ];
  13.  
  14. exports.fixPostings = function(reference, callback) {
  15.  
  16. var path = '/.media/' + reference.sha256;
  17.  
  18. if (reference.extension) {
  19.  
  20. if(images.indexOf(reference.extension < 0)) {
  21. return callback();
  22. }
  23.  
  24. path += '.' + reference.extension;
  25. }
  26.  
  27. var match = {
  28. 'files.path' : path
  29. };
  30.  
  31. var update = {
  32. $set : {
  33. 'files.$[file].thumb' : path
  34. }
  35. };
  36.  
  37. var ops = {
  38. arrayFilters : [ {
  39. 'file.path' : path
  40. } ]
  41. };
  42.  
  43. posts.updateOne(match, update, ops, function(error) {
  44.  
  45. if (error) {
  46. callback(error);
  47. } else {
  48. threads.updateOne(match, update, ops, callback);
  49. }
  50.  
  51. });
  52.  
  53. };
  54.  
  55. function startIteration(callback, lastId) {
  56.  
  57. references.find({
  58. sha256 : {
  59. $exists : true
  60. },
  61. hasThumb : {
  62. $ne : true
  63. },
  64. _id : lastId ? {
  65. $gt : lastId
  66. } : {
  67. $exists : true
  68. }
  69. }).sort({
  70. _id : 1
  71. }).limit(1).toArray(function(error, foundReferences) {
  72.  
  73. if (error || !foundReferences.length) {
  74. return callback(error);
  75. }
  76.  
  77. exports.fixPostings(foundReferences[0], function(error, sha256) {
  78.  
  79. if (error) {
  80. callback(error);
  81. } else {
  82. startIteration(callback, foundReferences[0]._id);
  83. }
  84.  
  85. });
  86. });
  87.  
  88. }
  89.  
  90. function connect(connectString, dbToUse, callback, attempts) {
  91.  
  92. attempts = attempts || 0;
  93.  
  94. mongo.MongoClient.connect(connectString, {
  95. useNewUrlParser : true,
  96. useUnifiedTopology : true
  97. }, function connectedDb(error, client) {
  98.  
  99. if (error) {
  100.  
  101. if (attempts > 9) {
  102. callback(error);
  103. } else {
  104.  
  105. console.log(error);
  106. console.log('Retrying in 10 seconds');
  107.  
  108. setTimeout(function() {
  109. connect(connectString, dbToUse, callback, ++attempts);
  110. }, 10000);
  111. }
  112.  
  113. } else {
  114.  
  115. cachedClient = client;
  116. cachedDb = client.db(dbToUse);
  117.  
  118. posts = cachedDb.collection('posts');
  119. threads = cachedDb.collection('threads');
  120. references = cachedDb.collection('uploadReferences');
  121.  
  122. startIteration(callback);
  123. }
  124.  
  125. });
  126.  
  127. }
  128.  
  129. exports.init = function(callback) {
  130.  
  131. var dbSettings = require('./settings/db.json');
  132.  
  133.  
  134.  
  135. var connectString = 'mongodb://';
  136.  
  137. if (dbSettings.user) {
  138. connectString += dbSettings.user + ':' + dbSettings.password + '@';
  139. }
  140.  
  141. connectString += dbSettings.address + ':';
  142. connectString += dbSettings.port + '/' + dbSettings.db;
  143.  
  144. if (dbSettings.ssl) {
  145. connectString += '?ssl=true';
  146. }
  147.  
  148. connect(connectString, dbSettings.db, callback);
  149.  
  150. };
  151.  
  152. exports.init(function(error) {
  153.  
  154. if (error) {
  155. console.log(error);
  156. }
  157.  
  158. cachedClient.close();
  159.  
  160. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement