2014-02-19 18:39:25 +00:00
|
|
|
http = require('http')
|
|
|
|
http.globalAgent.maxSockets = 300
|
2014-02-19 21:03:03 +00:00
|
|
|
https = require('https')
|
|
|
|
https.globalAgent.maxSockets = 300
|
2014-02-14 16:39:05 +00:00
|
|
|
settings = require("settings-sharelatex")
|
|
|
|
request = require("request")
|
|
|
|
logger = require("logger-sharelatex")
|
|
|
|
fs = require("fs")
|
|
|
|
knox = require("knox")
|
|
|
|
path = require("path")
|
|
|
|
LocalFileWriter = require("./LocalFileWriter")
|
|
|
|
_ = require("underscore")
|
|
|
|
|
|
|
|
thirtySeconds = 30 * 1000
|
|
|
|
|
|
|
|
buildDefaultOptions = (bucketName, method, key)->
|
|
|
|
return {
|
|
|
|
aws:
|
2014-03-04 15:01:13 +00:00
|
|
|
key: settings.filestore.s3.key
|
|
|
|
secret: settings.filestore.s3.secret
|
2014-02-14 16:39:05 +00:00
|
|
|
bucket: bucketName
|
|
|
|
method: method
|
|
|
|
timeout: thirtySeconds
|
|
|
|
uri:"https://#{bucketName}.s3.amazonaws.com/#{key}"
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports =
|
|
|
|
|
2014-02-25 16:38:13 +00:00
|
|
|
sendFile: (bucketName, key, fsPath, callback)->
|
2014-02-14 16:39:05 +00:00
|
|
|
s3Client = knox.createClient
|
2014-03-04 15:01:13 +00:00
|
|
|
key: settings.filestore.s3.key
|
|
|
|
secret: settings.filestore.s3.secret
|
2014-02-14 16:39:05 +00:00
|
|
|
bucket: bucketName
|
|
|
|
putEventEmiter = s3Client.putFile fsPath, key, (err, res)->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, bucketName:bucketName, key:key, fsPath:fsPath,"something went wrong uploading file to s3"
|
|
|
|
return callback(err)
|
2014-02-17 16:31:33 +00:00
|
|
|
if !res?
|
|
|
|
logger.err err:err, res:res, bucketName:bucketName, key:key, fsPath:fsPath, "no response from s3 put file"
|
2014-03-04 13:36:47 +00:00
|
|
|
return callback("no response from put file")
|
2014-02-14 16:39:05 +00:00
|
|
|
if res.statusCode != 200
|
|
|
|
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")
|
2014-06-02 16:25:40 +00:00
|
|
|
LocalFileWriter.deleteFile fsPath, (err)->
|
|
|
|
logger.log res:res, bucketName:bucketName, key:key, fsPath:fsPath,"file uploaded to s3"
|
|
|
|
callback(err)
|
2014-02-14 16:39:05 +00:00
|
|
|
putEventEmiter.on "error", (err)->
|
|
|
|
logger.err err:err, bucketName:bucketName, key:key, fsPath:fsPath, "error emmited on put of file"
|
|
|
|
callback err
|
|
|
|
|
2014-02-25 16:38:13 +00:00
|
|
|
sendStream: (bucketName, key, readStream, callback)->
|
2014-02-14 16:39:05 +00:00
|
|
|
logger.log bucketName:bucketName, key:key, "sending file to s3"
|
|
|
|
readStream.on "error", (err)->
|
|
|
|
logger.err bucketName:bucketName, key:key, "error on stream to send to s3"
|
|
|
|
LocalFileWriter.writeStream readStream, null, (err, fsPath)=>
|
|
|
|
if err?
|
|
|
|
logger.err bucketName:bucketName, key:key, fsPath:fsPath, err:err, "something went wrong writing stream to disk"
|
|
|
|
return callback(err)
|
2014-02-25 16:38:13 +00:00
|
|
|
@sendFile bucketName, key, fsPath, callback
|
2014-02-14 16:39:05 +00:00
|
|
|
|
2014-05-23 12:57:18 +00:00
|
|
|
getFileStream: (bucketName, key, callback = (err, res)->)->
|
2014-12-19 16:28:42 +00:00
|
|
|
callback = _.once callback
|
2014-02-14 16:39:05 +00:00
|
|
|
logger.log bucketName:bucketName, key:key, "getting file from s3"
|
2014-02-19 20:56:45 +00:00
|
|
|
s3Client = knox.createClient
|
2014-03-04 15:01:13 +00:00
|
|
|
key: settings.filestore.s3.key
|
|
|
|
secret: settings.filestore.s3.secret
|
2014-02-19 20:56:45 +00:00
|
|
|
bucket: bucketName
|
|
|
|
s3Stream = s3Client.get(key)
|
|
|
|
s3Stream.end()
|
|
|
|
s3Stream.on 'response', (res) ->
|
2014-05-23 12:57:18 +00:00
|
|
|
callback null, res
|
2014-02-19 20:56:45 +00:00
|
|
|
s3Stream.on 'error', (err) ->
|
2014-02-17 16:31:33 +00:00
|
|
|
logger.err err:err, bucketName:bucketName, key:key, "error getting file stream from s3"
|
2014-02-19 20:56:45 +00:00
|
|
|
callback err
|
2014-02-14 16:39:05 +00:00
|
|
|
|
|
|
|
copyFile: (bucketName, sourceKey, destKey, callback)->
|
|
|
|
logger.log bucketName:bucketName, sourceKey:sourceKey, destKey:destKey, "copying file in s3"
|
|
|
|
s3Client = knox.createClient
|
2014-03-04 15:01:13 +00:00
|
|
|
key: settings.filestore.s3.key
|
|
|
|
secret: settings.filestore.s3.secret
|
2014-02-14 16:39:05 +00:00
|
|
|
bucket: bucketName
|
|
|
|
s3Client.copyFile sourceKey, destKey, (err)->
|
|
|
|
if err?
|
2014-02-17 16:31:33 +00:00
|
|
|
logger.err err:err, bucketName:bucketName, sourceKey:sourceKey, destKey:destKey, "something went wrong copying file in aws"
|
2014-02-14 16:39:05 +00:00
|
|
|
callback(err)
|
|
|
|
|
|
|
|
deleteFile: (bucketName, key, callback)->
|
|
|
|
logger.log bucketName:bucketName, key:key, "delete file in s3"
|
|
|
|
options = buildDefaultOptions(bucketName, "delete", key)
|
|
|
|
request options, (err, res)->
|
|
|
|
if err?
|
2014-02-17 16:31:33 +00:00
|
|
|
logger.err err:err, res:res, bucketName:bucketName, key:key, "something went wrong deleting file in aws"
|
2014-02-14 16:39:05 +00:00
|
|
|
callback(err)
|
|
|
|
|
2014-06-02 15:49:49 +00:00
|
|
|
deleteDirectory: (bucketName, key, _callback)->
|
2014-06-02 16:25:40 +00:00
|
|
|
# deleteMultiple can call the callback multiple times so protect against this.
|
2014-06-02 15:49:49 +00:00
|
|
|
callback = (args...) ->
|
|
|
|
_callback(args...)
|
2014-06-02 16:25:40 +00:00
|
|
|
_callback = () ->
|
2014-06-02 15:49:49 +00:00
|
|
|
|
|
|
|
logger.log key: key, bucketName: bucketName, "deleting directory"
|
2014-02-14 16:39:05 +00:00
|
|
|
s3Client = knox.createClient
|
2014-03-04 15:01:13 +00:00
|
|
|
key: settings.filestore.s3.key
|
|
|
|
secret: settings.filestore.s3.secret
|
2014-02-14 16:39:05 +00:00
|
|
|
bucket: bucketName
|
|
|
|
s3Client.list prefix:key, (err, data)->
|
2015-03-31 09:35:33 +00:00
|
|
|
if err?
|
|
|
|
logger.err err:err, bucketName:bucketName, key:key, "something went wrong listing prefix in aws"
|
|
|
|
return callback(err)
|
2014-02-14 16:39:05 +00:00
|
|
|
keys = _.map data.Contents, (entry)->
|
|
|
|
return entry.Key
|
|
|
|
s3Client.deleteMultiple keys, callback
|
|
|
|
|
|
|
|
checkIfFileExists:(bucketName, key, callback)->
|
|
|
|
logger.log bucketName:bucketName, key:key, "checking if file exists in s3"
|
|
|
|
options = buildDefaultOptions(bucketName, "head", key)
|
|
|
|
request options, (err, res)->
|
|
|
|
if err?
|
2014-02-19 15:24:29 +00:00
|
|
|
logger.err err:err, res:res, bucketName:bucketName, key:key, "something went wrong checking file in aws"
|
2014-02-19 15:26:00 +00:00
|
|
|
return callback(err)
|
2014-02-18 17:43:21 +00:00
|
|
|
if !res?
|
|
|
|
logger.err err:err, res:res, bucketName:bucketName, key:key, "no response object returned when checking if file exists"
|
2014-02-19 14:58:36 +00:00
|
|
|
err = new Error("no response from s3 #{bucketName} #{key}")
|
|
|
|
return callback(err)
|
2014-02-14 16:39:05 +00:00
|
|
|
exists = res.statusCode == 200
|
|
|
|
logger.log bucketName:bucketName, key:key, exists:exists, "checked if file exsists in s3"
|
|
|
|
callback(err, exists)
|
|
|
|
|