ResourceWriter.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. } else {
  78. logger.log(
  79. { project_id: request.project_id, user_id: request.user_id },
  80. 'full sync'
  81. )
  82. return this.saveAllResourcesToDisk(
  83. request.project_id,
  84. request.resources,
  85. basePath,
  86. function (error) {
  87. if (error != null) {
  88. return callback(error)
  89. }
  90. return ResourceStateManager.saveProjectState(
  91. request.syncState,
  92. request.resources,
  93. basePath,
  94. function (error) {
  95. if (error != null) {
  96. return callback(error)
  97. }
  98. return callback(null, request.resources)
  99. }
  100. )
  101. }
  102. )
  103. }
  104. },
  105. saveIncrementalResourcesToDisk(project_id, resources, basePath, callback) {
  106. if (callback == null) {
  107. callback = function (error) {}
  108. }
  109. return this._createDirectory(basePath, error => {
  110. if (error != null) {
  111. return callback(error)
  112. }
  113. const jobs = Array.from(resources).map(resource =>
  114. (resource => {
  115. return callback =>
  116. this._writeResourceToDisk(project_id, resource, basePath, callback)
  117. })(resource)
  118. )
  119. return async.parallelLimit(jobs, parallelFileDownloads, callback)
  120. })
  121. },
  122. saveAllResourcesToDisk(project_id, resources, basePath, callback) {
  123. if (callback == null) {
  124. callback = function (error) {}
  125. }
  126. return this._createDirectory(basePath, error => {
  127. if (error != null) {
  128. return callback(error)
  129. }
  130. return this._removeExtraneousFiles(resources, basePath, error => {
  131. if (error != null) {
  132. return callback(error)
  133. }
  134. const jobs = Array.from(resources).map(resource =>
  135. (resource => {
  136. return callback =>
  137. this._writeResourceToDisk(
  138. project_id,
  139. resource,
  140. basePath,
  141. callback
  142. )
  143. })(resource)
  144. )
  145. return async.parallelLimit(jobs, parallelFileDownloads, callback)
  146. })
  147. })
  148. },
  149. _createDirectory(basePath, callback) {
  150. if (callback == null) {
  151. callback = function (error) {}
  152. }
  153. return fs.mkdir(basePath, function (err) {
  154. if (err != null) {
  155. if (err.code === 'EEXIST') {
  156. return callback()
  157. } else {
  158. logger.log({ err, dir: basePath }, 'error creating directory')
  159. return callback(err)
  160. }
  161. } else {
  162. return callback()
  163. }
  164. })
  165. },
  166. _removeExtraneousFiles(resources, basePath, _callback) {
  167. if (_callback == null) {
  168. _callback = function (error, outputFiles, allFiles) {}
  169. }
  170. const timer = new Metrics.Timer('unlink-output-files')
  171. const callback = function (error, ...result) {
  172. timer.done()
  173. return _callback(error, ...Array.from(result))
  174. }
  175. return OutputFileFinder.findOutputFiles(
  176. resources,
  177. basePath,
  178. function (error, outputFiles, allFiles) {
  179. if (error != null) {
  180. return callback(error)
  181. }
  182. const jobs = []
  183. for (const file of Array.from(outputFiles || [])) {
  184. ;(function (file) {
  185. const { path } = file
  186. let should_delete = true
  187. if (
  188. path.match(/^output\./) ||
  189. path.match(/\.aux$/) ||
  190. path.match(/^cache\//)
  191. ) {
  192. // knitr cache
  193. should_delete = false
  194. }
  195. if (path.match(/^output-.*/)) {
  196. // Tikz cached figures (default case)
  197. should_delete = false
  198. }
  199. if (path.match(/\.(pdf|dpth|md5)$/)) {
  200. // Tikz cached figures (by extension)
  201. should_delete = false
  202. }
  203. if (
  204. path.match(/\.(pygtex|pygstyle)$/) ||
  205. path.match(/(^|\/)_minted-[^\/]+\//)
  206. ) {
  207. // minted files/directory
  208. should_delete = false
  209. }
  210. if (
  211. path.match(/\.md\.tex$/) ||
  212. path.match(/(^|\/)_markdown_[^\/]+\//)
  213. ) {
  214. // markdown files/directory
  215. should_delete = false
  216. }
  217. if (path.match(/-eps-converted-to\.pdf$/)) {
  218. // Epstopdf generated files
  219. should_delete = false
  220. }
  221. if (
  222. path === 'output.pdf' ||
  223. path === 'output.dvi' ||
  224. path === 'output.log' ||
  225. path === 'output.xdv' ||
  226. path === 'output.stdout' ||
  227. path === 'output.stderr'
  228. ) {
  229. should_delete = true
  230. }
  231. if (path === 'output.tex') {
  232. // created by TikzManager if present in output files
  233. should_delete = true
  234. }
  235. if (should_delete) {
  236. return jobs.push(callback =>
  237. ResourceWriter._deleteFileIfNotDirectory(
  238. Path.join(basePath, path),
  239. callback
  240. )
  241. )
  242. }
  243. })(file)
  244. }
  245. return async.series(jobs, function (error) {
  246. if (error != null) {
  247. return callback(error)
  248. }
  249. return callback(null, outputFiles, allFiles)
  250. })
  251. }
  252. )
  253. },
  254. _deleteFileIfNotDirectory(path, callback) {
  255. if (callback == null) {
  256. callback = function (error) {}
  257. }
  258. return fs.stat(path, function (error, stat) {
  259. if (error != null && error.code === 'ENOENT') {
  260. return callback()
  261. } else if (error != null) {
  262. logger.err(
  263. { err: error, path },
  264. 'error stating file in deleteFileIfNotDirectory'
  265. )
  266. return callback(error)
  267. } else if (stat.isFile()) {
  268. return fs.unlink(path, function (error) {
  269. if (error != null) {
  270. logger.err(
  271. { err: error, path },
  272. 'error removing file in deleteFileIfNotDirectory'
  273. )
  274. return callback(error)
  275. } else {
  276. return callback()
  277. }
  278. })
  279. } else {
  280. return callback()
  281. }
  282. })
  283. },
  284. _writeResourceToDisk(project_id, resource, basePath, callback) {
  285. if (callback == null) {
  286. callback = function (error) {}
  287. }
  288. return ResourceWriter.checkPath(
  289. basePath,
  290. resource.path,
  291. function (error, path) {
  292. if (error != null) {
  293. return callback(error)
  294. }
  295. return fs.mkdir(
  296. Path.dirname(path),
  297. { recursive: true },
  298. function (error) {
  299. if (error != null) {
  300. return callback(error)
  301. }
  302. // TODO: Don't overwrite file if it hasn't been modified
  303. if (resource.url != null) {
  304. return UrlCache.downloadUrlToFile(
  305. project_id,
  306. resource.url,
  307. path,
  308. resource.modified,
  309. function (err) {
  310. if (err != null) {
  311. logger.err(
  312. {
  313. err,
  314. project_id,
  315. path,
  316. resource_url: resource.url,
  317. modified: resource.modified,
  318. },
  319. 'error downloading file for resources'
  320. )
  321. Metrics.inc('download-failed')
  322. }
  323. return callback()
  324. }
  325. ) // try and continue compiling even if http resource can not be downloaded at this time
  326. } else {
  327. fs.writeFile(path, resource.content, callback)
  328. }
  329. }
  330. )
  331. }
  332. )
  333. },
  334. checkPath(basePath, resourcePath, callback) {
  335. const path = Path.normalize(Path.join(basePath, resourcePath))
  336. if (path.slice(0, basePath.length + 1) !== basePath + '/') {
  337. return callback(new Error('resource path is outside root directory'))
  338. } else {
  339. return callback(null, path)
  340. }
  341. },
  342. }