Permalink
Browse files

new feature: push charts to amazon s3

  • Loading branch information...
1 parent 165de2a commit 66f44f64a20bc22c94ba40bc6e103bf9676a3eb9 @gka gka committed Nov 8, 2012
Showing with 2,086 additions and 16 deletions.
  1. +8 −1 config.template.yaml
  2. +8 −0 controller/chart-publish.php
  3. +25 −10 lib/api/charts.php
  4. +2 −1 lib/utils/chart_content.php
  5. +3 −3 templates/chart-publish.twig
  6. +2,034 −0 vendor/s3/S3.php
  7. +5 −0 www/api/index.php
  8. +1 −1 www/index.php
View
@@ -24,4 +24,11 @@ log_email: debug@datawrapper.de
piwik:
url: datastory.de/piwik # without http:// and trailing slash
idSite: 5
- # idSiteNoCharts: 6 # additionally, you can track the user interface w/o the charts, too
+ # idSiteNoCharts: 6 # additionally, you can track the user interface w/o the charts, too
+
+# optional: publish your charts on Amazon S3
+
+# s3:
+# bucket: BUCKET_NAME
+# accesskey: YOUR_AWS_ACCESS_KEY
+# secretkey: YOUR_AWS_SECTET_KEY
@@ -13,11 +13,19 @@
$app->get('/chart/:id/publish', function ($id) use ($app) {
check_chart_writable($id, function($user, $chart) use ($app) {
+ $cfg = $GLOBALS['dw_config'];
+ if (empty($cfg['s3'])) {
+ $iframe_src = 'http://' . $cfg['chart_domain'] . '/' . $chart->getID() . '/';
+ } else {
+ $iframe_src = 'http://' . $cfg['s3']['bucket'] . '.s3.amazonaws.com/' . $chart->getID() . '/index.html';
+ }
+
$page = array(
'chartData' => $chart->loadData(),
'chart' => $chart,
'visualizations' => get_visualizations_meta('', true),
'vis' => get_visualization_meta($chart->getType()),
+ 'iframe' => $iframe_src,
'themes' => get_themes_meta()
);
add_header_vars($page, 'chart');
View
@@ -201,6 +201,14 @@ function get_static_path($chart) {
return $static_path;
}
+function push_to_s3($filename, $uploadName, $contentType = 'text/plain') {
+ if (!empty($GLOBALS['dw_config']['s3'])) {
+ $cfg = $GLOBALS['dw_config']['s3'];
+ $s3 = new S3($cfg['accesskey'], $cfg['secretkey']);
+ $s3->putObject($s3->inputFile($filename, false), $cfg['bucket'], $uploadName, S3::ACL_PUBLIC_READ, array(), array('Content-Type' => $contentType));
+ }
+}
+
/**
* API: copy a chart
*
@@ -211,9 +219,10 @@ function get_static_path($chart) {
try {
$static_path = get_static_path($chart);
$url = 'http://'.$GLOBALS['dw_config']['domain'].'/chart/'.$chart->getID().'/preview?minify=1';
+ $outf = $static_path . '/index.html';
if (function_exists('curl_init')) {
$ch = curl_init($url);
- $fp = fopen($static_path . '/index.html', 'w');
+ $fp = fopen($outf, 'w');
$strCookie = 'DW-SESSION=' . $_COOKIE['DW-SESSION'] . '; path=/';
session_write_close();
@@ -233,9 +242,9 @@ function get_static_path($chart) {
)
));
$html = file_get_contents($url, false, $context);
- file_put_contents($static_path . '/index.html', $html);
+ file_put_contents($outf, $html);
}
-
+ push_to_s3($outf, $chart->getID() . '/index.html', 'text/html');
ok();
} catch (Exception $e) {
error('io-error', $e->getMessage());
@@ -262,32 +271,34 @@ function get_static_path($chart) {
// generate visualization script
$vis = $data['visualization'];
- $vis_path = $static_path . 'vis/' . $vis['id'] . '-' . $vis['version'] . '.min.js';
- if (!file_exists($vis_path)) {
+ $vis_path = 'vis/' . $vis['id'] . '-' . $vis['version'] . '.min.js';
+ if (!file_exists($static_path . $vis_path)) {
$all = file_get_contents('../static/js/dw.js');
foreach ($data['visJS'] as $js) {
if (substr($js, 0, 7) != 'http://' && substr($js, 0, 2) != '//') {
$all .= "\n\n\n" . file_get_contents('..' . $js);
}
}
$all = JSMin::minify($all);
- file_put_contents($vis_path, $all);
+ file_put_contents($static_path . $vis_path, $all);
}
+ push_to_s3($static_path . $vis_path, 'lib/' . $vis_path, 'application/javascript');
// generate theme script
$theme = $data['theme'];
- $theme_path = $static_path . 'theme/' . $theme['id'] . '-' . $theme['version'] . '.min.js';
+ $theme_path = 'theme/' . $theme['id'] . '-' . $theme['version'] . '.min.js';
- if (!file_exists($theme_path)) {
+ if (!file_exists($static_path . $theme_path)) {
$all = '';
foreach ($data['themeJS'] as $js) {
if (substr($js, 0, 7) != 'http://' && substr($js, 0, 2) != '//') {
$all .= "\n\n\n" . file_get_contents('..' . $js);
}
}
$minified = JSMin::minify($all);
- file_put_contents($theme_path, $minified);
+ file_put_contents($static_path . $theme_path, $minified);
}
+ push_to_s3($static_path . $theme_path, 'lib/' . $theme_path, 'application/javascript');
ok();
} catch (Exception $e) {
@@ -317,9 +328,11 @@ function get_static_path($chart) {
}
$cssmin = new CSSmin();
- $minified = $cssmin->run($all);
+ $minified = $all; //$cssmin->run($all);
file_put_contents($static_path . "/" . $chart->getID() . '.min.css', $minified);
+ push_to_s3($static_path."/".$chart->getID().'.min.css', $chart->getID().'/'.$chart->getID().'.min.css', 'text/css');
+
// copy themes assets
$theme = $data['theme'];
if (isset($theme['assets'])) {
@@ -328,6 +341,7 @@ function get_static_path($chart) {
$asset_tgt = $static_path . "/" . $asset;
if (file_exists($asset_src)) {
file_put_contents($asset_tgt, file_get_contents($asset_src));
+ push_to_s3($asset_src, $chart->getID() . '/' . $asset);
}
}
}
@@ -347,6 +361,7 @@ function get_static_path($chart) {
try {
$static_path = get_static_path($chart);
file_put_contents($static_path . "/data", $chart->loadData());
+ push_to_s3($static_path . "/data", $chart->getID() . '/data');
$chart->setPublishedAt(time());
$chart->save();
ok();
@@ -30,11 +30,12 @@ function get_chart_content($chart, $user, $minified = false, $path = '') {
$abs = 'http://' . $GLOBALS['dw_config']['domain'];
$base_js = array(
- $abs . '/static/vendor/globalize/globalize.min.js',
// use CDN instead
+ // $abs . '/static/vendor/globalize/globalize.min.js',
// $abs . '/static/vendor/underscore/underscore-min.js',
// $abs . '/static/vendor/jquery/jquery.min.js'
+ '//assets.datawrapper.s3.amazonaws.com/globalize.min.js',
'//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js',
'//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.2/jquery.min.js'
);
@@ -159,15 +159,15 @@ $(function() {
{% endif %}
<h3>{% trans "Link to this visualisation" %}</h3>
- <p><a href="http://{{ DW_CHART_CACHE_DOMAIN }}/{{ chart.id }}/">http://{{ DW_CHART_CACHE_DOMAIN }}/{{ chart.id }}/</a></p>
+ <p><a href="{{ iframe }}">{{ iframe }}</a></p>
<h3>{% trans "Embed into your website" %}</h3>
<p>{% trans "Just copy &amp; paste the following code into your website." %}</p>
<form class="form-inline">
<label>{% trans "Width" %}: <input type="text" id="embed-width" class="input-small" style="width:4ex" value="600" /> px</label>&nbsp;&nbsp;
<label>{% trans "Height" %}: <input type="text" id="embed-height" class="input-small" style="width:4ex" value="400" /> px</label>
- <textarea id="iframe-code" class="span4" rows="5"><iframe src="http://{{ DW_CHART_CACHE_DOMAIN }}/{{ chart.id }}/" frameborder="0" allowtransparency="true" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen width="{{ chart.metadata.publish['embed-width'] }}" height="{{ chart.metadata.publish['embed-height'] }}"></iframe></textarea>
+ <textarea id="iframe-code" class="span4" rows="5"><iframe src="{{ iframe }}" frameborder="0" allowtransparency="true" allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen width="{{ chart.metadata.publish['embed-width'] }}" height="{{ chart.metadata.publish['embed-height'] }}"></iframe></textarea>
<h3>{% trans "Show in Gallery" %}</h3>
<p>{{ "You can show some love to Datawrapper by agreeing to show your visualisation in our <a href='%GALLERY_URL%'>public gallery</a>" | trans | replace({ '%GALLERY_URL%': '/gallery/' }) | raw }}. {% trans "This is absolutely optional." %}</p>
@@ -220,7 +220,7 @@ $(function() {
<div class="span8">
{% if user.isAbleToPublish %}
- <iframe src="http://{{ DW_CHART_CACHE_DOMAIN }}/{{ chart.id }}" id="iframe-vis" style="width:{{ chart.metadata.publish['embed-width'] }}px;height:{{ chart.metadata.publish['embed-height'] }}px"></iframe>
+ <iframe src="{{ iframe }}" id="iframe-vis" style="width:{{ chart.metadata.publish['embed-width'] }}px;height:{{ chart.metadata.publish['embed-height'] }}px"></iframe>
{% else %}
Oops, something went wrong.

0 comments on commit 66f44f6

Please sign in to comment.