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")
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
|
|
|
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)->
|
2014-02-14 11:39:05 -05: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
|
|
|
|
|
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)->
|
2014-02-14 11:39:05 -05:00
|
|
|
if err?
|
|
|
|
logger.err bucket:bucket, key:key, opts: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)
|
2015-11-20 06:02:22 -05:00
|
|
|
PersistorManager.getFileStream bucket, convertedKey, opts, callback
|
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?
|
2015-07-24 03:06:52 -04:00
|
|
|
logger.err err:err, bucket:bucket, originalKey:originalKey, opts: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
|