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)->
|
|
|
|
convetedKey = KeyBuilder.getConvertedFolderKey(key)
|
2014-06-02 12:11:25 -04:00
|
|
|
PersistorManager.deleteDirectory bucket, convetedKey, (error) ->
|
|
|
|
return callback(error) if error?
|
|
|
|
PersistorManager.sendStream bucket, key, stream, callback
|
2014-02-14 11:39:05 -05:00
|
|
|
|
|
|
|
deleteFile: (bucket, key, callback)->
|
2014-12-19 11:57:44 -05:00
|
|
|
convetedKey = 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
|
2014-12-19 11:57:44 -05:00
|
|
|
(done)-> PersistorManager.deleteDirectory bucket, convetedKey, 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)->
|
2014-02-14 11:39:05 -05:00
|
|
|
convetedKey = KeyBuilder.addCachingToKey(key, opts)
|
2014-02-26 04:15:37 -05:00
|
|
|
PersistorManager.checkIfFileExists bucket, convetedKey, (err, exists)=>
|
2015-09-10 06:10:30 -04:00
|
|
|
if err?
|
|
|
|
return callback(err)
|
2014-02-14 11:39:05 -05:00
|
|
|
if exists
|
2015-09-08 04:23:08 -04:00
|
|
|
PersistorManager.getFileStream bucket, convetedKey, opts, callback
|
2014-02-14 11:39:05 -05:00
|
|
|
else
|
|
|
|
@_getConvertedFileAndCache bucket, key, convetedKey, opts, callback
|
|
|
|
|
|
|
|
_getConvertedFileAndCache: (bucket, key, convetedKey, opts, callback)->
|
2014-03-04 08:36:47 -05:00
|
|
|
self = @
|
|
|
|
convertedFsPath = ""
|
|
|
|
async.series [
|
|
|
|
(cb)->
|
|
|
|
self._convertFile bucket, key, opts, (err, fileSystemPath)->
|
|
|
|
convertedFsPath = fileSystemPath
|
|
|
|
cb err
|
|
|
|
(cb)->
|
|
|
|
ImageOptimiser.compressPng convertedFsPath, cb
|
|
|
|
(cb)->
|
|
|
|
PersistorManager.sendFile bucket, convetedKey, convertedFsPath, cb
|
|
|
|
], (err)->
|
2014-02-14 11:39:05 -05:00
|
|
|
if err?
|
|
|
|
return callback(err)
|
2015-09-08 04:23:08 -04:00
|
|
|
PersistorManager.getFileStream bucket, convetedKey, opts, callback
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2014-02-25 04:10:22 -05:00
|
|
|
_convertFile: (bucket, origonalKey, opts, callback)->
|
2015-09-08 04:23:08 -04:00
|
|
|
@_writeS3FileToDisk bucket, origonalKey, opts, (err, origonalFsPath)->
|
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?
|
|
|
|
logger.err err:err, bucket:bucket, origonalKey:origonalKey, opts:opts, "error converting file"
|
|
|
|
return callback(err)
|
|
|
|
LocalFileWriter.deleteFile origonalFsPath, ->
|
|
|
|
callback(err, destPath)
|
|
|
|
|
2014-02-14 11:39:05 -05:00
|
|
|
if opts.format?
|
2014-03-04 08:36:47 -05:00
|
|
|
FileConverter.convert origonalFsPath, opts.format, done
|
2014-02-14 11:39:05 -05:00
|
|
|
else if opts.style == "thumbnail"
|
2014-03-04 08:36:47 -05:00
|
|
|
FileConverter.thumbnail origonalFsPath, done
|
2014-02-14 11:39:05 -05:00
|
|
|
else if opts.style == "preview"
|
2014-03-04 08:36:47 -05:00
|
|
|
FileConverter.preview origonalFsPath, 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
|