Fix CORS s3 upload.
This commit is contained in:
parent
c276bce177
commit
b1a5b9c343
3 changed files with 20 additions and 20 deletions
|
@ -29,17 +29,13 @@ class UserRequestFiles(object):
|
|||
self._secret_key = s3_secret_key
|
||||
self._prefix = 'userfiles'
|
||||
|
||||
def prepare_for_drop(self, mimeType):
|
||||
def prepare_for_drop(self, mime_type):
|
||||
""" Returns a signed URL to upload a file to our bucket. """
|
||||
file_id = str(self._prefix + '/' + str(uuid4()))
|
||||
|
||||
expires = str(int(time.time() + 300))
|
||||
signingString = "PUT\n\n" + mimeType + "\n" + expires + "\n/" + self._bucket_name + "/" + file_id;
|
||||
|
||||
hmac_signer = hmac.new(self._secret_key, signingString, sha)
|
||||
signature = base64.b64encode(hmac_signer.digest())
|
||||
|
||||
url = "http://s3.amazonaws.com/" + self._bucket_name + "/" + file_id + "?AWSAccessKeyId=" + self._access_key + "&Expires=" + expires + "&Signature=" + urllib.quote(signature);
|
||||
logger.debug('Requested upload url with content type: %s' % mime_type)
|
||||
file_id = str(uuid4())
|
||||
full_key = os.path.join(self._prefix, file_id)
|
||||
k = Key(self._bucket, full_key)
|
||||
url = k.generate_url(300, 'PUT', headers={'Content-Type': mime_type})
|
||||
return (url, file_id)
|
||||
|
||||
def store_file(self, flask_file):
|
||||
|
|
|
@ -398,8 +398,8 @@ def get_repo_builds(namespace, repository):
|
|||
|
||||
@app.route('/api/filedrop/', methods=['POST'])
|
||||
def get_filedrop_url():
|
||||
mimeType = request.get_json()['mimeType']
|
||||
(url, file_id) = user_files.prepare_for_drop(mimeType)
|
||||
mime_type = request.get_json()['mimeType']
|
||||
(url, file_id) = user_files.prepare_for_drop(mime_type)
|
||||
return jsonify({
|
||||
'url': url,
|
||||
'file_id': file_id
|
||||
|
|
|
@ -912,7 +912,7 @@ function V1Ctrl($scope, UserService) {
|
|||
};
|
||||
}
|
||||
|
||||
function NewRepoCtrl($scope, $location, UserService, Restangular) {
|
||||
function NewRepoCtrl($scope, $location, $http, UserService, Restangular) {
|
||||
$scope.repo = {
|
||||
'is_public': 1,
|
||||
'description': '',
|
||||
|
@ -938,16 +938,20 @@ function NewRepoCtrl($scope, $location, UserService, Restangular) {
|
|||
var conductUpload = function(repo, file, url, fileId, mimeType) {
|
||||
var request = new XMLHttpRequest();
|
||||
request.open('PUT', url, true);
|
||||
request.overrideMimeType(mimeType);
|
||||
request.setRequestHeader('Content-Type', mimeType);
|
||||
request.onprogress = function(e) {
|
||||
var percentLoaded;
|
||||
if (e.lengthComputable) {
|
||||
$scope.upload_progress = (e.loaded / e.total) * 100;
|
||||
}
|
||||
$scope.apply(function() {
|
||||
var percentLoaded;
|
||||
if (e.lengthComputable) {
|
||||
$scope.upload_progress = (e.loaded / e.total) * 100;
|
||||
}
|
||||
});
|
||||
};
|
||||
request.onerror = function() {
|
||||
$('#couldnotbuildModal').modal();
|
||||
$location.path('/repository/' + repo.namespace + '/' + repo.name);
|
||||
$scope.apply(function() {
|
||||
$('#couldnotbuildModal').modal();
|
||||
$location.path('/repository/' + repo.namespace + '/' + repo.name);
|
||||
});
|
||||
};
|
||||
request.onreadystatechange = function() {
|
||||
var state = request.readyState;
|
||||
|
|
Reference in a new issue