-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from lpetre/plugin_restructure
Large restructure of s3-index plugin
- Loading branch information
Showing
2 changed files
with
114 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,107 @@ | ||
var AWS = require('aws-sdk'); | ||
var CoreObject = require('core-object'); | ||
var Promise = require('ember-cli/lib/ext/promise'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var readFile = Promise.denodeify(fs.readFile); | ||
|
||
var headObject = function(client, params) { | ||
return new Promise(function(resolve, reject) { | ||
client.headObject(params, function(err, data) { | ||
if (err && err.code === 'NotFound') { | ||
return resolve(); | ||
} | ||
else if (err) { | ||
return reject(err); | ||
} | ||
else { | ||
return resolve(data); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
module.exports = CoreObject.extend({ | ||
init: function(options) { | ||
var plugin = options.plugin; | ||
var config = plugin.pluginConfig; | ||
var readConfig = plugin.readConfig; | ||
|
||
this._client = plugin.readConfig('s3Client') || new AWS.S3(config); | ||
this._bucket = plugin.readConfig('bucket'); | ||
this._keyPrefix = plugin.readConfig('keyPrefix'); | ||
this._filePattern = plugin.readConfig('filePattern'); | ||
this._currentRevisionIdentifier = plugin.readConfig('currentRevisionIdentifier'); | ||
this._plugin = plugin; | ||
this._client = plugin.readConfig('s3Client') || new AWS.S3(config); | ||
}, | ||
|
||
fetchRevisions: function() { | ||
return Promise.hash({ | ||
revisions: this._getBucketContents(), | ||
current: this._getCurrentData() | ||
}) | ||
.then(this._createRevisionDataFromList.bind(this)); | ||
}, | ||
|
||
upload: function(key, fileContent) { | ||
var client = this._client; | ||
upload: function(options) { | ||
var client = this._client; | ||
var plugin = this._plugin; | ||
var bucket = options.bucket; | ||
var acl = options.acl || 'public-read'; | ||
var key = path.join(options.prefix, options.filePattern + ":" + options.revisionKey); | ||
var putObject = Promise.denodeify(client.putObject.bind(client)); | ||
|
||
var params = { | ||
Bucket: this._bucket, | ||
Bucket: bucket, | ||
Key: key, | ||
Body: fileContent, | ||
ACL: acl, | ||
ContentType: 'text/html', | ||
CacheControl: 'max-age=0, no-cache' | ||
} | ||
|
||
return new Promise(function(resolve, reject) { | ||
client.putObject(params, function(err, data) { | ||
if (err) { | ||
return reject(err); | ||
} else { | ||
return resolve(data); | ||
} | ||
}); | ||
return readFile(options.filePath).then(function(fileContents) { | ||
params.Body = fileContents; | ||
return putObject(params).then(function() { | ||
plugin.log('✔ ' + key); | ||
}) | ||
}); | ||
}, | ||
|
||
overwriteCurrentIndex: function(newRevisionKey) { | ||
var client = this._client; | ||
var bucket = this._bucket; | ||
var copySource = encodeURIComponent(bucket+'/'+newRevisionKey); | ||
activate: function(options) { | ||
var plugin = this._plugin; | ||
var client = this._client; | ||
var bucket = options.bucket; | ||
var acl = options.acl || 'public-read'; | ||
var revisionKey = path.join(options.prefix, options.filePattern + ":" + options.revisionKey); | ||
var indexKey = path.join(options.prefix, options.filePattern); | ||
var copySource = encodeURIComponent(path.join(bucket, revisionKey)); | ||
var copyObject = Promise.denodeify(client.copyObject.bind(client)); | ||
|
||
var params = { | ||
Bucket: bucket, | ||
CopySource: copySource, | ||
Key: this._filePattern | ||
Key: indexKey, | ||
ACL: acl, | ||
}; | ||
|
||
return new Promise(function(resolve, reject) { | ||
client.copyObject(params, function(err, data) { | ||
if (err) { | ||
return reject(err); | ||
} else { | ||
return resolve(data); | ||
} | ||
}); | ||
}); | ||
}, | ||
|
||
_createRevisionDataFromList: function(data) { | ||
const revisionsData = this._sortBucketContents(data.revisions).Contents; | ||
const currentRevision = data.current; | ||
|
||
return revisionsData.map(function(d) { | ||
var revision = d.Key; | ||
|
||
return { revision: revision, active: revision === currentRevision }; | ||
return this.fetchRevisions(options).then(function(revisions) { | ||
var found = revisions.map(function(element) { return element.revision; }).indexOf(options.revisionKey); | ||
if (found >= 0) { | ||
return copyObject(params).then(function() { | ||
plugin.log('✔ ' + revisionKey + " => " + indexKey); | ||
}) | ||
} else { | ||
return Promise.reject("REVISION NOT FOUND!"); // see how we should handle a pipeline failure | ||
} | ||
}); | ||
}, | ||
|
||
_sortBucketContents: function(data) { | ||
data.Contents = data.Contents.sort(function(a, b) { | ||
return new Date(b.LastModified) - new Date(a.LastModified); | ||
}); | ||
return data; | ||
}, | ||
|
||
_getBucketContents: function() { | ||
var client = this._client; | ||
var keyPrefix = this._keyPrefix; | ||
var bucket = this._bucket; | ||
fetchRevisions: function(options) { | ||
var client = this._client; | ||
var bucket = options.bucket; | ||
var revisionPrefix = path.join(options.prefix, options.filePattern + ":"); | ||
var indexKey = path.join(options.prefix, options.filePattern); | ||
var listObjects = Promise.denodeify(client.listObjects.bind(client)); | ||
|
||
return new Promise(function(resolve, reject) { | ||
var params = { Bucket: bucket, Prefix: keyPrefix }; | ||
|
||
client.listObjects(params, function(err, data) { | ||
if (err) { | ||
return reject(err); | ||
} else { | ||
return resolve(data); | ||
} | ||
return Promise.hash({ | ||
revisions: listObjects({ Bucket: bucket, Prefix: revisionPrefix }), | ||
current: headObject(client, { Bucket: bucket, Key: indexKey }), | ||
}) | ||
.then(function(data) { | ||
return data.revisions.Contents.sort(function(a, b) { | ||
return new Date(b.LastModified) - new Date(a.LastModified); | ||
}).map(function(d) { | ||
var revision = d.Key.substr(revisionPrefix.length); | ||
var active = data.current && d.ETag === data.current.ETag; | ||
return { revision: revision, timestamp: d.LastModified, active: active }; | ||
}); | ||
}); | ||
}, | ||
|
||
_getCurrentData: function() { | ||
var client = this._client; | ||
var bucket = this._bucket; | ||
var currentRevisionIdentifier = this._currentRevisionIdentifier; | ||
|
||
return new Promise(function(resolve, reject) { | ||
var params = { Bucket: bucket, Key: currentRevisionIdentifier }; | ||
|
||
client.getObject(params, function(err, data) { | ||
if (err && err.code === 'NoSuchKey') { | ||
return resolve(); | ||
} | ||
else if (err) { | ||
return reject(err); | ||
} | ||
else { | ||
var json = JSON.parse(data.Body.toString('utf8')); | ||
|
||
return resolve(json.revision); | ||
} | ||
}); | ||
}); | ||
} | ||
}); |