FileSystemImportManager.coffee 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. async = require "async"
  2. fs = require "fs"
  3. _ = require "underscore"
  4. FileTypeManager = require "./FileTypeManager"
  5. EditorController = require "../Editor/EditorController"
  6. logger = require("logger-sharelatex")
  7. module.exports = FileSystemImportManager =
  8. addDoc: (user_id, project_id, folder_id, name, path, charset, replace, callback = (error, doc)-> )->
  9. FileSystemImportManager._isSafeOnFileSystem path, (err, isSafe)->
  10. if !isSafe
  11. logger.log user_id:user_id, project_id:project_id, folder_id:folder_id, name:name, path:path, "add doc is from symlink, stopping process"
  12. return callback("path is symlink")
  13. fs.readFile path, charset, (error, content) ->
  14. return callback(error) if error?
  15. content = content.replace(/\r\n?/g, "\n") # convert Windows line endings to unix. very old macs also created \r-separated lines
  16. lines = content.split("\n")
  17. if replace
  18. EditorController.upsertDoc project_id, folder_id, name, lines, "upload", user_id, callback
  19. else
  20. EditorController.addDoc project_id, folder_id, name, lines, "upload", user_id, callback
  21. addFile: (user_id, project_id, folder_id, name, path, replace, callback = (error, file)-> )->
  22. FileSystemImportManager._isSafeOnFileSystem path, (err, isSafe)->
  23. if !isSafe
  24. logger.log user_id:user_id, project_id:project_id, folder_id:folder_id, name:name, path:path, "add file is from symlink, stopping insert"
  25. return callback("path is symlink")
  26. if replace
  27. EditorController.upsertFile project_id, folder_id, name, path, null, "upload", user_id, callback
  28. else
  29. EditorController.addFile project_id, folder_id, name, path, null, "upload", user_id, callback
  30. addFolder: (user_id, project_id, folder_id, name, path, replace, callback = (error)-> ) ->
  31. FileSystemImportManager._isSafeOnFileSystem path, (err, isSafe)->
  32. if !isSafe
  33. logger.log user_id:user_id, project_id:project_id, folder_id:folder_id, path:path, "add folder is from symlink, stopping insert"
  34. return callback("path is symlink")
  35. EditorController.addFolder project_id, folder_id, name, "upload", (error, new_folder) =>
  36. return callback(error) if error?
  37. FileSystemImportManager.addFolderContents user_id, project_id, new_folder._id, path, replace, (error) ->
  38. return callback(error) if error?
  39. callback null, new_folder
  40. addFolderContents: (user_id, project_id, parent_folder_id, folderPath, replace, callback = (error)-> ) ->
  41. FileSystemImportManager._isSafeOnFileSystem folderPath, (err, isSafe)->
  42. if !isSafe
  43. logger.log user_id:user_id, project_id:project_id, parent_folder_id:parent_folder_id, folderPath:folderPath, "add folder contents is from symlink, stopping insert"
  44. return callback("path is symlink")
  45. fs.readdir folderPath, (error, entries = []) =>
  46. return callback(error) if error?
  47. async.eachSeries(
  48. entries,
  49. (entry, callback) =>
  50. FileTypeManager.shouldIgnore entry, (error, ignore) =>
  51. return callback(error) if error?
  52. if !ignore
  53. FileSystemImportManager.addEntity user_id, project_id, parent_folder_id, entry, "#{folderPath}/#{entry}", replace, callback
  54. else
  55. callback()
  56. callback
  57. )
  58. addEntity: (user_id, project_id, folder_id, name, path, replace, callback = (error, entity)-> ) ->
  59. FileSystemImportManager._isSafeOnFileSystem path, (err, isSafe)->
  60. if !isSafe
  61. logger.log user_id:user_id, project_id:project_id, folder_id:folder_id, path:path, "add entry is from symlink, stopping insert"
  62. return callback("path is symlink")
  63. FileTypeManager.isDirectory path, (error, isDirectory) =>
  64. return callback(error) if error?
  65. if isDirectory
  66. FileSystemImportManager.addFolder user_id, project_id, folder_id, name, path, replace, callback
  67. else
  68. FileTypeManager.getType name, path, (error, isBinary, charset) =>
  69. return callback(error) if error?
  70. if isBinary
  71. FileSystemImportManager.addFile user_id, project_id, folder_id, name, path, replace, (err, entity) ->
  72. entity?.type = 'file'
  73. callback(err, entity)
  74. else
  75. FileSystemImportManager.addDoc user_id, project_id, folder_id, name, path, charset, replace, (err, entity) ->
  76. entity?.type = 'doc'
  77. callback(err, entity)
  78. _isSafeOnFileSystem: (path, callback = (err, isSafe)->)->
  79. fs.lstat path, (err, stat)->
  80. if err?
  81. logger.err err:err, "error with path symlink check"
  82. return callback(err)
  83. isSafe = stat.isFile() or stat.isDirectory()
  84. callback(err, isSafe)