Skip to content

Commit

Permalink
jiren changes from pur 68
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdonthemic committed Jan 9, 2014
1 parent 0610ea5 commit cd3673c
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 69 deletions.
3 changes: 2 additions & 1 deletion .env-sample
Expand Up @@ -12,4 +12,5 @@ SKIP_AUTH=false
GOOGLE_REDIRECT_URL=
MANDRILL_API_KEY=
SUPPORT_EMAIL=
AWS_SECRET_KEY=
AWS_SECRET_KEY=
AWS_ACCESS_KEY=
20 changes: 13 additions & 7 deletions actions/aws_signature.js
Expand Up @@ -10,23 +10,29 @@ exports.action = {
name: "awsSignature",
description: "Generate s3 signature to upload doc. Method: POST",
inputs: {
required: ['redirect_url'],
required: ['redirect_url', 'file_name'],
optional: []
},
authenticated: true,
outputExample: {},
version: 1.0,
run: function(api, connection, next) {
console.log("running get s")
var acl = 'public-read';
var file_name = (new Date()).getTime() + '-' + connection.params.file_name;
var signature = aws.s3Signature(api.configData.aws.secret_key,
[
["starts-with", "$key", "thurgood/"],
{"acl": "public-read"},
{"success_action_redirect": connection.params.redirect_url},
["starts-with", "$Content-Type", ""],
["content-length-range", 0, 104857600]
["eq", "$bucket", api.configData.aws.bucket],
["starts-with", "$key", file_name],
{"acl": acl},
["starts-with", "$Content-Type", ""]
],
1);

signature.aws_access_key = api.configData.aws.access_key;
signature.file_name = file_name;
signature.acl = acl;
signature.codeUrl = 'https://'+ api.configData.aws.bucket + '.s3.amazonaws.com/' + file_name;

api.response.success(connection, null, signature);
next(connection, true);
}
Expand Down
4 changes: 3 additions & 1 deletion config.js
Expand Up @@ -160,7 +160,9 @@ configData.rabbitmq = {
//////////////

configData.aws = {
secret_key: process.env.AWS_SECRET_KEY
secret_key: process.env.AWS_SECRET_KEY,
access_key: process.env.AWS_ACCESS_KEY,
bucket: 'cs-thurgood'
};

//////////////
Expand Down
22 changes: 5 additions & 17 deletions lib/aws_signature.js
Expand Up @@ -3,17 +3,18 @@ var crypto = require('crypto');
function s3Signature(aws_secret_key, conditions, expiration_in_minutes) {
var policy = { "conditions": conditions }, expiration = new Date();

expiration_in_minutes = expiration_in_minutes || 2;
expiration_in_minutes = expiration_in_minutes || 3;
expiration.setMinutes(expiration.getMinutes() + expiration_in_minutes);
policy['expiration'] = expiration;

var policy_base64 = new Buffer(JSON.stringify(policy))
policy_json = JSON.stringify({expiration: expiration, conditions: conditions});

var policy_base64 = new Buffer(policy_json)
.toString("base64")
.replace(/\n/g, "");


var signature = crypto.createHmac("sha1", aws_secret_key)
.update(s3Policy(conditions, expiration_in_minutes))
.update(policy_base64)
.digest("base64")
.replace(/\n/g, "");
return {
Expand All @@ -22,17 +23,4 @@ function s3Signature(aws_secret_key, conditions, expiration_in_minutes) {
}
}

function s3Policy(conditions, expiration_in_minutes){
var policy = { "conditions": conditions }, expiration = new Date();

expiration_in_minutes = expiration_in_minutes || 2;
expiration.setMinutes(expiration.getMinutes() + expiration_in_minutes);
policy['expiration'] = expiration;

return new Buffer(JSON.stringify(policy)).toString("base64").replace(/\n/g, "");
}


exports.s3Signature = s3Signature;
exports.s3Policy = s3Policy;

66 changes: 31 additions & 35 deletions public/js/controllers.js
Expand Up @@ -31,7 +31,7 @@ thurgood.controller('NavCtrl', ['$scope', '$location', '$http', 'Auth', function
/**
* Controller for the Jobs page
*/
thurgood.controller('JobsCtrl', ['$scope', '$http', '$filter', '$location', '$modal', 'Jobs', 'ngTableParams', function($scope, $http, $filter, $location, $modal, Jobs, ngTableParams) {
thurgood.controller('JobsCtrl', ['$scope', '$http', '$filter', '$location', '$modal', 'Jobs', 'ngTableParams', 'AwsS3', function($scope, $http, $filter, $location, $modal, Jobs, ngTableParams, AwsS3) {
var promise = Jobs.query().$promise;
$scope.loading = true;
$scope.submitStatus = {};
Expand Down Expand Up @@ -112,22 +112,21 @@ thurgood.controller('JobsCtrl', ['$scope', '$http', '$filter', '$location', '$mo

// Upload file
$scope.fileNameChanged = function(file) {
$http.post('/api/1/awssignature', {redirect_url: $location.absUrl()}).success(function(res){

if (res.success != true) {
errorHandler(res);
return;
}

$scope.policy = res.aws.policy;
$scope.signature = res.aws.signature;
$scope.$apply();

document.getElementById("s3UploadForm").submit();
var url = 'https://cs-test-jeff.s3.amazonaws.com/thurgood/' + $scope.timestamp + '-' + file.name;
$scope.job.codeUrl = url;
var awsPromise = AwsS3.signature({redirect_url: $location.absUrl(), file_name: file.name});
awsPromise.success(function(res){
var data = res.data;

$scope.policy = data.policy;
$scope.signature = data.signature;
$scope.file_name = data.file_name;
$scope.job.codeUrl = data.codeUrl;
$scope.uploadWarning = 'Note: please make sure the file has finished uploading before pressing Create!';
$scope.$apply();

AwsS3.upload($('#s3UploadForm'), file, data, function(){
$scope.status = 'Upload Complete.';
$scope.uploadWarning = undefined;
$scope.$digest($scope);
});
});
};
}
Expand Down Expand Up @@ -224,7 +223,7 @@ thurgood.controller('JobsCtrl', ['$scope', '$http', '$filter', '$location', '$mo
/**
* Controller for a job's detail page
*/
thurgood.controller('JobsDetailCtrl', ['$scope', '$http', '$location', '$routeParams', '$modal', 'Jobs', 'Pt', function($scope, $http, $location, $routeParams, $modal, Jobs, Pt) {
thurgood.controller('JobsDetailCtrl', ['$scope', '$http', '$location', '$routeParams', '$modal', 'Jobs', 'Pt', 'AwsS3', function($scope, $http, $location, $routeParams, $modal, Jobs, Pt, AwsS3) {
var jobId = $routeParams.id;
var job = {};
$scope.jobId = jobId;
Expand Down Expand Up @@ -370,25 +369,22 @@ thurgood.controller('JobsDetailCtrl', ['$scope', '$http', '$location', '$routePa

// Upload file
$scope.fileNameChanged = function(file) {
$http.post('/api/1/awssignature', {redirect_url: $location.absUrl()}).success(function(res){

if (res.success != true) {
errorHandler(res);
return;
}

$scope.policy = res.data.policy;
$scope.signature = res.data.signature;
$scope.$apply();

console.log($scope.policy);
console.log($scope.signature);

document.getElementById("s3UploadForm").submit();
var url = 'https://cs-test-jeff.s3.amazonaws.com/thurgood/' + $scope.job.id + '-' + file.name;
$scope.job.codeUrl = detailScope.job.codeUrl = url;
var awsPromise = AwsS3.signature({redirect_url: $location.absUrl(), file_name: file.name});
awsPromise.success(function(res){
var data = res.data;

$scope.policy = data.policy;
$scope.signature = data.signature;
//$scope.redirect_url = 'https://thurgood.s3.amazonaws.com/thurgood/' + file_name;
$scope.file_name = data.file_name;
$scope.job.codeUrl = data.codeUrl;
$scope.uploadWarning = 'Note: please make sure the file has finished uploading before pressing Create!';
$scope.$apply();

AwsS3.upload($('#s3UploadForm'), file, data, function(){
$scope.status = 'Upload Complete.';
$scope.uploadWarning = undefined;
$scope.$digest($scope);
});
});
};
}
Expand Down
33 changes: 32 additions & 1 deletion public/js/services.js
Expand Up @@ -121,7 +121,38 @@ thurgood.factory('Auth', function($http, $location){

accessLevels: accessLevels,
userRoles: userRoles,
user: currentUser
user: currentUser,

};
});

thurgood.factory('AwsS3', ['$http', function ($http) {
return {
signature: function(data){
return $http.post('/api/1/awssignature', data);
},

upload: function(form, file, data, success){
var fd = new FormData();

fd.append('key', data.file_name);
fd.append('acl', data.acl);
fd.append('Content-Type', '');
fd.append('AWSAccessKeyId', data.aws_access_key);
fd.append('policy', data.policy)
fd.append('signature', data.signature);
fd.append("file", file);

return $.ajax({
url: form.attr('action'),
data: fd,
processData: false,
contentType: false,
type: 'POST',
success: success,
async: true
});
}
}
}]);

7 changes: 4 additions & 3 deletions public/views/pages/jobs-detail.html
Expand Up @@ -106,10 +106,11 @@ <h4><span ng-show="status == 'SUCCESS'">Job <a href="#/jobs/{{jobId}}" ng-click=
<div>
<input type="text" class="form-control mModal-input" ng-model="job.codeUrl" placeholder="Code URL">
</div>
<form id="s3UploadForm" action="https://cs-test-jeff.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="thurgood/{{job.id}}-${filename}">
<form id="s3UploadForm" action="https://cs-thurgood.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="{{file_name}}">
<input type="hidden" name="acl" value="public-read">
<input type="hidden" name="AWSAccessKeyId" value="AKIAJXNGEXQID3CELIVA">
<input type="hidden" name="Content-Type" value='' />
<input type="hidden" name="AWSAccessKeyId" value="">
<input type="hidden" name="policy" value="{{policy}}">
<input type="hidden" name="signature" value="{{signature}}">
<input type="submit">
Expand Down
9 changes: 5 additions & 4 deletions public/views/pages/jobs.html
Expand Up @@ -23,7 +23,7 @@ <h1>Jobs{{totalItems >= 0 ? ' (' + totalItems + ')' : ''}}</h1>
<h3>Create job</h3>
</div>
<div class="modal-body mModal">
<div class="alert alert-{{status == 'Creating job...' || status == 'SUCCESS' ? 'success' : 'danger'}} mModal" ng-show="status">
<div class="alert alert-{{status == 'Creating job...' || status == 'SUCCESS' || status == 'Upload Complete.' ? 'success' : 'danger'}} mModal" ng-show="status">
<h4><span ng-show="status == 'SUCCESS'">Job <a href="#/jobs/{{jobId}}" ng-click="cancel()">{{jobId}}</a> created successfully!</span>{{status == 'SUCCESS' ? '' : status}}</h4>
</div>
<div class="form-horizontal">
Expand All @@ -33,10 +33,11 @@ <h4><span ng-show="status == 'SUCCESS'">Job <a href="#/jobs/{{jobId}}" ng-click=
<div>
<input type="text" class="form-control mModal-input" ng-model="job.codeUrl" placeholder="Code URL">
</div>
<form id="s3UploadForm" action="https://cs-test-jeff.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="thurgood/{{timestamp}}-${filename}">
<form id="s3UploadForm" action="https://cs-thurgood.s3.amazonaws.com/" method="post" enctype="multipart/form-data">
<input type="hidden" name="key" value="{{file_name}}">
<input type="hidden" name="acl" value="public-read">
<input type="hidden" name="AWSAccessKeyId" value="AKIAJXNGEXQID3CELIVA">
<input type="hidden" name="Content-Type" value='' />
<input type="hidden" name="AWSAccessKeyId" value="">
<input type="hidden" name="policy" value="{{policy}}">
<input type="hidden" name="signature" value="{{signature}}">
<input type="submit">
Expand Down

0 comments on commit cd3673c

Please sign in to comment.