delete converted file when finished to clean up

This commit is contained in:
Henry Oswald 2014-03-04 13:36:47 +00:00
parent 94a6504635
commit 711e95a82a
6 changed files with 34 additions and 20 deletions

View file

@ -2,7 +2,6 @@ PersistorManager = require("./PersistorManager")
settings = require("settings-sharelatex") settings = require("settings-sharelatex")
logger = require("logger-sharelatex") logger = require("logger-sharelatex")
FileHandler = require("./FileHandler") FileHandler = require("./FileHandler")
LocalFileWriter = require("./LocalFileWriter")
metrics = require("./metrics") metrics = require("./metrics")
oneDayInSeconds = 60 * 60 * 24 oneDayInSeconds = 60 * 60 * 24

View file

@ -7,7 +7,6 @@ KeyBuilder = require("./KeyBuilder")
async = require("async") async = require("async")
ImageOptimiser = require("./ImageOptimiser") ImageOptimiser = require("./ImageOptimiser")
module.exports = module.exports =
insertFile: (bucket, key, stream, callback)-> insertFile: (bucket, key, stream, callback)->
@ -45,28 +44,37 @@ module.exports =
@_getConvertedFileAndCache bucket, key, convetedKey, opts, callback @_getConvertedFileAndCache bucket, key, convetedKey, opts, callback
_getConvertedFileAndCache: (bucket, key, convetedKey, opts, callback)-> _getConvertedFileAndCache: (bucket, key, convetedKey, opts, callback)->
@_convertFile bucket, key, opts, (err, fsPath)-> 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)->
if err? 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)
PersistorManager.sendFile bucket, convetedKey, fsPath, (err)->
if err?
logger.err err:err, bucket:bucket, key:key, convetedKey:convetedKey, opts:opts, "something went wrong sending the file"
return callback(err) return callback(err)
PersistorManager.getFileStream bucket, convetedKey, callback PersistorManager.getFileStream bucket, convetedKey, callback
_convertFile: (bucket, origonalKey, opts, callback)-> _convertFile: (bucket, origonalKey, opts, callback)->
@_writeS3FileToDisk bucket, origonalKey, (err, origonalFsPath)-> @_writeS3FileToDisk bucket, origonalKey, (err, origonalFsPath)->
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)
if opts.format? if opts.format?
FileConverter.convert origonalFsPath, opts.format, callback FileConverter.convert origonalFsPath, opts.format, done
else if opts.style == "thumbnail" else if opts.style == "thumbnail"
FileConverter.thumbnail origonalFsPath, callback FileConverter.thumbnail origonalFsPath, done
else if opts.style == "preview" else if opts.style == "preview"
FileConverter.preview origonalFsPath, callback FileConverter.preview origonalFsPath, done
else else
throw new Error("should have specified opts to convert file with #{JSON.stringify(opts)}") throw new Error("should have specified opts to convert file with #{JSON.stringify(opts)}")

View file

@ -1,7 +1,6 @@
exec = require('child_process').exec exec = require('child_process').exec
logger = require("logger-sharelatex") logger = require("logger-sharelatex")
module.exports = module.exports =
compressPng: (localPath, callback)-> compressPng: (localPath, callback)->

View file

@ -26,6 +26,7 @@ module.exports =
callback err callback err
deleteFile: (fsPath, callback)-> deleteFile: (fsPath, callback)->
logger.log fsPath:fsPath, "removing local temp file"
fs.unlink fsPath, callback fs.unlink fsPath, callback
_getPath : (key)-> _getPath : (key)->

View file

@ -45,7 +45,7 @@ module.exports =
return callback(err) return callback(err)
if !res? if !res?
logger.err err:err, res:res, bucketName:bucketName, key:key, fsPath:fsPath, "no response from s3 put file" logger.err err:err, res:res, bucketName:bucketName, key:key, fsPath:fsPath, "no response from s3 put file"
callback("no response from put file") return callback("no response from put file")
if res.statusCode != 200 if res.statusCode != 200
logger.err bucketName:bucketName, key:key, fsPath:fsPath, "non 200 response from s3 putting file" logger.err bucketName:bucketName, key:key, fsPath:fsPath, "non 200 response from s3 putting file"
return callback("non 200 response from s3 on put file") return callback("non 200 response from s3 on put file")

View file

