Skip to content

Commit

Permalink
new feature: push charts to amazon s3
Browse files Browse the repository at this point in the history
  • Loading branch information
gka committed Nov 8, 2012
1 parent 165de2a commit 66f44f6
Show file tree
Hide file tree
Showing 8 changed files with 2,086 additions and 16 deletions.
9 changes: 8 additions & 1 deletion config.template.yaml
Expand Up @@ -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
8 changes: 8 additions & 0 deletions controller/chart-publish.php
Expand Up @@ -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');
Expand Down
35 changes: 25 additions & 10 deletions lib/api/charts.php
Expand Up @@ -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
*
Expand All @@ -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();
Expand All @@ -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());
Expand All @@ -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) {
Expand Down Expand Up @@ -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'])) {
Expand All @@ -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);
}
}
}
Expand All @@ -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();
Expand Down
3 changes: 2 additions & 1 deletion lib/utils/chart_content.php
Expand Up @@ -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'
);
Expand Down
6 changes: 3 additions & 3 deletions templates/chart-publish.twig
Expand Up @@ -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>
Expand Down Expand Up @@ -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 %}

Expand Down

0 comments on commit 66f44f6

Please sign in to comment.