2018-08-06 13:16:24 +00:00
|
|
|
import $ from 'jquery'
|
|
|
|
|
2018-08-06 21:15:23 +00:00
|
|
|
// https://stackoverflow.com/a/18650828
|
2019-04-16 18:17:17 +00:00
|
|
|
function formatBytes (a, b) {
|
2019-10-23 08:24:20 +00:00
|
|
|
if (a === 0) return '0 Bytes'
|
2019-04-16 18:17:17 +00:00
|
|
|
let c = 1024
|
|
|
|
let d = b || 2
|
|
|
|
let e = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
|
|
|
|
let f = Math.floor(Math.log(a) / Math.log(c))
|
|
|
|
return parseFloat((a / Math.pow(c, f)).toFixed(d)) + ' ' + e[f]
|
|
|
|
}
|
2018-08-06 21:15:23 +00:00
|
|
|
|
2019-04-16 18:17:17 +00:00
|
|
|
function recursiveStats (table, subtable) {
|
|
|
|
for (let key in table) {
|
|
|
|
let val = table[key]
|
2019-10-23 08:24:20 +00:00
|
|
|
if (typeof val === 'object') {
|
2019-04-16 18:17:17 +00:00
|
|
|
recursiveStats(val, key)
|
2018-08-06 21:15:23 +00:00
|
|
|
} else {
|
2019-04-16 18:17:17 +00:00
|
|
|
if (key === 'time') {
|
|
|
|
let date = new Date(null)
|
|
|
|
date.setSeconds(Math.floor(parseInt(val) / 1000)) // specify value for SECONDS here
|
|
|
|
val = date.toISOString().substr(11, 8)
|
|
|
|
} else if (key.indexOf('bytes') !== -1) {
|
2018-08-06 21:15:23 +00:00
|
|
|
val = formatBytes(val, 3)
|
|
|
|
}
|
2019-04-16 18:17:17 +00:00
|
|
|
$('#stat_' + (subtable ? subtable + '_' : '') + key).text(val)
|
2018-08-06 21:15:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 10:43:31 +00:00
|
|
|
function updateLinkList (k) {
|
|
|
|
if (k && k.error) return alert(k.error)
|
|
|
|
$.get('/dashboard/link', function (res) {
|
|
|
|
if (res.error) return
|
|
|
|
$('#link-list').html('<tbody></tbody>')
|
|
|
|
$('#link-list tbody').append('<tr><th>Name</th><th>URL</th><th>Action</th></tr>')
|
|
|
|
|
|
|
|
for (let i in res) {
|
|
|
|
let p = res[i]
|
|
|
|
p.name = p.name.replace(/\</g, '<').replace(/\>/g, '>')
|
|
|
|
$('#link-list tbody').append('<tr data-url="' + p.url + '"><td>' + p.name +'</td><td>' +
|
|
|
|
'<a href="' + p.url +'" target="_blank" rel="nofollow">' +
|
|
|
|
p.url + '</a></td><td><a href="#" class="delete-link">Remove</a></td></tr>')
|
|
|
|
}
|
|
|
|
|
|
|
|
$('.delete-link').click(function (e) {
|
|
|
|
e.preventDefault()
|
|
|
|
let pr = $(this).parent().parent().attr('data-url')
|
|
|
|
$.post('/dashboard/link/delete', { url: pr }, updateLinkList)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-16 18:17:17 +00:00
|
|
|
function dashboard (k) {
|
|
|
|
$.get('/dashboard/data', function (res) {
|
2018-08-06 13:16:24 +00:00
|
|
|
if (res.error) {
|
2019-04-16 18:17:17 +00:00
|
|
|
window.location.href = '/'
|
2018-08-06 13:16:24 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-04-16 18:17:17 +00:00
|
|
|
let fullURL = window.location.origin + '/watch/' + res.name
|
2018-08-06 13:16:24 +00:00
|
|
|
$('#myStream').attr('src', fullURL)
|
2019-04-16 18:17:17 +00:00
|
|
|
$('#stream_url').text(fullURL).attr('href', fullURL)
|
2018-08-06 21:15:23 +00:00
|
|
|
$('#stream_live').text(res.live ? 'Yes' : 'No')
|
2018-08-06 13:16:24 +00:00
|
|
|
})
|
|
|
|
|
2019-10-23 08:24:20 +00:00
|
|
|
$('#show_key').click(function (e) {
|
|
|
|
e.preventDefault()
|
2019-04-16 18:17:17 +00:00
|
|
|
$('#show_key').html(k)
|
2018-08-06 13:16:24 +00:00
|
|
|
})
|
2018-08-06 21:15:23 +00:00
|
|
|
|
2019-10-23 10:43:31 +00:00
|
|
|
$('.go-page').click(function (e) {
|
|
|
|
let el = $(this)
|
|
|
|
$('.go-page').removeClass('active')
|
|
|
|
el.addClass('active')
|
|
|
|
$('.page:visible').fadeOut(function () {
|
|
|
|
$('#page-' + el.attr('data-page')).fadeIn()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
$('#add-link').submit(function (e) {
|
|
|
|
e.preventDefault()
|
|
|
|
let name = $('input[name="name"]').val()
|
|
|
|
let url = $('input[name="url"]').val()
|
|
|
|
|
|
|
|
if (name.length > 120) return alert('Only 120 characters are allowed in the name.')
|
|
|
|
|
|
|
|
$.post('/dashboard/link', { name, url }, function () {
|
|
|
|
$('input[name="name"]').val('')
|
|
|
|
$('input[name="url"]').val('')
|
|
|
|
updateLinkList()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2018-08-06 21:15:23 +00:00
|
|
|
setInterval(function () {
|
2019-04-16 18:17:17 +00:00
|
|
|
$.get('/dashboard/stats', function (res) {
|
2018-08-06 21:15:23 +00:00
|
|
|
if (res.error) return
|
2019-04-16 18:17:17 +00:00
|
|
|
recursiveStats(res, '')
|
2018-08-06 21:15:23 +00:00
|
|
|
})
|
|
|
|
}, 5000)
|
2019-10-23 10:43:31 +00:00
|
|
|
|
|
|
|
updateLinkList()
|
|
|
|
|
|
|
|
let hash = window.location.hash
|
|
|
|
if (hash.indexOf('#') === 0) hash = hash.substr(1)
|
|
|
|
if ($('#page-' + hash).length) {
|
|
|
|
$('.go-page').removeClass('active')
|
|
|
|
$('.go-page[data-page="' + hash + '"').addClass('active')
|
|
|
|
$('.page:visible').hide()
|
|
|
|
$('.page#page-' + hash).show()
|
|
|
|
}
|
2018-08-06 13:16:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 18:17:17 +00:00
|
|
|
export default dashboard
|