UrlCache.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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.pipeUrlToFileWithRetry(
  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 project_id + ':' + crypto.createHash('md5').update(url).digest('hex')
  152. },
  153. _cacheFilePathForUrl(project_id, url) {
  154. return `${Settings.path.clsiCacheDir}/${UrlCache._cacheFileNameForUrl(
  155. project_id,
  156. url
  157. )}`
  158. },
  159. _copyFile(from, to, _callback) {
  160. if (_callback == null) {
  161. _callback = function (error) {}
  162. }
  163. const callbackOnce = function (error) {
  164. if (error != null) {
  165. logger.error({ err: error, from, to }, 'error copying file from cache')
  166. }
  167. _callback(error)
  168. return (_callback = function () {})
  169. }
  170. const writeStream = fs.createWriteStream(to)
  171. const readStream = fs.createReadStream(from)
  172. writeStream.on('error', callbackOnce)
  173. readStream.on('error', callbackOnce)
  174. writeStream.on('close', callbackOnce)
  175. return writeStream.on('open', () => readStream.pipe(writeStream))
  176. },
  177. _clearUrlFromCache(project_id, url, callback) {
  178. if (callback == null) {
  179. callback = function (error) {}
  180. }
  181. return UrlCache._clearUrlDetails(project_id, url, function (error) {
  182. if (error != null) {
  183. return callback(error)
  184. }
  185. return UrlCache._deleteUrlCacheFromDisk(project_id, url, function (
  186. error
  187. ) {
  188. if (error != null) {
  189. return callback(error)
  190. }
  191. return callback(null)
  192. })
  193. })
  194. },
  195. _deleteUrlCacheFromDisk(project_id, url, callback) {
  196. if (callback == null) {
  197. callback = function (error) {}
  198. }
  199. return fs.unlink(UrlCache._cacheFilePathForUrl(project_id, url), function (
  200. error
  201. ) {
  202. if (error != null && error.code !== 'ENOENT') {
  203. // no error if the file isn't present
  204. return callback(error)
  205. } else {
  206. return callback()
  207. }
  208. })
  209. },
  210. _findUrlDetails(project_id, url, callback) {
  211. if (callback == null) {
  212. callback = function (error, urlDetails) {}
  213. }
  214. const job = (cb) =>
  215. db.UrlCache.findOne({ where: { url, project_id } })
  216. .then((urlDetails) => cb(null, urlDetails))
  217. .error(cb)
  218. return dbQueue.queue.push(job, callback)
  219. },
  220. _updateOrCreateUrlDetails(project_id, url, lastModified, callback) {
  221. if (callback == null) {
  222. callback = function (error) {}
  223. }
  224. const job = (cb) =>
  225. db.UrlCache.findOrCreate({ where: { url, project_id } })
  226. .spread((urlDetails, created) =>
  227. urlDetails
  228. .update({ lastModified })
  229. .then(() => cb())
  230. .error(cb)
  231. )
  232. .error(cb)
  233. return dbQueue.queue.push(job, callback)
  234. },
  235. _clearUrlDetails(project_id, url, callback) {
  236. if (callback == null) {
  237. callback = function (error) {}
  238. }
  239. const job = (cb) =>
  240. db.UrlCache.destroy({ where: { url, project_id } })
  241. .then(() => cb(null))
  242. .error(cb)
  243. return dbQueue.queue.push(job, callback)
  244. },
  245. _findAllUrlsInProject(project_id, callback) {
  246. if (callback == null) {
  247. callback = function (error, urls) {}
  248. }
  249. const job = (cb) =>
  250. db.UrlCache.findAll({ where: { project_id } })
  251. .then((urlEntries) =>
  252. cb(
  253. null,
  254. urlEntries.map((entry) => entry.url)
  255. )
  256. )
  257. .error(cb)
  258. return dbQueue.queue.push(job, callback)
  259. }
  260. }