-
Notifications
You must be signed in to change notification settings - Fork 8.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Url shortener #5497
Url shortener #5497
Changes from 7 commits
25c6627
5783f31
7fcef6b
cef5afd
1049279
0fb1450
e59750c
96b7404
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,4 @@ | ||
<form role="form" class="vis-share"> | ||
|
||
<p> | ||
<div class="input-group"> | ||
<label> | ||
Embed this dashboard | ||
<small>Add to your html source. Note all clients must still be able to access kibana</small> | ||
</label> | ||
<div class="form-control" disabled>{{opts.shareData().embed}}</div> | ||
</div> | ||
</p> | ||
|
||
<p> | ||
<div class="input-group"> | ||
<label> | ||
Share a link | ||
</label> | ||
<div class="form-control" disabled>{{opts.shareData().link}}</div> | ||
</div> | ||
</p> | ||
</form> | ||
<share | ||
object-type="dashboard" | ||
object-id="{{opts.dashboard.id}}"> | ||
</share> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<share | ||
object-type="search" | ||
object-id="{{opts.savedSearch.id}}" | ||
allow-embed="false"> | ||
</share> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,4 @@ | ||
<form role="form" class="vis-share"> | ||
|
||
<p> | ||
<div class="form-group"> | ||
<label> | ||
Embed this visualization. | ||
<small>Add to your html source. Note all clients must still be able to access kibana</small> | ||
</label> | ||
<div class="form-control" disabled>{{conf.shareData().embed}}</div> | ||
</div> | ||
</p> | ||
|
||
<p> | ||
<div class="form-group"> | ||
<label> | ||
Share a link | ||
</label> | ||
<div class="form-control" disabled>{{conf.shareData().link}}</div> | ||
</div> | ||
</p> | ||
|
||
</form> | ||
<share | ||
object-type="visualization" | ||
object-id="{{conf.savedVis.id}}"> | ||
</share> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
const crypto = require('crypto'); | ||
|
||
export default function (server) { | ||
async function updateMetadata(urlId, urlDoc) { | ||
const client = server.plugins.elasticsearch.client; | ||
|
||
try { | ||
await client.update({ | ||
index: '.kibana', | ||
type: 'url', | ||
id: urlId, | ||
body: { | ||
doc: { | ||
'accessDate': new Date(), | ||
'accessCount': urlDoc._source.accessCount + 1 | ||
} | ||
} | ||
}); | ||
} catch (err) { | ||
console.log('Warning: Error updating url metadata', err); | ||
//swallow errors. It isn't critical if there is no update. | ||
} | ||
} | ||
|
||
async function getUrlDoc(urlId) { | ||
const urlDoc = await new Promise((resolve, reject) => { | ||
const client = server.plugins.elasticsearch.client; | ||
|
||
client.get({ | ||
index: '.kibana', | ||
type: 'url', | ||
id: urlId | ||
}) | ||
.then(response => { | ||
resolve(response); | ||
}) | ||
.catch(err => { | ||
resolve(); | ||
}); | ||
}); | ||
|
||
return urlDoc; | ||
} | ||
|
||
async function createUrlDoc(url, urlId) { | ||
const newUrlId = await new Promise((resolve, reject) => { | ||
const client = server.plugins.elasticsearch.client; | ||
|
||
client.index({ | ||
index: '.kibana', | ||
type: 'url', | ||
id: urlId, | ||
body: { | ||
url, | ||
'accessCount': 0, | ||
'createDate': new Date(), | ||
'accessDate': new Date() | ||
} | ||
}) | ||
.then(response => { | ||
resolve(response._id); | ||
}) | ||
.catch(err => { | ||
reject(err); | ||
}); | ||
}); | ||
|
||
return newUrlId; | ||
} | ||
|
||
function createUrlId(url) { | ||
const urlId = crypto.createHash('md5') | ||
.update(url) | ||
.digest('hex'); | ||
|
||
return urlId; | ||
} | ||
|
||
return { | ||
async generateUrlId(url) { | ||
const urlId = createUrlId(url); | ||
|
||
const urlDoc = await getUrlDoc(urlId); | ||
if (urlDoc) return urlId; | ||
|
||
return createUrlDoc(url, urlId); | ||
}, | ||
async getUrl(urlId) { | ||
try { | ||
const urlDoc = await getUrlDoc(urlId); | ||
if (!urlDoc) throw new Error('Requested shortened url does note exist in kibana index'); | ||
|
||
updateMetadata(urlId, urlDoc); | ||
|
||
return urlDoc._source.url; | ||
} catch (err) { | ||
return '/'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. thoughts on letting the server throw instead of redirecting to /? similar to how url/app/asdf will throw an invalid app error, or other pages give 404s |
||
} | ||
} | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const app = require('ui/modules').get('kibana'); | ||
|
||
app.directive('share', function () { | ||
return { | ||
restrict: 'E', | ||
scope: { | ||
objectType: '@', | ||
objectId: '@', | ||
setAllowEmbed: '&?allowEmbed' | ||
}, | ||
template: require('ui/share/views/share.html'), | ||
controller: function ($scope) { | ||
$scope.allowEmbed = $scope.setAllowEmbed ? $scope.setAllowEmbed() : true; | ||
} | ||
}; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
const app = require('ui/modules').get('kibana'); | ||
const Clipboard = require('clipboard'); | ||
|
||
require('../styles/index.less'); | ||
|
||
app.directive('shareObjectUrl', function (Private, Notifier) { | ||
const urlShortener = Private(require('../lib/url_shortener')); | ||
|
||
return { | ||
restrict: 'E', | ||
scope: { | ||
getShareAsEmbed: '&shareAsEmbed' | ||
}, | ||
template: require('ui/share/views/share_object_url.html'), | ||
link: function ($scope, $el) { | ||
const notify = new Notifier({ | ||
location: `Share ${$scope.$parent.objectType}` | ||
}); | ||
|
||
$scope.textbox = $el.find('input.url')[0]; | ||
$scope.clipboardButton = $el.find('button.clipboard-button')[0]; | ||
|
||
const clipboard = new Clipboard($scope.clipboardButton, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this need to be cleaned up on destroy? |
||
target: function (trigger) { | ||
return $scope.textbox; | ||
} | ||
}); | ||
|
||
clipboard.on('success', function (e) { | ||
notify.info('URL copied to clipboard.'); | ||
e.clearSelection(); | ||
}); | ||
|
||
clipboard.on('error', function (e) { | ||
notify.info('URL selected. Press Ctrl+C to copy.'); | ||
}); | ||
|
||
$scope.clipboard = clipboard; | ||
}, | ||
controller: function ($scope, $location) { | ||
function updateUrl(url) { | ||
$scope.url = url; | ||
|
||
if ($scope.shareAsEmbed) { | ||
$scope.formattedUrl = `<iframe src="${$scope.url}" height="600" width="800"></iframe>`; | ||
} else { | ||
$scope.formattedUrl = $scope.url; | ||
} | ||
|
||
$scope.shortGenerated = false; | ||
} | ||
|
||
$scope.shareAsEmbed = $scope.getShareAsEmbed(); | ||
|
||
$scope.generateShortUrl = function () { | ||
if ($scope.shortGenerated) return; | ||
|
||
urlShortener.shortenUrl($scope.url) | ||
.then(shortUrl => { | ||
updateUrl(shortUrl); | ||
$scope.shortGenerated = true; | ||
}); | ||
}; | ||
|
||
$scope.getUrl = function () { | ||
return $location.absUrl(); | ||
}; | ||
|
||
$scope.$watch('getUrl()', updateUrl); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we use $on('$locationChangeSuccess' instead? |
||
} | ||
}; | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require('./directives/share'); | ||
require('./directives/share_object_url'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should this use server.log?