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

82 lines
3 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')
Settings = require "settings-sharelatex"
Errors = require "../Errors/Errors"
multer = require('multer')
upload = null
try
upload = multer(
dest: Settings.path.uploadFolder
limits: fileSize: Settings.maxUploadSize
)
catch err
if err.message == "EEXIST"
logger.log uploadFolder:Settings.path.uploadFolder, "dir already exists, continuing"
else
logger.err err:err, "caught error from multer in uploads router"
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"
if error.name? && error.name == 'InvalidError'
res.status(422).json { success: false, error: req.i18n.translate(error.message) }
else
res.status(500).json { success: false, error: req.i18n.translate("upload_failed") }
2014-02-12 05:23:40 -05:00
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
multerMiddleware: (req, res, next) ->
return res.status(500).json {success: false, error: req.i18n.translate("upload_failed")} unless upload?
upload.single('qqfile') req, res, (err) ->
if err instanceof multer.MulterError && err.code == 'LIMIT_FILE_SIZE'
return res.status(422).json {success: false, error: req.i18n.translate("file_too_large")}
next(err)