2015-07-22 14:42:45 +00:00
|
|
|
logger = require "logger-sharelatex"
|
|
|
|
aws = require "aws-sdk"
|
|
|
|
_ = require "underscore"
|
|
|
|
fs = require "fs"
|
2015-11-20 13:38:23 +00:00
|
|
|
Errors = require "./Errors"
|
2015-07-22 14:42:45 +00:00
|
|
|
|
2015-07-24 13:48:46 +00:00
|
|
|
s3 = new aws.S3()
|
2015-07-22 14:42:45 +00:00
|
|
|
|
|
|
|
module.exports =
|
2015-11-20 13:38:23 +00:00
|
|
|
sendFile: (bucketName, key, fsPath, callback)->
|
|
|
|
logger.log bucketName:bucketName, key:key, "send file data to s3"
|
|
|
|
stream = fs.createReadStream fsPath
|
|
|
|
s3.upload Bucket: bucketName, Key: key, Body: stream, (err, data) ->
|
|
|
|
if err?
|
|
|
|
logger.err err: err, Bucket: bucketName, Key: key, "error sending file data to s3"
|
|
|
|
callback err
|
2015-07-22 14:42:45 +00:00
|
|
|
|
2015-11-20 13:38:23 +00:00
|
|
|
sendStream: (bucketName, key, stream, callback)->
|
|
|
|
logger.log bucketName:bucketName, key:key, "send file stream to s3"
|
|
|
|
s3.upload Bucket: bucketName, Key: key, Body: stream, (err, data) ->
|
|
|
|
if err?
|
|
|
|
logger.err err: err, Bucket: bucketName, Key: key, "error sending file stream to s3"
|
|
|
|
callback err
|
2015-07-22 14:42:45 +00:00
|
|
|
|
2015-11-20 13:38:23 +00:00
|
|
|
getFileStream: (bucketName, key, opts, callback = (err, res)->)->
|
|
|
|
logger.log bucketName:bucketName, key:key, "get file stream from s3"
|
|
|
|
callback = _.once callback
|
|
|
|
params =
|
|
|
|
Bucket:bucketName
|
|
|
|
Key: key
|
|
|
|
if opts.start? and opts.end?
|
|
|
|
params['Range'] = "bytes=#{opts.start}-#{opts.end}"
|
|
|
|
request = s3.getObject params
|
|
|
|
stream = request.createReadStream()
|
|
|
|
stream.on 'readable', () ->
|
|
|
|
callback null, stream
|
|
|
|
stream.on 'error', (err) ->
|
|
|
|
logger.err err:err, bucketName:bucketName, key:key, "error getting file stream from s3"
|
|
|
|
if err.code == 'NoSuchKey'
|
|
|
|
return callback new Errors.NotFoundError "File not found in S3: #{bucketName}:#{key}"
|
|
|
|
callback err
|
2015-07-22 14:42:45 +00:00
|
|
|
|
2015-11-20 13:38:23 +00:00
|
|
|
copyFile: (bucketName, sourceKey, destKey, callback)->
|
|
|
|
logger.log bucketName:bucketName, sourceKey:sourceKey, destKey: destKey, "copying file in s3"
|
|
|
|
source = bucketName + '/' + sourceKey
|
|
|
|
s3.copyObject {Bucket: bucketName, Key: destKey, CopySource: source}, (err) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, bucketName:bucketName, sourceKey:sourceKey, destKey:destKey, "something went wrong copying file in s3"
|
|
|
|
callback err
|
2015-07-22 14:42:45 +00:00
|
|
|
|
2015-11-20 13:38:23 +00:00
|
|
|
deleteFile: (bucketName, key, callback)->
|
|
|
|
logger.log bucketName:bucketName, key:key, "delete file in s3"
|
|
|
|
s3.deleteObject {Bucket: bucketName, Key: key}, (err) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, bucketName:bucketName, key:key, "something went wrong deleting file in s3"
|
|
|
|
callback err
|
2015-07-22 14:42:45 +00:00
|
|
|
|
2015-11-20 13:38:23 +00:00
|
|
|
deleteDirectory: (bucketName, key, callback)->
|
|
|
|
logger.log bucketName:bucketName, key:key, "delete directory in s3"
|
|
|
|
s3.listObjects {Bucket: bucketName, Prefix: key}, (err, data) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, bucketName:bucketName, key:key, "something went wrong listing prefix in s3"
|
|
|
|
return callback err
|
|
|
|
if data.Contents.length == 0
|
|
|
|
logger.log bucketName:bucketName, key:key, "the directory is empty"
|
|
|
|
return callback()
|
|
|
|
keys = _.map data.Contents, (entry)->
|
|
|
|
Key: entry.Key
|
|
|
|
s3.deleteObjects
|
|
|
|
Bucket: bucketName
|
|
|
|
Delete:
|
|
|
|
Objects: keys
|
|
|
|
Quiet: true
|
|
|
|
, (err) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, bucketName:bucketName, key:keys, "something went wrong deleting directory in s3"
|
|
|
|
callback err
|
2015-07-22 14:42:45 +00:00
|
|
|
|
2015-11-20 13:38:23 +00:00
|
|
|
checkIfFileExists:(bucketName, key, callback)->
|
|
|
|
logger.log bucketName:bucketName, key:key, "check file existence in s3"
|
|
|
|
s3.headObject {Bucket: bucketName, Key: key}, (err, data) ->
|
|
|
|
if err?
|
2015-11-25 14:40:35 +00:00
|
|
|
return (callback null, false) if err.code == 'NotFound'
|
2015-11-20 13:38:23 +00:00
|
|
|
logger.err err:err, bucketName:bucketName, key:key, "something went wrong checking head in s3"
|
|
|
|
return callback err
|
|
|
|
callback null, data.ETag?
|
2015-07-22 14:42:45 +00:00
|
|
|
|
2016-03-13 19:22:14 +00:00
|
|
|
directorySize:(bucketName, key, callback)->
|
2016-03-12 07:35:49 +00:00
|
|
|
logger.log bucketName:bucketName, key:key, "get project size in s3"
|
2016-03-12 14:08:07 +00:00
|
|
|
s3.listObjects {Bucket: bucketName, Prefix: key}, (err, data) ->
|
|
|
|
if err?
|
|
|
|
logger.err err:err, bucketName:bucketName, key:key, "something went wrong listing prefix in s3"
|
|
|
|
return callback err
|
|
|
|
if data.Contents.length == 0
|
|
|
|
logger.log bucketName:bucketName, key:key, "the directory is empty"
|
|
|
|
return callback()
|
|
|
|
totalSize = 0
|
|
|
|
_.each data.Contents, (entry)->
|
|
|
|
totalSize += entry.Size
|
|
|
|
callback null, totalSize
|
2016-03-12 07:35:49 +00:00
|
|
|
|