2014-02-12 05:23:40 -05:00
|
|
|
logger = require "logger-sharelatex"
|
2017-04-03 11:18:30 -04:00
|
|
|
metrics = require "metrics-sharelatex"
|
2014-02-12 05:23:40 -05:00
|
|
|
fs = require "fs"
|
|
|
|
Path = require "path"
|
|
|
|
FileSystemImportManager = require "./FileSystemImportManager"
|
|
|
|
ProjectUploadManager = require "./ProjectUploadManager"
|
2016-09-05 10:58:31 -04:00
|
|
|
AuthenticationController = require('../Authentication/AuthenticationController')
|
2018-01-02 07:43:41 -05:00
|
|
|
LockManager = require("../../infrastructure/LockManager")
|
2014-02-12 05:23:40 -05:00
|
|
|
|
|
|
|
module.exports = ProjectUploadController =
|
|
|
|
uploadProject: (req, res, next) ->
|
|
|
|
timer = new metrics.Timer("project-upload")
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
2015-06-30 11:37:21 -04:00
|
|
|
{originalname, path} = req.files.qqfile
|
|
|
|
name = Path.basename(originalname, ".zip")
|
2014-02-12 05:23:40 -05:00
|
|
|
ProjectUploadManager.createProjectFromZipArchive user_id, name, path, (error, project) ->
|
|
|
|
fs.unlink path, ->
|
|
|
|
timer.done()
|
|
|
|
if error?
|
|
|
|
logger.error
|
|
|
|
err: error, file_path: path, file_name: name,
|
|
|
|
"error uploading project"
|
|
|
|
res.send success: false
|
|
|
|
else
|
|
|
|
logger.log
|
|
|
|
project: project._id, file_path: path, file_name: name,
|
|
|
|
"uploaded project"
|
|
|
|
res.send success: true, project_id: project._id
|
2016-09-05 10:58:31 -04:00
|
|
|
|
2014-02-12 05:23:40 -05:00
|
|
|
uploadFile: (req, res, next) ->
|
|
|
|
timer = new metrics.Timer("file-upload")
|
2016-08-02 10:36:59 -04:00
|
|
|
name = req.files.qqfile?.originalname
|
|
|
|
path = req.files.qqfile?.path
|
2014-02-12 05:23:40 -05:00
|
|
|
project_id = req.params.Project_id
|
|
|
|
folder_id = req.query.folder_id
|
2015-03-02 07:46:02 -05:00
|
|
|
if !name? or name.length == 0 or name.length > 150
|
|
|
|
logger.err project_id:project_id, name:name, "bad name when trying to upload file"
|
|
|
|
return res.send success: false
|
2016-02-29 11:03:22 -05:00
|
|
|
logger.log folder_id:folder_id, project_id:project_id, "getting upload file request"
|
2016-09-05 10:58:31 -04:00
|
|
|
user_id = AuthenticationController.getLoggedInUserId(req)
|
2018-01-02 07:43:41 -05:00
|
|
|
|
2018-01-09 11:00:08 -05:00
|
|
|
LockManager.runWithLock project_id,
|
|
|
|
(cb) -> FileSystemImportManager.addEntity user_id, project_id, folder_id, name, path, true, cb
|
|
|
|
(error, entity) ->
|
|
|
|
fs.unlink path, ->
|
|
|
|
timer.done()
|
|
|
|
if error?
|
|
|
|
logger.error
|
|
|
|
err: error, project_id: project_id, file_path: path,
|
|
|
|
file_name: name, folder_id: folder_id,
|
|
|
|
"error uploading file"
|
|
|
|
res.send success: false
|
|
|
|
else
|
|
|
|
logger.log
|
|
|
|
project_id: project_id, file_path: path, file_name: name, folder_id: folder_id
|
|
|
|
"uploaded file"
|
|
|
|
res.send success: true, entity_id: entity?._id, entity_type: entity?.type
|
2017-06-08 10:12:18 -04:00
|
|
|
|