2014-02-14 11:39:05 -05:00
|
|
|
settings = require("settings-sharelatex")
|
2014-02-26 04:15:37 -05:00
|
|
|
PersistorManager = require("./PersistorManager")
|
2014-02-14 11:39:05 -05:00
|
|
|
LocalFileWriter = require("./LocalFileWriter")
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
FileConverter = require("./FileConverter")
|
|
|
|
KeyBuilder = require("./KeyBuilder")
|
|
|
|
async = require("async")
|
|
|
|
ImageOptimiser = require("./ImageOptimiser")
|
2018-11-09 09:04:26 -05:00
|
|
|
Errors = require('./Errors')
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2018-07-04 11:41:31 -04:00
|
|
|
module.exports = FileHandler =
|
2014-02-14 11:39:05 -05:00
|
|
|
|
|
|
|
insertFile: (bucket, key, stream, callback)->
|
2015-07-24 03:06:52 -04:00
|
|
|
convertedKey = KeyBuilder.getConvertedFolderKey key
|
|
|
|
PersistorManager.deleteDirectory bucket, convertedKey, (error) ->
|
2014-06-02 12:11:25 -04:00
|
|
|
return callback(error) if error?
|
|
|
|
PersistorManager.sendStream bucket, key, stream, callback
|
2014-02-14 11:39:05 -05:00
|
|
|
|
|
|
|
deleteFile: (bucket, key, callback)->
|
2015-07-24 03:06:52 -04:00
|
|
|
convertedKey = KeyBuilder.getConvertedFolderKey key
|
2014-02-14 11:39:05 -05:00
|
|
|
async.parallel [
|
2014-02-26 04:15:37 -05:00
|
|
|
(done)-> PersistorManager.deleteFile bucket, key, done
|
2015-07-24 03:06:52 -04:00
|
|
|
(done)-> PersistorManager.deleteDirectory bucket, convertedKey, done
|
2014-02-14 11:39:05 -05:00
|
|
|
], callback
|
|
|
|
|
2014-05-23 08:57:18 -04:00
|
|
|
getFile: (bucket, key, opts = {}, callback)->
|
2018-07-04 11:41:31 -04:00
|
|
|
# In this call, opts can contain credentials
|
|
|
|
logger.log bucket:bucket, key:key, opts:@_scrubSecrets(opts), "getting file"
|
2014-02-14 11:39:05 -05:00
|
|
|
if !opts.format? and !opts.style?
|
|
|
|
@_getStandardFile bucket, key, opts, callback
|
|
|
|
else
|
|
|
|
@_getConvertedFile bucket, key, opts, callback
|
|
|
|
|
2014-05-23 08:57:18 -04:00
|
|
|
_getStandardFile: (bucket, key, opts, callback)->
|
2015-08-27 10:21:33 -04:00
|
|
|
PersistorManager.getFileStream bucket, key, opts, (err, fileStream)->
|
2018-11-09 09:04:26 -05:00
|
|
|
if err? and !(err instanceof Errors.NotFoundError)
|
2018-07-04 11:41:31 -04:00
|
|
|
logger.err bucket:bucket, key:key, opts:FileHandler._scrubSecrets(opts), "error getting fileStream"
|
2014-05-23 08:57:18 -04:00
|
|
|
callback err, fileStream
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2014-05-23 08:57:18 -04:00
|
|
|
_getConvertedFile: (bucket, key, opts, callback)->
|
2015-07-24 03:06:52 -04:00
|
|
|
convertedKey = KeyBuilder.addCachingToKey key, opts
|
|
|
|
PersistorManager.checkIfFileExists bucket, convertedKey, (err, exists)=>
|
2015-09-10 06:10:30 -04:00
|
|
|
if err?
|
2015-11-04 09:20:15 -05:00
|
|
|
return callback err
|
2014-02-14 11:39:05 -05:00
|
|
|
if exists
|
2015-11-20 06:07:04 -05:00
|
|
|
PersistorManager.getFileStream bucket, convertedKey, opts, callback
|
2014-02-14 11:39:05 -05:00
|
|
|
else
|
2015-07-24 03:06:52 -04:00
|
|
|
@_getConvertedFileAndCache bucket, key, convertedKey, opts, callback
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2015-07-24 03:06:52 -04:00
|
|
|
_getConvertedFileAndCache: (bucket, key, convertedKey, opts, callback)->
|
2014-03-04 08:36:47 -05:00
|
|
|
convertedFsPath = ""
|
2016-12-13 09:29:04 -05:00
|
|
|
originalFsPath = ""
|
2014-03-04 08:36:47 -05:00
|
|
|
async.series [
|
2015-11-20 06:02:22 -05:00
|
|
|
(cb) =>
|
2016-12-13 07:43:57 -05:00
|
|
|
@_convertFile bucket, key, opts, (err, fileSystemPath, originalFsPath) ->
|
2014-03-04 08:36:47 -05:00
|
|
|
convertedFsPath = fileSystemPath
|
2016-12-13 09:29:04 -05:00
|
|
|
originalFsPath = originalFsPath
|
2014-03-04 08:36:47 -05:00
|
|
|
cb err
|
|
|
|
(cb)->
|
|
|
|
ImageOptimiser.compressPng convertedFsPath, cb
|
|
|
|
(cb)->
|
2015-07-24 03:06:52 -04:00
|
|
|
PersistorManager.sendFile bucket, convertedKey, convertedFsPath, cb
|
2014-03-04 08:36:47 -05:00
|
|
|
], (err)->
|
2014-02-14 11:39:05 -05:00
|
|
|
if err?
|
2016-12-13 07:43:57 -05:00
|
|
|
LocalFileWriter.deleteFile convertedFsPath, ->
|
|
|
|
LocalFileWriter.deleteFile originalFsPath, ->
|
2014-02-14 11:39:05 -05:00
|
|
|
return callback(err)
|
2018-11-07 11:23:55 -05:00
|
|
|
# Send back the converted file from the local copy to avoid problems
|
|
|
|
# with the file not being present in S3 yet. As described in the
|
|
|
|
# documentation below, we have already made a 'HEAD' request in
|
|
|
|
# checkIfFileExists so we only have "eventual consistency" if we try
|
|
|
|
# to stream it from S3 here. This was a cause of many 403 errors.
|
|
|
|
#
|
|
|
|
# "Amazon S3 provides read-after-write consistency for PUTS of new
|
|
|
|
# objects in your S3 bucket in all regions with one caveat. The
|
|
|
|
# caveat is that if you make a HEAD or GET request to the key name
|
|
|
|
# (to find if the object exists) before creating the object, Amazon
|
|
|
|
# S3 provides eventual consistency for read-after-write.""
|
|
|
|
# https://docs.aws.amazon.com/AmazonS3/latest/dev/Introduction.html#ConsistencyModel
|
|
|
|
LocalFileWriter.getStream convertedFsPath, (err, readStream) ->
|
|
|
|
return callback(err) if err?
|
|
|
|
readStream.on 'end', () ->
|
|
|
|
logger.log {convertedFsPath: convertedFsPath}, "deleting temporary file"
|
|
|
|
LocalFileWriter.deleteFile convertedFsPath, ->
|
|
|
|
callback(null, readStream)
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2015-07-24 03:06:52 -04:00
|
|
|
_convertFile: (bucket, originalKey, opts, callback)->
|
2015-11-16 03:59:27 -05:00
|
|
|
@_writeS3FileToDisk bucket, originalKey, opts, (err, originalFsPath)->
|
2015-09-10 06:10:30 -04:00
|
|
|
if err?
|
|
|
|
return callback(err)
|
2014-03-04 08:36:47 -05:00
|
|
|
done = (err, destPath)->
|
|
|
|
if err?
|
2018-07-04 11:41:31 -04:00
|
|
|
logger.err err:err, bucket:bucket, originalKey:originalKey, opts:FileHandler._scrubSecrets(opts), "error converting file"
|
2014-03-04 08:36:47 -05:00
|
|
|
return callback(err)
|
2015-07-24 03:06:52 -04:00
|
|
|
LocalFileWriter.deleteFile originalFsPath, ->
|
2016-12-13 07:43:57 -05:00
|
|
|
callback(err, destPath, originalFsPath)
|
2014-03-04 08:36:47 -05:00
|
|
|
|
2014-02-14 11:39:05 -05:00
|
|
|
if opts.format?
|
2015-07-24 03:06:52 -04:00
|
|
|
FileConverter.convert originalFsPath, opts.format, done
|
2014-02-14 11:39:05 -05:00
|
|
|
else if opts.style == "thumbnail"
|
2015-07-24 03:06:52 -04:00
|
|
|
FileConverter.thumbnail originalFsPath, done
|
2014-02-14 11:39:05 -05:00
|
|
|
else if opts.style == "preview"
|
2015-07-24 03:06:52 -04:00
|
|
|
FileConverter.preview originalFsPath, done
|
2014-02-25 11:38:13 -05:00
|
|
|
else
|
2015-09-10 06:10:30 -04:00
|
|
|
return callback(new Error("should have specified opts to convert file with #{JSON.stringify(opts)}"))
|
2014-02-14 11:39:05 -05:00
|
|
|
|
|
|
|
|
2015-09-08 04:23:08 -04:00
|
|
|
_writeS3FileToDisk: (bucket, key, opts, callback)->
|
|
|
|
PersistorManager.getFileStream bucket, key, opts, (err, fileStream)->
|
2015-09-10 04:12:49 -04:00
|
|
|
if err?
|
|
|
|
return callback(err)
|
2014-02-14 11:39:05 -05:00
|
|
|
LocalFileWriter.writeStream fileStream, key, callback
|
2016-03-13 15:22:14 -04:00
|
|
|
|
|
|
|
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
|
2018-07-04 11:41:31 -04:00
|
|
|
|
|
|
|
_scrubSecrets: (opts)->
|
|
|
|
safe = Object.assign {}, opts
|
|
|
|
delete safe.credentials
|
|
|
|
safe
|