overleaf/services/filestore/app/coffee/FileHandler.coffee

103 lines
3.7 KiB
CoffeeScript
Raw Normal View History

2014-02-14 16:39:05 +00:00
settings = require("settings-sharelatex")
PersistorManager = require("./PersistorManager")
2014-02-14 16:39:05 +00:00
LocalFileWriter = require("./LocalFileWriter")
logger = require("logger-sharelatex")
FileConverter = require("./FileConverter")
KeyBuilder = require("./KeyBuilder")
async = require("async")
ImageOptimiser = require("./ImageOptimiser")
module.exports =
insertFile: (bucket, key, stream, callback)->
convertedKey = KeyBuilder.getConvertedFolderKey key
PersistorManager.deleteDirectory bucket, convertedKey, (error) ->
2014-06-02 16:11:25 +00:00
return callback(error) if error?
PersistorManager.sendStream bucket, key, stream, callback
2014-02-14 16:39:05 +00:00
deleteFile: (bucket, key, callback)->
convertedKey = KeyBuilder.getConvertedFolderKey key
2014-02-14 16:39:05 +00:00
async.parallel [
(done)-> PersistorManager.deleteFile bucket, key, done
(done)-> PersistorManager.deleteDirectory bucket, convertedKey, done
2014-02-14 16:39:05 +00:00
], callback
getFile: (bucket, key, opts = {}, callback)->
2014-02-14 16:39:05 +00:00
logger.log bucket:bucket, key:key, opts:opts, "getting file"
if !opts.format? and !opts.style?
@_getStandardFile bucket, key, opts, callback
else
@_getConvertedFile bucket, key, opts, callback
_getStandardFile: (bucket, key, opts, callback)->
PersistorManager.getFileStream bucket, key, opts, (err, fileStream)->
2014-02-14 16:39:05 +00:00
if err?
logger.err bucket:bucket, key:key, opts:opts, "error getting fileStream"
callback err, fileStream
2014-02-14 16:39:05 +00:00
_getConvertedFile: (bucket, key, opts, callback)->
convertedKey = KeyBuilder.addCachingToKey key, opts
PersistorManager.checkIfFileExists bucket, convertedKey, (err, exists)=>
2015-09-10 10:10:30 +00:00
if err?
return callback err
2014-02-14 16:39:05 +00:00
if exists
PersistorManager.getFileStream bucket, convertedKey, opts, callback
2014-02-14 16:39:05 +00:00
else
@_getConvertedFileAndCache bucket, key, convertedKey, opts, callback
2014-02-14 16:39:05 +00:00
_getConvertedFileAndCache: (bucket, key, convertedKey, opts, callback)->
convertedFsPath = ""
2016-12-13 14:29:04 +00:00
originalFsPath = ""
async.series [
(cb) =>
@_convertFile bucket, key, opts, (err, fileSystemPath, originalFsPath) ->
convertedFsPath = fileSystemPath
2016-12-13 14:29:04 +00:00
originalFsPath = originalFsPath
cb err
(cb)->
ImageOptimiser.compressPng convertedFsPath, cb
(cb)->
PersistorManager.sendFile bucket, convertedKey, convertedFsPath, cb
], (err)->
2014-02-14 16:39:05 +00:00
if err?
LocalFileWriter.deleteFile convertedFsPath, ->
LocalFileWriter.deleteFile originalFsPath, ->
2014-02-14 16:39:05 +00:00
return callback(err)
PersistorManager.getFileStream bucket, convertedKey, opts, callback
2014-02-14 16:39:05 +00:00
_convertFile: (bucket, originalKey, opts, callback)->
2015-11-16 08:59:27 +00:00
@_writeS3FileToDisk bucket, originalKey, opts, (err, originalFsPath)->
2015-09-10 10:10:30 +00:00
if err?
return callback(err)
done = (err, destPath)->
if err?
logger.err err:err, bucket:bucket, originalKey:originalKey, opts:opts, "error converting file"
return callback(err)
LocalFileWriter.deleteFile originalFsPath, ->
callback(err, destPath, originalFsPath)
logger.log opts:opts, "converting file depending on opts"
2014-02-14 16:39:05 +00:00
if opts.format?
FileConverter.convert originalFsPath, opts.format, done
2014-02-14 16:39:05 +00:00
else if opts.style == "thumbnail"
FileConverter.thumbnail originalFsPath, done
2014-02-14 16:39:05 +00:00
else if opts.style == "preview"
FileConverter.preview originalFsPath, done
else
2015-09-10 10:10:30 +00:00
return callback(new Error("should have specified opts to convert file with #{JSON.stringify(opts)}"))
2014-02-14 16:39:05 +00:00
_writeS3FileToDisk: (bucket, key, opts, callback)->
PersistorManager.getFileStream bucket, key, opts, (err, fileStream)->
if err?
return callback(err)
2014-02-14 16:39:05 +00:00
LocalFileWriter.writeStream fileStream, key, callback
getDirectorySize: (bucket, project_id, callback)->
logger.log bucket:bucket, project_id:project_id, "getting project size"
PersistorManager.directorySize bucket, project_id, (err, size)->
if err?
logger.err bucket:bucket, project_id:project_id, "error getting size"
callback err, size