diff --git a/HTML/Files.html b/HTML/Files.html new file mode 100644 index 0000000..9419ed4 --- /dev/null +++ b/HTML/Files.html @@ -0,0 +1,12 @@ + + +
+ + diff --git a/HTML/Files_test.html b/HTML/Files_test.html new file mode 100644 index 0000000..d7c74f2 --- /dev/null +++ b/HTML/Files_test.html @@ -0,0 +1,16 @@ + + + + + + + + + + + + + diff --git a/HTML/s3files.js b/HTML/s3files.js new file mode 100644 index 0000000..1b28e38 --- /dev/null +++ b/HTML/s3files.js @@ -0,0 +1,142 @@ +var dataBucketName = 'pocloud-data'; +var bucketRegion = 'us-east-1'; +var IdentityPoolId = 'us-east-1:4ba4f2d0-ff79-446e-a510-ac16e411d3cc'; + +AWS.config.update({ + region: bucketRegion, + credentials: new AWS.CognitoIdentityCredentials({ + IdentityPoolId: IdentityPoolId + }) +}); + +function getHtml(template) { + return template.join('\n'); + } + +var s3 = new AWS.S3({ + // apiVersion: '2006-03-01', + params: {Bucket: dataBucketName} +}); + +function fixDeviceId(deviceId){ + deviceId = deviceId.replace(/:/g, ""); + deviceId = deviceId.replace("[", ""); + deviceId = deviceId.replace("]", ""); + return deviceId; +} + +function checkDevice(deviceId) { + deviceId = fixDeviceId(deviceId); + // deviceId = deviceId.trim(); + if (!deviceId) { + return alert('Device names must contain at least one non-space character.'); + } + if (deviceId.indexOf('/') !== -1) { + return alert('Device names cannot contain slashes.'); + } + var deviceKey = encodeURIComponent(deviceId) + '/'; + s3.headObject({Key: deviceKey}, function(err, data) { + if (!err) { + // return alert('Device already exists.'); + return viewDevicefiles(deviceId); + } + if (err.code !== 'NotFound') { + return alert('There was an error creating your device: ' + err.message); + } + s3.putObject({Key: deviceKey}, function(err, data) { + if (err) { + return alert('There was an error creating your device: ' + err.message); + } + // alert('Successfully created device.'); + return viewDevicefiles(deviceId); + }); + }); +} + +function viewDevicefiles(deviceId) { + deviceId = fixDeviceId(deviceId); + var deviceFileKey = encodeURIComponent(deviceId) + '//'; + s3.listObjects({Prefix: deviceFileKey}, function(err, data) { + if (err) { + return alert('There was an error viewing your device: ' + err.message); + } + // `this` references the AWS.Response instance that represents the response + var href = this.request.httpRequest.endpoint.href; + var bucketUrl = href + dataBucketName + '/'; + console.log(data); + + var files = data.Contents.map(function(file) { + var fileKey = file.Key; + var fileUrl = bucketUrl + encodeURIComponent(fileKey); + var fileFilename = fileKey.replace(deviceFileKey, ''); + var fileSize = file.Size / 1000; // kB + var lastModified = file.LastModified; + console.log(file); + return getHtml([ + 'You do not have any files for this device. Please add files.
'; + var htmlTemplate = [ + '| Filename | File Size | Last Modified |
|---|