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-02-26 04:15:37 -05:00
|
|
|
PersistorManager.deleteDirectory bucket, convetedKey, ->
|
2014-02-25 11:38:13 -05:00
|
|
|
PersistorManager.sendStream bucket, key, stream, ->
|
2014-02-14 11:39:05 -05:00
|
|
|
callback()
|
|
|
|
|
|
|
|
deleteFile: (bucket, key, callback)->
|
|
|
|
convetedKey = KeyBuilder.getConvertedFolderKey(bucket, key)
|
|
|
|
async.parallel [
|
2014-02-26 04:15:37 -05:00
|
|
|
(done)-> PersistorManager.deleteFile bucket, key, done
|
|
|
|
(done)-> PersistorManager.deleteFile bucket, convetedKey, done
|
2014-02-14 11:39:05 -05:00
|
|
|
], callback
|
|
|
|
|
|
|
|
getFile: (bucket, key, opts = {}, callback)->
|
|
|
|
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)->
|
2014-02-26 04:15:37 -05:00
|
|
|
PersistorManager.getFileStream bucket, key, (err, fileStream)->
|
2014-02-14 11:39:05 -05:00
|
|
|
if err?
|
|
|
|
logger.err bucket:bucket, key:key, opts:opts, "error getting fileStream"
|
|
|
|
callback err, fileStream
|
|
|
|
|
|
|
|
_getConvertedFile: (bucket, key, opts, callback)->
|
|
|
|
convetedKey = KeyBuilder.addCachingToKey(key, opts)
|
2014-02-26 04:15:37 -05:00
|
|
|
PersistorManager.checkIfFileExists bucket, convetedKey, (err, exists)=>
|
2014-02-14 11:39:05 -05:00
|
|
|
if exists
|
2014-02-26 04:15:37 -05:00
|
|
|
PersistorManager.getFileStream bucket, convetedKey, callback
|
2014-02-14 11:39:05 -05:00
|
|
|
else
|
|
|
|
@_getConvertedFileAndCache bucket, key, convetedKey, opts, callback
|
|
|
|
|
|
|
|
_getConvertedFileAndCache: (bucket, key, convetedKey, opts, callback)->
|
|
|
|
@_convertFile bucket, key, opts, (err, fsPath)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, fsPath:fsPath, bucket:bucket, key:key, opts:opts, "something went wrong with converting file"
|
|
|
|
return callback(err)
|
|
|
|
ImageOptimiser.compressPng fsPath, (err)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, fsPath:fsPath, bucket:bucket, key:key, opts:opts, "something went wrong optimising png file"
|
|
|
|
return callback(err)
|
2014-02-25 11:38:13 -05:00
|
|
|
PersistorManager.sendFile bucket, convetedKey, fsPath, (err)->
|
2014-02-14 11:39:05 -05:00
|
|
|
if err?
|
2014-02-25 04:10:22 -05:00
|
|
|
logger.err err:err, bucket:bucket, key:key, convetedKey:convetedKey, opts:opts, "something went wrong sending the file"
|
2014-02-14 11:39:05 -05:00
|
|
|
return callback(err)
|
2014-02-26 04:15:37 -05:00
|
|
|
PersistorManager.getFileStream bucket, convetedKey, callback
|
2014-02-14 11:39:05 -05:00
|
|
|
|
2014-02-25 04:10:22 -05:00
|
|
|
_convertFile: (bucket, origonalKey, opts, callback)->
|
2014-02-14 11:39:05 -05:00
|
|
|
@_writeS3FileToDisk bucket, origonalKey, (err, origonalFsPath)->
|
|
|
|
if opts.format?
|
|
|
|
FileConverter.convert origonalFsPath, opts.format, callback
|
|
|
|
else if opts.style == "thumbnail"
|
|
|
|
FileConverter.thumbnail origonalFsPath, callback
|
|
|
|
else if opts.style == "preview"
|
|
|
|
FileConverter.preview origonalFsPath, callback
|
2014-02-25 11:38:13 -05:00
|
|
|
else
|
2014-02-14 11:39:05 -05:00
|
|
|
throw new Error("should have specified opts to convert file with #{JSON.stringify(opts)}")
|
|
|
|
|
|
|
|
|
|
|
|
_writeS3FileToDisk: (bucket, key, callback)->
|
2014-02-26 04:15:37 -05:00
|
|
|
PersistorManager.getFileStream bucket, key, (err, fileStream)->
|
2014-02-14 11:39:05 -05:00
|
|
|
LocalFileWriter.writeStream fileStream, key, callback
|
|
|
|
|