Skip to content

Commit

Permalink
fix for issue with settings page api call and sub folder installs
Browse files Browse the repository at this point in the history
  • Loading branch information
Lewis McMahon committed Jul 7, 2016
1 parent 84d13c6 commit 64c4c28
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 88 deletions.
12 changes: 10 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ var settings = {
var accessKeyIdFromDb = false;
var secretAccessKeyFromDb = false;

var adminRoute = "/admin/plugins/s3-uploads";

function fetchSettings(callback) {
db.getObjectFields(Package.name, Object.keys(settings), function (err, newSettings) {
if (err) {
Expand Down Expand Up @@ -133,6 +131,7 @@ plugin.load = function (params, callback) {
if (err) {
return winston.error(err.message);
}
var adminRoute = "/admin/plugins/s3-uploads";

params.router.get(adminRoute, params.middleware.applyCSRF, params.middleware.admin.buildHeader, renderAdmin);
params.router.get("/api" + adminRoute, params.middleware.applyCSRF, renderAdmin);
Expand All @@ -148,10 +147,19 @@ function renderAdmin(req, res) {
// Regenerate csrf token
var token = req.csrfToken();

var Config = require("./../../config.json");
var forumPath = "";
if(Config.url){
forumPath = forumPath+String(Config.url);
}
if(forumPath.split("").reverse()[0] != "/" ){
forumPath = forumPath + "/";
}
var data = {
bucket: settings.bucket,
host: settings.host,
path: settings.path,
forumPath: forumPath,
region: settings.region,
accessKeyId: (accessKeyIdFromDb && settings.accessKeyId) || "",
secretAccessKey: (accessKeyIdFromDb && settings.secretAccessKey) || "",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nodebb-plugin-s3-uploads-updated",
"version": "0.3.10",
"version": "0.3.11",
"description": "A plugin for NodeBB to take file uploads and store them on S3 updated for 1.0",
"main": "index.js",
"scripts": {
Expand Down
188 changes: 103 additions & 85 deletions templates/admin/plugins/s3-uploads.tpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<h1><i class="fa fa-picture-o"></i> S3 Uploads Configuration</h1>
<hr />
<hr/>

<p>You can configure this plugin via a combination of the below, for instance, you can use <em>instance meta-data</em> and <em>environment variables</em> in combination. You can also specify values in the form below, and those will be stored in the database.</p>
<p>You can configure this plugin via a combination of the below, for instance, you can use <em>instance meta-data</em>
and <em>environment variables</em> in combination. You can also specify values in the form below, and those will be
stored in the database.</p>

<h3>Environment Variables</h3>
<pre><code>export AWS_ACCESS_KEY_ID="xxxxx"
Expand All @@ -12,104 +14,120 @@ export S3_UPLOADS_PATH="path"
</code></pre>

<p>
Asset host and asset path are optional. You can leave these blank to default to the standard asset url - http://mybucket.s3.amazonaws.com/uuid.jpg.<br/>
Asset host can be set to a custom asset host. For example, if set to cdn.mywebsite.com then the asset url is http://cdn.mywebsite.com/uuid.jpg.<br/>
Asset path can be set to a custom asset path. For example, if set to /assets, then the asset url is http://mybucket.s3.amazonaws.com/assets/uuid.jpg.<br/>
If both are asset host and path are set, then the url will be http://cdn.mywebsite.com/assets/uuid.jpg.
Asset host and asset path are optional. You can leave these blank to default to the standard asset url -
http://mybucket.s3.amazonaws.com/uuid.jpg.<br/>
Asset host can be set to a custom asset host. For example, if set to cdn.mywebsite.com then the asset url is
http://cdn.mywebsite.com/uuid.jpg.<br/>
Asset path can be set to a custom asset path. For example, if set to /assets, then the asset url is
http://mybucket.s3.amazonaws.com/assets/uuid.jpg.<br/>
If both are asset host and path are set, then the url will be http://cdn.mywebsite.com/assets/uuid.jpg.
</p>

<h3>Instance meta-data</h3>
<p>This plugin is compatible with the instance meta-data API, you'll need to setup role delegation for this to work. See the following links:</p>
<p>This plugin is compatible with the instance meta-data API, you'll need to setup role delegation for this to work. See
the following links:</p>
<ul>
<li><a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html">EC2 Documentation: Instance Metadata and User Data</a></li>
<li><a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/roles-assume-role.html">IAM Documentation: Assuming a Role</a></li>
<li><a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/role-usecase-ec2app.html">IAM Documentation: EC2 Role Example</a></li>
<li><a href="http://docs.aws.amazon.com/STS/latest/UsingSTS/sts_delegate.html">STS Documentation: Delegation</a></li>
<li><a href="http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html">EC2 Documentation:
Instance Metadata and User Data</a></li>
<li><a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/roles-assume-role.html">IAM Documentation: Assuming a
Role</a></li>
<li><a href="http://docs.aws.amazon.com/IAM/latest/UserGuide/role-usecase-ec2app.html">IAM Documentation: EC2 Role
Example</a></li>
<li><a href="http://docs.aws.amazon.com/STS/latest/UsingSTS/sts_delegate.html">STS Documentation: Delegation</a>
</li>
</ul>
<div class="alert alert-warning">
<p>If you need help, create an <a href="https://github.com/LewisMcMahon/nodebb-plugin-s3-uploads/issues/">issue on Github</a>.</p>
<p>If you need help, create an <a href="https://github.com/LewisMcMahon/nodebb-plugin-s3-uploads/issues/">issue on
Github</a>.</p>
</div>

<h3>Database Stored configuration:</h3>
<form id="s3-upload-bucket">
<label for="s3bucket">Bucket</label><br />
<input type="text" id="s3bucket" name="bucket" value="{bucket}" title="S3 Bucket" class="form-control input-lg" placeholder="S3 Bucket"><br />

<label for="s3host">Host</label><br />
<input type="text" id="s3host" name="host" value="{host}" title="S3 Host" class="form-control input-lg" placeholder="website.com"><br />

<label for="s3path">Path</label><br />
<input type="text" id="s3path" name="path" value="{path}" title="S3 Path" class="form-control input-lg" placeholder="/assets"><br />

<label for="aws-region">Region</label><br />
<select id="aws-region" name="region" title="AWS Region" class="form-control">
<option value="">.. </option>
<option value="us-east-1">Standard (us-east-1)</option>
<option value="us-west-1">N. California (us-west-1)</option>
<option value="us-west-2">Oregon (us-west-2)</option>
<option value="eu-west-1">Ireland (eu-west-1)</option>
<option value="eu-central-1">Frankfurt (eu-central-1)</option>
<option value="ap-northeast-1">Tokyo (ap-northeast-1)</option>
<option value="ap-northeast-2">Seoul (ap-northeast-2)</option>
<option value="ap-southeast-1">Singapore (ap-southeast-1)</option>
<option value="ap-southeast-2">Sydney (ap-southeast-2)</option>
<option value="sa-east-1">São Paulo (sa-east-1)</option>
</select>
<br />

<button class="btn btn-primary" type="submit">Save</button>
<label for="s3bucket">Bucket</label><br/>
<input type="text" id="s3bucket" name="bucket" value="{bucket}" title="S3 Bucket" class="form-control input-lg"
placeholder="S3 Bucket"><br/>

<label for="s3host">Host</label><br/>
<input type="text" id="s3host" name="host" value="{host}" title="S3 Host" class="form-control input-lg"
placeholder="website.com"><br/>

<label for="s3path">Path</label><br/>
<input type="text" id="s3path" name="path" value="{path}" title="S3 Path" class="form-control input-lg"
placeholder="/assets"><br/>

<label for="aws-region">Region</label><br/>
<select id="aws-region" name="region" title="AWS Region" class="form-control">
<option value="">..</option>
<option value="us-east-1">Standard (us-east-1)</option>
<option value="us-west-1">N. California (us-west-1)</option>
<option value="us-west-2">Oregon (us-west-2)</option>
<option value="eu-west-1">Ireland (eu-west-1)</option>
<option value="eu-central-1">Frankfurt (eu-central-1)</option>
<option value="ap-northeast-1">Tokyo (ap-northeast-1)</option>
<option value="ap-northeast-2">Seoul (ap-northeast-2)</option>
<option value="ap-southeast-1">Singapore (ap-southeast-1)</option>
<option value="ap-southeast-2">Sydney (ap-southeast-2)</option>
<option value="sa-east-1">São Paulo (sa-east-1)</option>
</select>
<br/>

<button class="btn btn-primary" type="submit">Save</button>
</form>

<br><br>
<form id="s3-upload-credentials">
<label for="bucket">Credentials</label><br />
<div class="alert alert-warning">
Configuring this plugin using the fields below is <strong>NOT recommended</strong>, as it can be a potential security issue. We highly recommend that you investigate using either <strong>Environment Variables</strong> or <strong>Instance Meta-data</strong>
</div>
<input type="text" name="accessKeyId" value="{accessKeyId}" maxlength="20" title="Access Key ID" class="form-control input-lg" placeholder="Access Key ID"><br />
<input type="text" name="secretAccessKey" value="{secretAccessKey}" title="Secret Access Key" class="form-control input-lg" placeholder="Secret Access Key"><br />
<button class="btn btn-primary" type="submit">Save</button>
<label for="bucket">Credentials</label><br/>
<div class="alert alert-warning">
Configuring this plugin using the fields below is <strong>NOT recommended</strong>, as it can be a potential
security issue. We highly recommend that you investigate using either <strong>Environment Variables</strong> or
<strong>Instance Meta-data</strong>
</div>
<input type="text" name="accessKeyId" value="{accessKeyId}" maxlength="20" title="Access Key ID"
class="form-control input-lg" placeholder="Access Key ID"><br/>
<input type="text" name="secretAccessKey" value="{secretAccessKey}" title="Secret Access Key"
class="form-control input-lg" placeholder="Secret Access Key"><br/>
<button class="btn btn-primary" type="submit">Save</button>
</form>

<script>
$(document).ready(function() {
$('#aws-region option[value="{region}"]').prop('selected', true)
$("#s3-upload-bucket").on("submit", function (e) {
e.preventDefault();
save("s3settings", this);
});
$("#s3-upload-credentials").on("submit", function (e) {
e.preventDefault();
var form = this;
bootbox.confirm("Are you sure you wish to store your credentials for accessing S3 in the database?", function (confirm) {
if (confirm) {
save("credentials", form);
}
});
});
function save(type, form) {
var data = {
_csrf: '{csrf}' || $('#csrf_token').val()
};
var values = $(form).serializeArray();
for (var i = 0, l = values.length; i < l; i++) {
data[values[i].name] = values[i].value;
}
$.post('/api/admin/plugins/s3-uploads/' + type, data).done(function (response) {
if (response) {
ajaxify.refresh();
app.alertSuccess(response);
}
}).fail(function (jqXHR, textStatus, errorThrown) {
ajaxify.refresh();
app.alertError(jqXHR.responseJSON ? jqXHR.responseJSON.error : 'Error saving!');
});
}
});
$(document).ready(function () {
$('#aws-region option[value="{region}"]').prop('selected', true)
$("#s3-upload-bucket").on("submit", function (e) {
e.preventDefault();
save("s3settings", this);
});
$("#s3-upload-credentials").on("submit", function (e) {
e.preventDefault();
var form = this;
bootbox.confirm("Are you sure you wish to store your credentials for accessing S3 in the database?", function (confirm) {
if (confirm) {
save("credentials", form);
}
});
});
function save(type, form) {
var data = {
_csrf: '{csrf}' || $('#csrf_token').val()
};
var values = $(form).serializeArray();
for (var i = 0, l = values.length; i < l; i++) {
data[values[i].name] = values[i].value;
}
$.post('{forumPath}api/admin/plugins/s3-uploads/' + type, data).done(function (response) {
if (response) {
ajaxify.refresh();
app.alertSuccess(response);
}
}).fail(function (jqXHR, textStatus, errorThrown) {
ajaxify.refresh();
app.alertError(jqXHR.responseJSON ? jqXHR.responseJSON.error : 'Error saving!');
});
}
});
</script>

0 comments on commit 64c4c28

Please sign in to comment.