overleaf/services/web/app/coffee/Features/Uploads/ProjectUploadController.coffee

55 lines
2 KiB
CoffeeScript
Raw Normal View History

2014-02-12 05:23:40 -05:00
logger = require "logger-sharelatex"
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')
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)
{originalname, path} = req.file
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")
name = req.file?.originalname
path = req.file?.path
2014-02-12 05:23:40 -05:00
project_id = req.params.Project_id
folder_id = req.query.folder_id
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
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
FileSystemImportManager.addEntity user_id, project_id, folder_id, name, path, true, (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