Skip to content

Commit

Permalink
NXP-24490: add bundle to contribute S3 upload provider for Web UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Gabez0r authored and Florent Guillaume committed Mar 8, 2018
1 parent bf22605 commit 775e526
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 0 deletions.
15 changes: 15 additions & 0 deletions nuxeo-core-binarymanager-s3-web-ui/pom.xml
@@ -0,0 +1,15 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.nuxeo.ecm.core</groupId>
<artifactId>nuxeo-core-binarymanager-cloud</artifactId>
<version>10.1-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>nuxeo-core-binarymanager-s3-web-ui</artifactId>
<name>Nuxeo S3 Upload Provider for Web UI</name>
<description>Nuxeo Core S3 Upload Provider for Web UI</description>

</project>
@@ -0,0 +1,4 @@
Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-SymbolicName: org.nuxeo.ecm.core.storage.binarymanager.s3.webui;singleton:=true
Nuxeo-Component: OSGI-INF/nuxeo-s3-direct-upload-webresources-contrib.xml
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<fragment version="1">

<install>
<!-- Unzip the contents of our web application into the server -->
<unzip from="${bundle.fileName}" to="/" prefix="web">
<include>web/nuxeo.war/**</include>
</unzip>
</install>

</fragment>
@@ -0,0 +1,21 @@
<?xml version="1.0"?>

<component name="org.nuxeo.ecm.core.storage.binarymanager.s3.webui.resources.contrib">

<require>org.nuxeo.web.ui.resources</require>

<extension target="org.nuxeo.ecm.platform.WebResources" point="resources">
<resource name="nuxeo-s3-direct-upload.html" type="import">
<uri>/ui/nuxeo-s3-direct-upload/nuxeo-s3-direct-upload.html</uri>
</resource>
</extension>

<extension target="org.nuxeo.ecm.platform.WebResources" point="bundles">
<bundle name="web-ui">
<resources append="true">
<resource>nuxeo-s3-direct-upload.html</resource>
</resources>
</bundle>
</extension>

</component>

Large diffs are not rendered by default.

@@ -0,0 +1,159 @@
<!--
@license
(C) Copyright Nuxeo Corp. (http://nuxeo.com/)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<script type="text/javascript" src="aws-sdk-2.201.0.min.js"></script>
<script type="text/javascript">
(function () {
var _resource;

var S3Provider = function(connection, accept, batchAppend) {
this.connection = connection;
this.accept = accept;
this.batchAppend = batchAppend;
this.uploader = null;
this.batchId = null;
this.files = [];
};

S3Provider.prototype._resource = function() {
if (!_resource) {
_resource = document.createElement('nuxeo-resource');
document.body.appendChild(_resource);
}
return _resource;
}

S3Provider.prototype._upload = function(file, callback) {
return new Promise(function(resolve, reject) {
this.uploader.upload({
Key: this.extraInfo.baseKey.replace(/^\/+/g, '').concat(file.name),
ContentType: file.type,
Body: file
}).on('httpUploadProgress', function(evt) {
if (typeof callback === 'function') {
callback({ type: 'uploadProgress', fileIdx: file.index, progress: (evt.loaded / evt.total) * 100 });
}
}.bind(this)).send(function(error, data) {
if (error === null) {
var resource = this._resource();
resource.path = ['upload', this.batchId, file.index, 'complete'].join('/');
resource.data = {
name: file.name,
fileSize: file.size,
key: data.Key,
bucket: data.Bucket,
etag: data.ETag
};
resource.post().then(function() {
if (typeof callback === 'function') {
callback({ type: 'uploadCompleted', fileIdx: file.index })
}
resolve();
});
} else {
reject(error)
}
}.bind(this));
}.bind(this));
};

S3Provider.prototype._ensureBatch = function() {
if (!this.batchAppend || !this.uploader) {
this.files = [];
return this._newBatch();
}
return Promise.resolve();
};

S3Provider.prototype._initCredentials = function(options) {
AWS.config.update({
credentials: new AWS.Credentials(options.awsSecretKeyId, options.awsSecretAccessKey, options.awsSessionToken),
region: options.region,
useAccelerateEndpoint: options.useS3Accelerate || false,
leavePartsOnError: true
});

AWS.config.credentials.expireTime = new Date(options.expiration);
};

S3Provider.prototype._newBatch = function() {
var resource = this._resource();
resource.path = 'upload/new/s3';
return resource.post().then(function(response) {
this.batchId = response.batchId;
this.extraInfo = response.extraInfo;
this._initCredentials(response.extraInfo);
this.uploader = new AWS.S3({
params: {
Bucket: this.extraInfo.bucket,
},
computeChecksums: true
});
}.bind(this));
};

S3Provider.prototype.accepts = Nuxeo.UploaderBehavior.getProviders()['default'].prototype.accepts;

S3Provider.prototype.upload = function(files, callback) {
this._ensureBatch().then(function() {
if ((new Date()).getTime() >= this.extraInfo.expiration) {
this._refreshBatchInfo();
}

var promises = [];
for (var i = 0; i < files.length; ++i) {
var file = files[i];
file.index = this.files.length;
this.files.push(file);
if (typeof callback === 'function') {
callback({ type: 'uploadStarted', file: file })
}
promises.push(this._upload(file, callback));
}
return Promise.all(promises).then(function() {
callback({ type: 'batchFinished', batchId: this.batchId });
}.bind(this));
}.bind(this));
};

S3Provider.prototype.hasProgress = function() {
return true;
};

S3Provider.prototype.cancelBatch = function() {
this.uploader = null;
this.batchId = null;
this.files = [];
};

S3Provider.prototype.batchExecute = function(operationId, params, headers) {
return this.connection.operation(operationId).then(function(operation) {
var options = {
url: [operation._nuxeo._restURL, 'upload', this.batchId, 'execute', operationId].join('/')
.replace(/\/+/g, '\/')
};
if (headers) {
options['headers'] = headers;
}
if (params.context) {
operation = operation.context(params.context);
}
return operation.params(params).execute(options);
}.bind(this));
};

Nuxeo.UploaderBehavior.registerProvider('s3', S3Provider);
Nuxeo.UploaderBehavior.defaultProvider = 's3';
})();
</script>
1 change: 1 addition & 0 deletions pom.xml
Expand Up @@ -14,6 +14,7 @@
<modules>
<module>nuxeo-core-binarymanager-common</module>
<module>nuxeo-core-binarymanager-s3</module>
<module>nuxeo-core-binarymanager-s3-web-ui</module>
<module>nuxeo-core-binarymanager-azure</module>
<module>nuxeo-core-binarymanager-jclouds</module>
</modules>
Expand Down

0 comments on commit 775e526

Please sign in to comment.