ResourceWriter.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-unused-vars,
  6. no-useless-escape,
  7. */
  8. // TODO: This file was created by bulk-decaffeinate.
  9. // Fix any style issues and re-enable lint.
  10. /*
  11. * decaffeinate suggestions:
  12. * DS101: Remove unnecessary use of Array.from
  13. * DS102: Remove unnecessary code created because of implicit returns
  14. * DS207: Consider shorter variations of null checks
  15. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  16. */
  17. let ResourceWriter
  18. const UrlCache = require('./UrlCache')
  19. const Path = require('path')
  20. const fs = require('fs')
  21. const async = require('async')
  22. const OutputFileFinder = require('./OutputFileFinder')
  23. const ResourceStateManager = require('./ResourceStateManager')
  24. const Metrics = require('./Metrics')
  25. const logger = require('logger-sharelatex')
  26. const settings = require('@overleaf/settings')
  27. const parallelFileDownloads = settings.parallelFileDownloads || 1
  28. module.exports = ResourceWriter = {
  29. syncResourcesToDisk(request, basePath, callback) {
  30. if (callback == null) {
  31. callback = function (error, resourceList) {}
  32. }
  33. if (request.syncType === 'incremental') {
  34. logger.log(
  35. { project_id: request.project_id, user_id: request.user_id },
  36. 'incremental sync'
  37. )
  38. return ResourceStateManager.checkProjectStateMatches(
  39. request.syncState,
  40. basePath,
  41. function (error, resourceList) {
  42. if (error != null) {
  43. return callback(error)
  44. }
  45. return ResourceWriter._removeExtraneousFiles(
  46. resourceList,
  47. basePath,
  48. function (error, outputFiles, allFiles) {
  49. if (error != null) {
  50. return callback(error)
  51. }
  52. return ResourceStateManager.checkResourceFiles(
  53. resourceList,
  54. allFiles,
  55. basePath,
  56. function (error) {
  57. if (error != null) {
  58. return callback(error)
  59. }
  60. return ResourceWriter.saveIncrementalResourcesToDisk(
  61. request.project_id,
  62. request.resources,
  63. basePath,
  64. function (error) {
  65. if (error != null) {
  66. return callback(error)
  67. }
  68. return callback(null, resourceList)
  69. }
  70. )
  71. }
  72. )
  73. }
  74. )
  75. }
  76. )
  77. }
  78. logger.log(
  79. { project_id: request.project_id, user_id: request.user_id },
  80. 'full sync'
  81. )
  82. UrlCache.createProjectDir(request.project_id, error => {
  83. if (error != null) {
  84. return callback(error)
  85. }
  86. this.saveAllResourcesToDisk(
  87. request.project_id,
  88. request.resources,
  89. basePath,
  90. function (error) {
  91. if (error != null) {
  92. return callback(error)
  93. }
  94. return ResourceStateManager.saveProjectState(
  95. request.syncState,
  96. request.resources,
  97. basePath,
  98. function (error) {
  99. if (error != null) {
  100. return callback(error)
  101. }
  102. return callback(null, request.resources)
  103. }
  104. )
  105. }
  106. )
  107. })
  108. },
  109. saveIncrementalResourcesToDisk(project_id, resources, basePath, callback) {
  110. if (callback == null) {
  111. callback = function (error) {}
  112. }
  113. return this._createDirectory(basePath, error => {
  114. if (error != null) {
  115. return callback(error)
  116. }
  117. const jobs = Array.from(resources).map(resource =>
  118. (resource => {
  119. return callback =>
  120. this._writeResourceToDisk(project_id, resource, basePath, callback)
  121. })(resource)
  122. )
  123. return async.parallelLimit(jobs, parallelFileDownloads, callback)
  124. })
  125. },
  126. saveAllResourcesToDisk(project_id, resources, basePath, callback) {
  127. if (callback == null) {
  128. callback = function (error) {}
  129. }
  130. return this._createDirectory(basePath, error => {
  131. if (error != null) {
  132. return callback(error)
  133. }
  134. return this._removeExtraneousFiles(resources, basePath, error => {
  135. if (error != null) {
  136. return callback(error)
  137. }
  138. const jobs = Array.from(resources).map(resource =>
  139. (resource => {
  140. return callback =>
  141. this._writeResourceToDisk(
  142. project_id,
  143. resource,
  144. basePath,
  145. callback
  146. )
  147. })(resource)
  148. )
  149. return async.parallelLimit(jobs, parallelFileDownloads, callback)
  150. })
  151. })
  152. },
  153. _createDirectory(basePath, callback) {
  154. if (callback == null) {
  155. callback = function (error) {}
  156. }
  157. return fs.mkdir(basePath, function (err) {
  158. if (err != null) {
  159. if (err.code === 'EEXIST') {
  160. return callback()
  161. } else {
  162. logger.log({ err, dir: basePath }, 'error creating directory')
  163. return callback(err)
  164. }
  165. } else {
  166. return callback()
  167. }
  168. })
  169. },
  170. _removeExtraneousFiles(resources, basePath, _callback) {
  171. if (_callback == null) {
  172. _callback = function (error, outputFiles, allFiles) {}
  173. }
  174. const timer = new Metrics.Timer('unlink-output-files')
  175. const callback = function (error, ...result) {
  176. timer.done()
  177. return _callback(error, ...Array.from(result))
  178. }
  179. return OutputFileFinder.findOutputFiles(
  180. resources,
  181. basePath,
  182. function (error, outputFiles, allFiles) {
  183. if (error != null) {
  184. return callback(error)
  185. }
  186. const jobs = []
  187. for (const file of Array.from(outputFiles || [])) {
  188. ;(function (file) {
  189. const { path } = file
  190. let should_delete = true
  191. if (
  192. path.match(/^output\./) ||
  193. path.match(/\.aux$/) ||
  194. path.match(/^cache\//)
  195. ) {
  196. // knitr cache
  197. should_delete = false
  198. }
  199. if (path.match(/^output-.*/)) {
  200. // Tikz cached figures (default case)
  201. should_delete = false
  202. }
  203. if (path.match(/\.(pdf|dpth|md5)$/)) {
  204. // Tikz cached figures (by extension)
  205. should_delete = false
  206. }
  207. if (
  208. path.match(/\.(pygtex|pygstyle)$/) ||
  209. path.match(/(^|\/)_minted-[^\/]+\//)
  210. ) {
  211. // minted files/directory
  212. should_delete = false
  213. }
  214. if (
  215. path.match(/\.md\.tex$/) ||
  216. path.match(/(^|\/)_markdown_[^\/]+\//)
  217. ) {
  218. // markdown files/directory
  219. should_delete = false
  220. }
  221. if (path.match(/-eps-converted-to\.pdf$/)) {
  222. // Epstopdf generated files
  223. should_delete = false
  224. }
  225. if (
  226. path === 'output.pdf' ||
  227. path === 'output.dvi' ||
  228. path === 'output.log' ||
  229. path === 'output.xdv' ||
  230. path === 'output.stdout' ||
  231. path === 'output.stderr'
  232. ) {
  233. should_delete = true
  234. }
  235. if (path === 'output.tex') {
  236. // created by TikzManager if present in output files
  237. should_delete = true
  238. }
  239. if (should_delete) {
  240. return jobs.push(callback =>
  241. ResourceWriter._deleteFileIfNotDirectory(
  242. Path.join(basePath, path),
  243. callback
  244. )
  245. )
  246. }
  247. })(file)
  248. }
  249. return async.series(jobs, function (error) {
  250. if (error != null) {
  251. return callback(error)
  252. }
  253. return callback(null, outputFiles, allFiles)
  254. })
  255. }
  256. )
  257. },
  258. _deleteFileIfNotDirectory(path, callback) {
  259. if (callback == null) {
  260. callback = function (error) {}
  261. }
  262. return fs.stat(path, function (error, stat) {
  263. if (error != null && error.code === 'ENOENT') {
  264. return callback()
  265. } else if (error != null) {
  266. logger.err(
  267. { err: error, path },
  268. 'error stating file in deleteFileIfNotDirectory'
  269. )
  270. return callback(error)
  271. } else if (stat.isFile()) {
  272. return fs.unlink(path, function (error) {
  273. if (error != null) {
  274. logger.err(
  275. { err: error, path },
  276. 'error removing file in deleteFileIfNotDirectory'
  277. )
  278. return callback(error)
  279. } else {
  280. return callback()
  281. }
  282. })
  283. } else {
  284. return callback()
  285. }
  286. })
  287. },
  288. _writeResourceToDisk(project_id, resource, basePath, callback) {
  289. if (callback == null) {
  290. callback = function (error) {}
  291. }
  292. return ResourceWriter.checkPath(
  293. basePath,
  294. resource.path,
  295. function (error, path) {
  296. if (error != null) {
  297. return callback(error)
  298. }
  299. return fs.mkdir(
  300. Path.dirname(path),
  301. { recursive: true },
  302. function (error) {
  303. if (error != null) {
  304. return callback(error)
  305. }
  306. // TODO: Don't overwrite file if it hasn't been modified
  307. if (resource.url != null) {
  308. return UrlCache.downloadUrlToFile(
  309. project_id,
  310. resource.url,
  311. path,
  312. resource.modified,
  313. function (err) {
  314. if (err != null) {
  315. logger.err(
  316. {
  317. err,
  318. project_id,
  319. path,
  320. resource_url: resource.url,
  321. modified: resource.modified,
  322. },
  323. 'error downloading file for resources'
  324. )
  325. Metrics.inc('download-failed')
  326. }
  327. return callback()
  328. }
  329. ) // try and continue compiling even if http resource can not be downloaded at this time
  330. } else {
  331. fs.writeFile(path, resource.content, callback)
  332. }
  333. }
  334. )
  335. }
  336. )
  337. },
  338. checkPath(basePath, resourcePath, callback) {
  339. const path = Path.normalize(Path.join(basePath, resourcePath))
  340. if (path.slice(0, basePath.length + 1) !== basePath + '/') {
  341. return callback(new Error('resource path is outside root directory'))
  342. } else {
  343. return callback(null, path)
  344. }
  345. },
  346. }