2014-02-12 10:23:40 +00:00
async = require " async "
fs = require " fs "
_ = require " underscore "
FileTypeManager = require " ./FileTypeManager "
EditorController = require " ../Editor/EditorController "
2016-03-12 12:01:36 +00:00
logger = require ( " logger-sharelatex " )
2014-02-12 10:23:40 +00:00
module.exports = FileSystemImportManager =
2016-02-23 16:46:14 +00:00
addDoc: ( user_id , project_id , folder_id , name , path , replace , callback = (error, doc)-> ) ->
2016-03-12 15:05:29 +00:00
FileSystemImportManager . _isSafeOnFileSystem path , (err, isSafe)->
if ! isSafe
2016-03-12 12:01:36 +00:00
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 "
return callback ( " path is symlink " )
fs . readFile path , " utf8 " , (error, content = "") ->
return callback ( error ) if error ?
content = content . replace ( /\r/g , " " )
lines = content . split ( " \n " )
if replace
2018-02-01 15:31:42 +00:00
EditorController . upsertDoc project_id , folder_id , name , lines , " upload " , user_id , callback
2016-03-12 12:01:36 +00:00
else
2018-02-01 15:31:42 +00:00
EditorController . addDoc project_id , folder_id , name , lines , " upload " , user_id , callback
2016-03-12 12:01:36 +00:00
addFile: ( user_id , project_id , folder_id , name , path , replace , callback = (error, file)-> ) ->
2016-03-12 15:05:29 +00:00
FileSystemImportManager . _isSafeOnFileSystem path , (err, isSafe)->
if ! isSafe
2016-03-12 12:01:36 +00:00
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 "
return callback ( " path is symlink " )
2018-02-01 15:31:42 +00:00
if replace
2018-02-14 15:12:46 +00:00
EditorController . upsertFile project_id , folder_id , name , path , null , " upload " , user_id , callback
2016-03-12 12:01:36 +00:00
else
2018-02-14 15:12:46 +00:00
EditorController . addFile project_id , folder_id , name , path , null , " upload " , user_id , callback
2014-02-12 10:23:40 +00:00
2016-02-23 16:46:14 +00:00
addFolder: ( user_id , project_id , folder_id , name , path , replace , callback = (error)-> ) ->
2016-03-12 15:05:29 +00:00
FileSystemImportManager . _isSafeOnFileSystem path , (err, isSafe)->
if ! isSafe
2016-03-12 12:01:36 +00:00
logger . log user_id : user_id , project_id : project_id , folder_id : folder_id , path : path , " add folder is from symlink, stopping insert "
return callback ( " path is symlink " )
2018-02-01 15:31:42 +00:00
EditorController . addFolder project_id , folder_id , name , " upload " , (error, new_folder) =>
2014-02-12 10:23:40 +00:00
return callback ( error ) if error ?
2016-03-12 12:01:36 +00:00
FileSystemImportManager . addFolderContents user_id , project_id , new_folder . _id , path , replace , (error) ->
return callback ( error ) if error ?
callback null , new_folder
2014-02-12 10:23:40 +00:00
2016-02-23 16:46:14 +00:00
addFolderContents: ( user_id , project_id , parent_folder_id , folderPath , replace , callback = (error)-> ) ->
2016-03-12 15:05:29 +00:00
FileSystemImportManager . _isSafeOnFileSystem folderPath , (err, isSafe)->
if ! isSafe
2016-03-12 12:01:36 +00:00
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 "
return callback ( " path is symlink " )
fs . readdir folderPath , (error, entries = []) =>
return callback ( error ) if error ?
2018-03-13 13:30:06 +00:00
async . eachSeries (
entries ,
(entry, callback) =>
2016-03-12 12:01:36 +00:00
FileTypeManager . shouldIgnore entry , (error, ignore) =>
return callback ( error ) if error ?
if ! ignore
FileSystemImportManager . addEntity user_id , project_id , parent_folder_id , entry , " #{ folderPath } / #{ entry } " , replace , callback
else
callback ( )
2018-03-13 13:30:06 +00:00
callback
)
2016-03-12 12:01:36 +00:00
addEntity: ( user_id , project_id , folder_id , name , path , replace , callback = (error, entity)-> ) ->
2016-03-12 15:05:29 +00:00
FileSystemImportManager . _isSafeOnFileSystem path , (err, isSafe)->
if ! isSafe
2016-03-12 12:01:36 +00:00
logger . log user_id : user_id , project_id : project_id , folder_id : folder_id , path : path , " add entry is from symlink, stopping insert "
return callback ( " path is symlink " )
FileTypeManager . isDirectory path , (error, isDirectory) =>
return callback ( error ) if error ?
if isDirectory
FileSystemImportManager . addFolder user_id , project_id , folder_id , name , path , replace , callback
else
FileTypeManager . isBinary name , path , (error, isBinary) =>
2014-02-12 10:23:40 +00:00
return callback ( error ) if error ?
2016-03-12 12:01:36 +00:00
if isBinary
2017-06-08 14:55:17 +00:00
FileSystemImportManager . addFile user_id , project_id , folder_id , name , path , replace , (err, entity) ->
entity ? . type = ' file '
callback ( err , entity )
2014-02-12 10:23:40 +00:00
else
2017-06-08 14:55:17 +00:00
FileSystemImportManager . addDoc user_id , project_id , folder_id , name , path , replace , (err, entity) ->
entity ? . type = ' doc '
callback ( err , entity )
2014-02-12 10:23:40 +00:00
2016-03-12 12:01:36 +00:00
2016-03-12 15:05:29 +00:00
_isSafeOnFileSystem: ( path , callback = (err, isSafe)-> ) ->
2016-03-12 12:01:36 +00:00
fs . lstat path , (err, stat)->
if err ?
logger . err err : err , " error with path symlink check "
return callback ( err )
2016-03-12 15:05:29 +00:00
isSafe = stat . isFile ( ) or stat . isDirectory ( )
callback ( err , isSafe )
2014-02-12 10:23:40 +00:00