ResourceWriter.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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(resources, basePath, function (
  176. error,
  177. outputFiles,
  178. allFiles
  179. ) {
  180. if (error != null) {
  181. return callback(error)
  182. }
  183. const jobs = []
  184. for (const file of Array.from(outputFiles || [])) {
  185. ;(function (file) {
  186. const { path } = file
  187. let should_delete = true
  188. if (
  189. path.match(/^output\./) ||
  190. path.match(/\.aux$/) ||
  191. path.match(/^cache\//)
  192. ) {
  193. // knitr cache
  194. should_delete = false
  195. }
  196. if (path.match(/^output-.*/)) {
  197. // Tikz cached figures (default case)
  198. should_delete = false
  199. }
  200. if (path.match(/\.(pdf|dpth|md5)$/)) {
  201. // Tikz cached figures (by extension)
  202. should_delete = false
  203. }
  204. if (
  205. path.match(/\.(pygtex|pygstyle)$/) ||
  206. path.match(/(^|\/)_minted-[^\/]+\//)
  207. ) {
  208. // minted files/directory
  209. should_delete = false
  210. }
  211. if (
  212. path.match(/\.md\.tex$/) ||
  213. path.match(/(^|\/)_markdown_[^\/]+\//)
  214. ) {
  215. // markdown files/directory
  216. should_delete = false
  217. }
  218. if (path.match(/-eps-converted-to\.pdf$/)) {
  219. // Epstopdf generated files
  220. should_delete = false
  221. }
  222. if (
  223. path === 'output.pdf' ||
  224. path === 'output.dvi' ||
  225. path === 'output.log' ||
  226. path === 'output.xdv' ||
  227. path === 'output.stdout' ||
  228. path === 'output.stderr'
  229. ) {
  230. should_delete = true
  231. }
  232. if (path === 'output.tex') {
  233. // created by TikzManager if present in output files
  234. should_delete = true
  235. }
  236. if (should_delete) {
  237. return jobs.push((callback) =>
  238. ResourceWriter._deleteFileIfNotDirectory(
  239. Path.join(basePath, path),
  240. callback
  241. )
  242. )
  243. }
  244. })(file)
  245. }
  246. return async.series(jobs, function (error) {
  247. if (error != null) {
  248. return callback(error)
  249. }
  250. return callback(null, outputFiles, allFiles)
  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(basePath, resource.path, function (
  289. error,
  290. path
  291. ) {
  292. if (error != null) {
  293. return callback(error)
  294. }
  295. return fs.mkdir(Path.dirname(path), { recursive: true }, function (
  296. error
  297. ) {
  298. if (error != null) {
  299. return callback(error)
  300. }
  301. // TODO: Don't overwrite file if it hasn't been modified
  302. if (resource.url != null) {
  303. return UrlCache.downloadUrlToFile(
  304. project_id,
  305. resource.url,
  306. path,
  307. resource.modified,
  308. function (err) {
  309. if (err != null) {
  310. logger.err(
  311. {
  312. err,
  313. project_id,
  314. path,
  315. resource_url: resource.url,
  316. modified: resource.modified
  317. },
  318. 'error downloading file for resources'
  319. )
  320. Metrics.inc('download-failed')
  321. }
  322. return callback()
  323. }
  324. ) // try and continue compiling even if http resource can not be downloaded at this time
  325. } else {
  326. fs.writeFile(path, resource.content, callback)
  327. }
  328. })
  329. })
  330. },
  331. checkPath(basePath, resourcePath, callback) {
  332. const path = Path.normalize(Path.join(basePath, resourcePath))
  333. if (path.slice(0, basePath.length + 1) !== basePath + '/') {
  334. return callback(new Error('resource path is outside root directory'))
  335. } else {
  336. return callback(null, path)
  337. }
  338. }
  339. }