diff --git a/app.js b/app.js index 5ba7dcd..97bc0ff 100644 --- a/app.js +++ b/app.js @@ -1,3 +1,4 @@ +const allUsersURI = 'http://acs.amazonaws.com/groups/global/AllUsers' const dateFormat = 'YYYY-MM-DD HH:mm:ss' // showAlert creates an alert message and displays it @@ -47,9 +48,8 @@ function fileActionCallback(err, data) { } // filenameInput is the callback for changes in the filename -function filenameInput() { - setEditorMime($(this).val()) - $('#file-url').val(window.past3_config.base_url + getFilePrefix() + $(this).val()) +function filenameInput(e) { + updateFileURL() } // formatDate formats a Date() object into iso-like format @@ -95,8 +95,26 @@ function getFile(filename) { Key: getFilePrefix() + filename, }, loadFileIntoEditor) + // Check whether the file is public + s3.getObjectAcl({ + Bucket: window.past3_config.bucket, + Key: getFilePrefix() + filename, + }, (err, data) => { + if (err) return error(err) + + for (let grant of data.Grants) { + if (grant.Grantee.Type == "Group" && grant.Grantee.URI == allUsersURI) { + $('#acl').data('public', true) + return updateFileURL() + } + } + + $('#acl').data('public', false) + return updateFileURL() + }) + $('#filename').val(filename) - $('#filename').trigger('input') + updateFileURL() } // getFilePrefix retrieves the file prefix using the Cognito identityId @@ -165,7 +183,17 @@ function init() { if (cf) cf.find('i').removeClass('fa-file').addClass('fa-file-upload') }) + $('#acl').data('public', window.past3_config.default_public) + // Set up bindings + $('#acl').bind('click', (e) => { + let el = $(e.target) + el.data('public', !el.data('public')) + + updateFileURL() + return false + }) + $('#filename').bind('input', filenameInput) $('#newFile').bind('click', () => { @@ -215,6 +243,12 @@ function init() { e.preventDefault() return false } + + if ((e.metaKey || e.ctrlKey) && e.keyCode == 76) { // cmd + l / ctrl + l + $('#acl').trigger('click') + e.preventDefault() + return false + } }) } @@ -274,10 +308,12 @@ function loadFileList(err, data) { fileIcon = 'file' } - let li = $(` ${key}`) - li.find('.badge').text(`${formatDate(obj.LastModified)}`) + let li = $(` ${key}`) li.data('file', key) + let badge = $(`${formatDate(obj.LastModified)}`) + badge.appendTo(li) + if (key === $('#filename').val()) { li.addClass('active') } @@ -308,13 +344,16 @@ function renderButton() { // saveFile saves the editor content into the S3 bucket function saveFile(filename, content) { let mime = getMimeType(filename) + let pub = $('#acl').data('public') + let s3 = new AWS.S3() s3.putObject({ - ACL: window.past3_config.acl, + ACL: pub ? 'public-read' : 'private', Body: content, Bucket: window.past3_config.bucket, ContentType: mime.mime, Key: getFilePrefix() + filename, + ServerSideEncryption: 'AES256', }, saveFileCallback) } @@ -339,7 +378,7 @@ function setEditorMime(filename) { let autoMime = getMimeType(filename) window.editor.setOption('mode', autoMime.mime) CodeMirror.autoLoadMode(window.editor, autoMime.mode) - }, 500) + }, 100) } // signinCallback is triggered by the Google sign-in button @@ -355,6 +394,22 @@ function signinCallback(authResult) { } } +// updateFileURL sets the public URL of the file and adjusts the ACL button +function updateFileURL() { + let pub = $('#acl').data('public') + let filename = $('#filename').val() + + setEditorMime(filename) + + if (pub) { + $('#file-url').val(window.past3_config.base_url + getFilePrefix() + filename) + $('#acl').find('i').removeClass('fa-unlock').addClass('fa-lock') + } else { + $('#file-url').val('File is private, no URL available') + $('#acl').find('i').removeClass('fa-lock').addClass('fa-unlock') + } +} + // Initialize app on document ready $(() => init()) diff --git a/config.yml b/config.yml index 71f82e8..b18fed0 100644 --- a/config.yml +++ b/config.yml @@ -1,9 +1,8 @@ --- -# Canned ACL for files stored into S3. One of "private, public-read, -# public-read-write, authenticated-read, aws-exec-read, bucket-owner-read, -# bucket-owner-full-control" -acl: public-read +# Default acl setting for new files: If set to true the files are created +# using `public-read` acl, if set to false they are created with `private` acl. +default_public: true # Base URL for the uploaded files, either S3 download URL or CloudFront # distribution URL to which the file path is appended for display in the @@ -12,7 +11,7 @@ base_url: https://paste.luzifer.io/ # Theme name (lowercase) of Bootswatch.com theme (Optional, when not # specified the original bootstrap theme is used) -bootswatch_theme: cosmo +bootswatch_theme: flatly # Name of the Bucket used to store the files in. The bucket needs to be # whitelisted for Cognito authenticated users diff --git a/generate.py b/generate.py index 6d67af9..827aaf4 100755 --- a/generate.py +++ b/generate.py @@ -7,5 +7,4 @@ config = yaml.load(open('config.yml', 'r').read()) env = Environment(loader=FileSystemLoader('./')) template = env.get_template('index.html') -print(template.render(config)) - +print(template.render({'config': config})) diff --git a/index.html b/index.html index 5633b94..2bf5909 100644 --- a/index.html +++ b/index.html @@ -6,8 +6,8 @@ PaS(t)3 - S3 file editor - {% if bootswatch_theme %} - + {% if config.bootswatch_theme %} + {% else %} @@ -26,47 +26,37 @@ - + -