2018-06-20 05:01:03 -04:00
|
|
|
FileWriter = require '../../infrastructure/FileWriter'
|
|
|
|
EditorController = require '../Editor/EditorController'
|
2018-06-21 06:40:36 -04:00
|
|
|
ProjectLocator = require '../Project/ProjectLocator'
|
2018-06-21 10:27:32 -04:00
|
|
|
Project = require("../../models/Project").Project
|
|
|
|
ProjectGetter = require("../Project/ProjectGetter")
|
2018-06-20 05:01:03 -04:00
|
|
|
_ = require 'underscore'
|
2018-06-21 10:27:32 -04:00
|
|
|
{
|
|
|
|
ProjectNotFoundError,
|
|
|
|
V1ProjectNotFoundError,
|
|
|
|
BadDataError
|
|
|
|
} = require './LinkedFilesErrors'
|
2018-06-20 05:01:03 -04:00
|
|
|
|
|
|
|
|
|
|
|
module.exports = LinkedFilesHandler =
|
|
|
|
|
2018-06-21 06:40:36 -04:00
|
|
|
getFileById: (project_id, file_id, callback=(err, file)->) ->
|
|
|
|
ProjectLocator.findElement {
|
|
|
|
project_id,
|
|
|
|
element_id: file_id,
|
|
|
|
type: 'file'
|
|
|
|
}, (err, file, path, parentFolder) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
callback(null, file, path, parentFolder)
|
|
|
|
|
2018-06-21 10:27:32 -04:00
|
|
|
getSourceProject: (data, callback=(err, project)->) ->
|
|
|
|
projection = {_id: 1, name: 1}
|
|
|
|
if data.v1_source_doc_id?
|
|
|
|
Project.findOne {'overleaf.id': data.v1_source_doc_id}, projection, (err, project) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
if !project?
|
|
|
|
return callback(new V1ProjectNotFoundError())
|
|
|
|
callback(null, project)
|
|
|
|
else if data.source_project_id?
|
|
|
|
ProjectGetter.getProject data.source_project_id, projection, (err, project) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
if !project?
|
|
|
|
return callback(new ProjectNotFoundError())
|
|
|
|
callback(null, project)
|
|
|
|
else
|
|
|
|
callback(new BadDataError('neither v1 nor v2 id present'))
|
|
|
|
|
2018-06-20 05:01:03 -04:00
|
|
|
importFromStream: (
|
|
|
|
project_id,
|
|
|
|
readStream,
|
|
|
|
linkedFileData,
|
|
|
|
name,
|
|
|
|
parent_folder_id,
|
|
|
|
user_id,
|
|
|
|
callback=(err, file)->
|
|
|
|
) ->
|
|
|
|
callback = _.once(callback)
|
|
|
|
FileWriter.writeStreamToDisk project_id, readStream, (err, fsPath) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
EditorController.upsertFile project_id,
|
|
|
|
parent_folder_id,
|
|
|
|
name,
|
|
|
|
fsPath,
|
|
|
|
linkedFileData,
|
|
|
|
"upload",
|
|
|
|
user_id,
|
|
|
|
(err, file) =>
|
|
|
|
return callback(err) if err?
|
|
|
|
callback(null, file)
|
|
|
|
|
|
|
|
importContent: (
|
|
|
|
project_id,
|
|
|
|
content,
|
|
|
|
linkedFileData,
|
|
|
|
name,
|
|
|
|
parent_folder_id,
|
|
|
|
user_id,
|
|
|
|
callback=(err, file)->
|
|
|
|
) ->
|
|
|
|
callback = _.once(callback)
|
|
|
|
FileWriter.writeContentToDisk project_id, content, (err, fsPath) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
EditorController.upsertFile project_id,
|
|
|
|
parent_folder_id,
|
|
|
|
name,
|
|
|
|
fsPath,
|
|
|
|
linkedFileData,
|
|
|
|
"upload",
|
|
|
|
user_id,
|
|
|
|
(err, file) =>
|
|
|
|
return callback(err) if err?
|
|
|
|
callback(null, file)
|