@ -22,6 +22,7 @@ describe "FileHandler", ->
insertFile: sinon.stub() insertFile: sinon.stub()
@LocalFileWriter = @LocalFileWriter =
writeStream: sinon.stub() writeStream: sinon.stub()
deleteFile: sinon.stub()
@FileConverter = @FileConverter =
convert: sinon.stub() convert: sinon.stub()
thumbnail: sinon.stub() thumbnail: sinon.stub()
@ -134,16 +135,18 @@ describe "FileHandler", ->
describe "_getConvertedFileAndCache", -> describe "_getConvertedFileAndCache", ->
it "should _convertFile ", (done)-> it "should _convertFile ", (done)->
@stubbedStream = {"something":"here"}
@PersistorManager.sendFile = sinon.stub().callsArgWith(3) @PersistorManager.sendFile = sinon.stub().callsArgWith(3)
@PersistorManager.getFileStream = sinon.stub().callsArgWith(2) @PersistorManager.getFileStream = sinon.stub().callsArgWith(2, null, @stubbedStream)
@convetedKey = @key+"converted" @convetedKey = @key+"converted"
@handler._convertFile = sinon.stub().callsArgWith(3, null, @stubbedPath) @handler._convertFile = sinon.stub().callsArgWith(3, null, @stubbedPath)
@ImageOptimiser.compressPng = sinon.stub().callsArgWith(1) @ImageOptimiser.compressPng = sinon.stub().callsArgWith(1)
@handler._getConvertedFileAndCache @bucket, @key, @convetedKey, {}, => @handler._getConvertedFileAndCache @bucket, @key, @convetedKey, {}, (err, fsStream)=>
@handler._convertFile.called.should.equal true @handler._convertFile.called.should.equal true
@PersistorManager.sendFile.calledWith(@bucket, @convetedKey, @stubbedPath).should.equal true @PersistorManager.sendFile.calledWith(@bucket, @convetedKey, @stubbedPath).should.equal true
@PersistorManager.getFileStream.calledWith(@bucket, @convetedKey).should.equal true @PersistorManager.getFileStream.calledWith(@bucket, @convetedKey).should.equal true
@ImageOptimiser.compressPng.calledWith(@stubbedPath).should.equal true @ImageOptimiser.compressPng.calledWith(@stubbedPath).should.equal true
fsStream.should.equal @stubbedStream
done() done()
describe "_convertFile", -> describe "_convertFile", ->
@ -152,23 +155,27 @@ describe "FileHandler", ->
@FileConverter.thumbnail.callsArgWith(1, null, @formattedStubbedPath) @FileConverter.thumbnail.callsArgWith(1, null, @formattedStubbedPath)
@FileConverter.preview.callsArgWith(1, null, @formattedStubbedPath) @FileConverter.preview.callsArgWith(1, null, @formattedStubbedPath)
@handler._writeS3FileToDisk = sinon.stub().callsArgWith(2, null, @stubbedPath) @handler._writeS3FileToDisk = sinon.stub().callsArgWith(2, null, @stubbedPath)
@LocalFileWriter.deleteFile.callsArgWith(1)
it "should call thumbnail on the writer path if style was thumbnail was specified", (done)-> it "should call thumbnail on the writer path if style was thumbnail was specified", (done)->
@handler._convertFile @bucket, @key, style:"thumbnail", (err, path)=> @handler._convertFile @bucket, @key, style:"thumbnail", (err, path)=>
path.should.equal @formattedStubbedPath path.should.equal @formattedStubbedPath
@FileConverter.thumbnail.calledWith(@stubbedPath).should.equal true @FileConverter.thumbnail.calledWith(@stubbedPath).should.equal true
@LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true
done() done()
it "should call preview on the writer path if style was preview was specified", (done)-> it "should call preview on the writer path if style was preview was specified", (done)->
@handler._convertFile @bucket, @key, style:"preview", (err, path)=> @handler._convertFile @bucket, @key, style:"preview", (err, path)=>
path.should.equal @formattedStubbedPath path.should.equal @formattedStubbedPath
@FileConverter.preview.calledWith(@stubbedPath).should.equal true @FileConverter.preview.calledWith(@stubbedPath).should.equal true
@LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true
done() done()
it "should call convert on the writer path if a format was specified", (done)-> it "should call convert on the writer path if a format was specified", (done)->
@handler._convertFile @bucket, @key, format:@format, (err, path)=> @handler._convertFile @bucket, @key, format:@format, (err, path)=>
path.should.equal @formattedStubbedPath path.should.equal @formattedStubbedPath
@FileConverter.convert.calledWith(@stubbedPath, @format).should.equal true @FileConverter.convert.calledWith(@stubbedPath, @format).should.equal true
@LocalFileWriter.deleteFile.calledWith(@stubbedPath).should.equal true
done() done()