2018-03-08 12:24:54 -05:00
|
|
|
Settings = require 'settings-sharelatex'
|
|
|
|
Path = require 'path'
|
|
|
|
FileWriter = require '../../infrastructure/FileWriter'
|
|
|
|
FileSystemImportManager = require '../Uploads/FileSystemImportManager'
|
2018-03-16 08:21:07 -04:00
|
|
|
ProjectEntityHandler = require '../Project/ProjectEntityHandler'
|
2018-03-08 12:24:54 -05:00
|
|
|
ProjectLocator = require '../Project/ProjectLocator'
|
2018-03-16 08:21:07 -04:00
|
|
|
EditorController = require '../Editor/EditorController'
|
2018-03-09 11:36:10 -05:00
|
|
|
Errors = require '../Errors/Errors'
|
|
|
|
moment = require 'moment'
|
2018-03-08 12:24:54 -05:00
|
|
|
|
|
|
|
module.exports = RestoreManager =
|
2018-03-16 08:21:07 -04:00
|
|
|
restoreDocFromDeletedDoc: (user_id, project_id, doc_id, name, callback = (error, doc, folder_id) ->) ->
|
|
|
|
# getDoc will return the deleted doc's lines, but we don't actually remove
|
|
|
|
# the deleted doc, just create a new one from its lines.
|
|
|
|
ProjectEntityHandler.getDoc project_id, doc_id, include_deleted: true, (error, lines) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
addDocWithName = (name, callback) ->
|
|
|
|
EditorController.addDoc project_id, null, name, lines, 'restore', user_id, callback
|
|
|
|
RestoreManager._addEntityWithUniqueName addDocWithName, name, callback
|
|
|
|
|
|
|
|
restoreFileFromV2: (user_id, project_id, version, pathname, callback = (error, entity) ->) ->
|
2018-03-08 12:24:54 -05:00
|
|
|
RestoreManager._writeFileVersionToDisk project_id, version, pathname, (error, fsPath) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
basename = Path.basename(pathname)
|
|
|
|
dirname = Path.dirname(pathname)
|
|
|
|
if dirname == '.' # no directory
|
|
|
|
dirname = ''
|
2018-03-09 11:36:10 -05:00
|
|
|
RestoreManager._findFolderOrRootFolderId project_id, dirname, (error, parent_folder_id) ->
|
2018-03-08 12:24:54 -05:00
|
|
|
return callback(error) if error?
|
2018-03-16 08:21:07 -04:00
|
|
|
addEntityWithName = (name, callback) ->
|
|
|
|
FileSystemImportManager.addEntity user_id, project_id, parent_folder_id, name, fsPath, false, callback
|
|
|
|
RestoreManager._addEntityWithUniqueName addEntityWithName, basename, callback
|
2018-03-09 11:36:10 -05:00
|
|
|
|
|
|
|
_findFolderOrRootFolderId: (project_id, dirname, callback = (error, folder_id) ->) ->
|
|
|
|
# We're going to try to recover the file into the folder it was in previously,
|
|
|
|
# but this is historical, so the folder may not exist anymore. Fallback to the
|
|
|
|
# root folder if not (folder_id == null)
|
|
|
|
ProjectLocator.findElementByPath {project_id, path: dirname}, (error, element, type) ->
|
|
|
|
if error? and not error instanceof Errors.NotFoundError
|
|
|
|
return callback(error)
|
|
|
|
if type == 'folder' and element?
|
|
|
|
return callback(null, element._id)
|
|
|
|
else
|
|
|
|
return callback(null, null)
|
|
|
|
|
2018-03-16 08:21:07 -04:00
|
|
|
_addEntityWithUniqueName: (addEntityWithName, basename, callback = (error) ->) ->
|
|
|
|
addEntityWithName basename, (error, entity) ->
|
2018-03-09 11:36:10 -05:00
|
|
|
if error?
|
|
|
|
if error instanceof Errors.InvalidNameError
|
|
|
|
# likely a duplicate name, so try with a prefix
|
|
|
|
date = moment(new Date()).format('Do MMM YY H:mm:ss')
|
|
|
|
# Move extension to the end so the file type is preserved
|
|
|
|
extension = Path.extname(basename)
|
|
|
|
basename = Path.basename(basename, extension)
|
|
|
|
basename = "#{basename} (Restored on #{date})"
|
|
|
|
if extension != ''
|
|
|
|
basename = "#{basename}#{extension}"
|
2018-03-16 08:21:07 -04:00
|
|
|
addEntityWithName basename, callback
|
2018-03-08 12:24:54 -05:00
|
|
|
else
|
2018-03-09 11:36:10 -05:00
|
|
|
callback(error)
|
|
|
|
else
|
2018-03-12 07:47:56 -04:00
|
|
|
callback(null, entity)
|
2018-03-08 12:24:54 -05:00
|
|
|
|
|
|
|
_writeFileVersionToDisk: (project_id, version, pathname, callback = (error, fsPath) ->) ->
|
2018-03-09 11:36:10 -05:00
|
|
|
url = "#{Settings.apis.project_history.url}/project/#{project_id}/version/#{version}/#{encodeURIComponent(pathname)}"
|
2018-03-08 12:24:54 -05:00
|
|
|
FileWriter.writeUrlToDisk project_id, url, callback
|