UrlCache.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. let UrlCache
  16. const db = require('./db')
  17. const dbQueue = require('./DbQueue')
  18. const UrlFetcher = require('./UrlFetcher')
  19. const Settings = require('settings-sharelatex')
  20. const crypto = require('crypto')
  21. const fs = require('fs')
  22. const logger = require('logger-sharelatex')
  23. const async = require('async')
  24. module.exports = UrlCache = {
  25. downloadUrlToFile(project_id, url, destPath, lastModified, callback) {
  26. if (callback == null) {
  27. callback = function(error) {}
  28. }
  29. return UrlCache._ensureUrlIsInCache(
  30. project_id,
  31. url,
  32. lastModified,
  33. (error, pathToCachedUrl) => {
  34. if (error != null) {
  35. return callback(error)
  36. }
  37. return UrlCache._copyFile(pathToCachedUrl, destPath, function(error) {
  38. if (error != null) {
  39. return UrlCache._clearUrlDetails(project_id, url, () =>
  40. callback(error)
  41. )
  42. } else {
  43. return callback(error)
  44. }
  45. })
  46. }
  47. )
  48. },
  49. clearProject(project_id, callback) {
  50. if (callback == null) {
  51. callback = function(error) {}
  52. }
  53. return UrlCache._findAllUrlsInProject(project_id, function(error, urls) {
  54. logger.log(
  55. { project_id, url_count: urls.length },
  56. 'clearing project URLs'
  57. )
  58. if (error != null) {
  59. return callback(error)
  60. }
  61. const jobs = Array.from(urls || []).map(url =>
  62. (url => callback =>
  63. UrlCache._clearUrlFromCache(project_id, url, function(error) {
  64. if (error != null) {
  65. logger.error(
  66. { err: error, project_id, url },
  67. 'error clearing project URL'
  68. )
  69. }
  70. return callback()
  71. }))(url)
  72. )
  73. return async.series(jobs, callback)
  74. })
  75. },
  76. _ensureUrlIsInCache(project_id, url, lastModified, callback) {
  77. if (callback == null) {
  78. callback = function(error, pathOnDisk) {}
  79. }
  80. if (lastModified != null) {
  81. // MYSQL only stores dates to an accuracy of a second but the incoming lastModified might have milliseconds.
  82. // So round down to seconds
  83. lastModified = new Date(Math.floor(lastModified.getTime() / 1000) * 1000)
  84. }
  85. return UrlCache._doesUrlNeedDownloading(
  86. project_id,
  87. url,
  88. lastModified,
  89. (error, needsDownloading) => {
  90. if (error != null) {
  91. return callback(error)
  92. }
  93. if (needsDownloading) {
  94. logger.log({ url, lastModified }, 'downloading URL')
  95. return UrlFetcher.pipeUrlToFile(
  96. url,
  97. UrlCache._cacheFilePathForUrl(project_id, url),
  98. error => {
  99. if (error != null) {
  100. return callback(error)
  101. }
  102. return UrlCache._updateOrCreateUrlDetails(
  103. project_id,
  104. url,
  105. lastModified,
  106. error => {
  107. if (error != null) {
  108. return callback(error)
  109. }
  110. return callback(
  111. null,
  112. UrlCache._cacheFilePathForUrl(project_id, url)
  113. )
  114. }
  115. )
  116. }
  117. )
  118. } else {
  119. logger.log({ url, lastModified }, 'URL is up to date in cache')
  120. return callback(null, UrlCache._cacheFilePathForUrl(project_id, url))
  121. }
  122. }
  123. )
  124. },
  125. _doesUrlNeedDownloading(project_id, url, lastModified, callback) {
  126. if (callback == null) {
  127. callback = function(error, needsDownloading) {}
  128. }
  129. if (lastModified == null) {
  130. return callback(null, true)
  131. }
  132. return UrlCache._findUrlDetails(project_id, url, function(
  133. error,
  134. urlDetails
  135. ) {
  136. if (error != null) {
  137. return callback(error)
  138. }
  139. if (
  140. urlDetails == null ||
  141. urlDetails.lastModified == null ||
  142. urlDetails.lastModified.getTime() < lastModified.getTime()
  143. ) {
  144. return callback(null, true)
  145. } else {
  146. return callback(null, false)
  147. }
  148. })
  149. },
  150. _cacheFileNameForUrl(project_id, url) {
  151. return (
  152. project_id +
  153. ':' +
  154. crypto
  155. .createHash('md5')
  156. .update(url)
  157. .digest('hex')
  158. )
  159. },
  160. _cacheFilePathForUrl(project_id, url) {
  161. return `${Settings.path.clsiCacheDir}/${UrlCache._cacheFileNameForUrl(
  162. project_id,
  163. url
  164. )}`
  165. },
  166. _copyFile(from, to, _callback) {
  167. if (_callback == null) {
  168. _callback = function(error) {}
  169. }
  170. const callbackOnce = function(error) {
  171. if (error != null) {
  172. logger.error({ err: error, from, to }, 'error copying file from cache')
  173. }
  174. _callback(error)
  175. return (_callback = function() {})
  176. }
  177. const writeStream = fs.createWriteStream(to)
  178. const readStream = fs.createReadStream(from)
  179. writeStream.on('error', callbackOnce)
  180. readStream.on('error', callbackOnce)
  181. writeStream.on('close', callbackOnce)
  182. return writeStream.on('open', () => readStream.pipe(writeStream))
  183. },
  184. _clearUrlFromCache(project_id, url, callback) {
  185. if (callback == null) {
  186. callback = function(error) {}
  187. }
  188. return UrlCache._clearUrlDetails(project_id, url, function(error) {
  189. if (error != null) {
  190. return callback(error)
  191. }
  192. return UrlCache._deleteUrlCacheFromDisk(project_id, url, function(error) {
  193. if (error != null) {
  194. return callback(error)
  195. }
  196. return callback(null)
  197. })
  198. })
  199. },
  200. _deleteUrlCacheFromDisk(project_id, url, callback) {
  201. if (callback == null) {
  202. callback = function(error) {}
  203. }
  204. return fs.unlink(UrlCache._cacheFilePathForUrl(project_id, url), function(
  205. error
  206. ) {
  207. if (error != null && error.code !== 'ENOENT') {
  208. // no error if the file isn't present
  209. return callback(error)
  210. } else {
  211. return callback()
  212. }
  213. })
  214. },
  215. _findUrlDetails(project_id, url, callback) {
  216. if (callback == null) {
  217. callback = function(error, urlDetails) {}
  218. }
  219. const job = cb =>
  220. db.UrlCache.find({ where: { url, project_id } })
  221. .then(urlDetails => cb(null, urlDetails))
  222. .error(cb)
  223. return dbQueue.queue.push(job, callback)
  224. },
  225. _updateOrCreateUrlDetails(project_id, url, lastModified, callback) {
  226. if (callback == null) {
  227. callback = function(error) {}
  228. }
  229. const job = cb =>
  230. db.UrlCache.findOrCreate({ where: { url, project_id } })
  231. .spread((urlDetails, created) =>
  232. urlDetails
  233. .updateAttributes({ lastModified })
  234. .then(() => cb())
  235. .error(cb)
  236. )
  237. .error(cb)
  238. return dbQueue.queue.push(job, callback)
  239. },
  240. _clearUrlDetails(project_id, url, callback) {
  241. if (callback == null) {
  242. callback = function(error) {}
  243. }
  244. const job = cb =>
  245. db.UrlCache.destroy({ where: { url, project_id } })
  246. .then(() => cb(null))
  247. .error(cb)
  248. return dbQueue.queue.push(job, callback)
  249. },
  250. _findAllUrlsInProject(project_id, callback) {
  251. if (callback == null) {
  252. callback = function(error, urls) {}
  253. }
  254. const job = cb =>
  255. db.UrlCache.findAll({ where: { project_id } })
  256. .then(urlEntries =>
  257. cb(
  258. null,
  259. urlEntries.map(entry => entry.url)
  260. )
  261. )
  262. .error(cb)
  263. return dbQueue.queue.push(job, callback)
  264. }
  265. }