generalize date string handling, replacing hardcoded lookups, fixes #586
This commit is contained in:
parent
b0800060c2
commit
f391773c65
128
js/privatebin.js
128
js/privatebin.js
|
@ -209,6 +209,64 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
'=': '='
|
'=': '='
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* number of seconds in a minute
|
||||||
|
*
|
||||||
|
* @name Helper.minute
|
||||||
|
* @private
|
||||||
|
* @enum {number}
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
const minute = 60;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* number of seconds in an hour
|
||||||
|
*
|
||||||
|
* = 60 * 60 seconds
|
||||||
|
*
|
||||||
|
* @name Helper.minute
|
||||||
|
* @private
|
||||||
|
* @enum {number}
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
const hour = 3600;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* number of seconds in a day
|
||||||
|
*
|
||||||
|
* = 60 * 60 * 24 seconds
|
||||||
|
*
|
||||||
|
* @name Helper.day
|
||||||
|
* @private
|
||||||
|
* @enum {number}
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
const day = 86400;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* number of seconds in a month (30 days, an approximation)
|
||||||
|
*
|
||||||
|
* = 60 * 60 * 24 * 30 seconds
|
||||||
|
*
|
||||||
|
* @name Helper.month
|
||||||
|
* @private
|
||||||
|
* @enum {number}
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
const month = 2592000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* number of seconds in a non-leap year
|
||||||
|
*
|
||||||
|
* = 60 * 60 * 24 * 365 seconds
|
||||||
|
*
|
||||||
|
* @name Helper.year
|
||||||
|
* @private
|
||||||
|
* @enum {number}
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
const year = 31536000;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* cache for script location
|
* cache for script location
|
||||||
*
|
*
|
||||||
|
@ -229,31 +287,67 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
me.secondsToHuman = function(seconds)
|
me.secondsToHuman = function(seconds)
|
||||||
{
|
{
|
||||||
let v;
|
let v;
|
||||||
if (seconds < 60)
|
if (seconds < minute)
|
||||||
{
|
{
|
||||||
v = Math.floor(seconds);
|
v = Math.floor(seconds);
|
||||||
return [v, 'second'];
|
return [v, 'second'];
|
||||||
}
|
}
|
||||||
if (seconds < 60 * 60)
|
if (seconds < hour)
|
||||||
{
|
{
|
||||||
v = Math.floor(seconds / 60);
|
v = Math.floor(seconds / minute);
|
||||||
return [v, 'minute'];
|
return [v, 'minute'];
|
||||||
}
|
}
|
||||||
if (seconds < 60 * 60 * 24)
|
if (seconds < day)
|
||||||
{
|
{
|
||||||
v = Math.floor(seconds / (60 * 60));
|
v = Math.floor(seconds / hour);
|
||||||
return [v, 'hour'];
|
return [v, 'hour'];
|
||||||
}
|
}
|
||||||
// If less than 2 months, display in days:
|
// If less than 2 months, display in days:
|
||||||
if (seconds < 60 * 60 * 24 * 60)
|
if (seconds < (2 * month))
|
||||||
{
|
{
|
||||||
v = Math.floor(seconds / (60 * 60 * 24));
|
v = Math.floor(seconds / day);
|
||||||
return [v, 'day'];
|
return [v, 'day'];
|
||||||
}
|
}
|
||||||
v = Math.floor(seconds / (60 * 60 * 24 * 30));
|
v = Math.floor(seconds / month);
|
||||||
return [v, 'month'];
|
return [v, 'month'];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* converts a duration string into seconds
|
||||||
|
*
|
||||||
|
* The string is expected to be optional digits, followed by a time.
|
||||||
|
* Supported times are: min, hour, day, month, year, never
|
||||||
|
* Examples: 5min, 13hour, never
|
||||||
|
*
|
||||||
|
* @name Helper.durationToSeconds
|
||||||
|
* @function
|
||||||
|
* @param {String} duration
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
me.durationToSeconds = function(duration)
|
||||||
|
{
|
||||||
|
let pieces = duration.split(/\d+/),
|
||||||
|
factor = pieces[0] || 0,
|
||||||
|
timespan = pieces[1] || pieces[0];
|
||||||
|
switch (timespan)
|
||||||
|
{
|
||||||
|
case 'min':
|
||||||
|
return factor * minute;
|
||||||
|
case 'hour':
|
||||||
|
return factor * hour;
|
||||||
|
case 'day':
|
||||||
|
return factor * day;
|
||||||
|
case 'month':
|
||||||
|
return factor * month;
|
||||||
|
case 'year':
|
||||||
|
return factor * year;
|
||||||
|
case 'never':
|
||||||
|
return 0;
|
||||||
|
default:
|
||||||
|
return factor;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* text range selection
|
* text range selection
|
||||||
*
|
*
|
||||||
|
@ -432,22 +526,10 @@ jQuery.PrivateBin = (function($, RawDeflate) {
|
||||||
* @return {Date}
|
* @return {Date}
|
||||||
*/
|
*/
|
||||||
me.calculateExpirationDate = function(initialDate, expirationDisplayStringOrSecondsToExpire) {
|
me.calculateExpirationDate = function(initialDate, expirationDisplayStringOrSecondsToExpire) {
|
||||||
let expirationDate = new Date(initialDate);
|
let expirationDate = new Date(initialDate),
|
||||||
|
secondsToExpiration = expirationDisplayStringOrSecondsToExpire;
|
||||||
const expirationDisplayStringToSecondsDict = {
|
|
||||||
'5min': 300,
|
|
||||||
'10min': 600,
|
|
||||||
'1hour': 3500,
|
|
||||||
'1day': 86400,
|
|
||||||
'1week': 604800,
|
|
||||||
'1month': 2592000,
|
|
||||||
'1year': 31536000,
|
|
||||||
'never': 0
|
|
||||||
};
|
|
||||||
|
|
||||||
let secondsToExpiration = expirationDisplayStringOrSecondsToExpire;
|
|
||||||
if (typeof expirationDisplayStringOrSecondsToExpire === 'string') {
|
if (typeof expirationDisplayStringOrSecondsToExpire === 'string') {
|
||||||
secondsToExpiration = expirationDisplayStringToSecondsDict[expirationDisplayStringOrSecondsToExpire];
|
secondsToExpiration = me.durationToSeconds(expirationDisplayStringOrSecondsToExpire);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof secondsToExpiration !== 'number') {
|
if (typeof secondsToExpiration !== 'number') {
|
||||||
|
|
|
@ -72,7 +72,7 @@ endif;
|
||||||
?>
|
?>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/purify-2.0.8.js" integrity="sha512-QwcEKGuEmKtMguCO9pqNtUtZqq9b/tJ8gNr5qhY8hykq3zKTlDOvpZAmf6Rs8yH35Bz1ZdctUjj2qEWxT5aXCg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/purify-2.0.8.js" integrity="sha512-QwcEKGuEmKtMguCO9pqNtUtZqq9b/tJ8gNr5qhY8hykq3zKTlDOvpZAmf6Rs8yH35Bz1ZdctUjj2qEWxT5aXCg==" crossorigin="anonymous"></script>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/legacy.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/legacy.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==" crossorigin="anonymous"></script>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-enOoc3FEmX00nbC+28Qrhjc2shbso/DWmeHVbLDy+a0jvXXweYXCr/B1PRqnXJzTBdPqVBYLVM1u6peVlTwNxg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-5GFThJ8KstWT1bNvB5JTAAXA+5QCNDv21foF7hSNoAc0oOxrHiUCP1ZlZs9zk4SbdIsmTSGL12Ecdj5CRISYxg==" crossorigin="anonymous"></script>
|
||||||
<link rel="apple-touch-icon" href="img/apple-touch-icon.png?<?php echo rawurlencode($VERSION); ?>" sizes="180x180" />
|
<link rel="apple-touch-icon" href="img/apple-touch-icon.png?<?php echo rawurlencode($VERSION); ?>" sizes="180x180" />
|
||||||
<link rel="icon" type="image/png" href="img/favicon-32x32.png?<?php echo rawurlencode($VERSION); ?>" sizes="32x32" />
|
<link rel="icon" type="image/png" href="img/favicon-32x32.png?<?php echo rawurlencode($VERSION); ?>" sizes="32x32" />
|
||||||
<link rel="icon" type="image/png" href="img/favicon-16x16.png?<?php echo rawurlencode($VERSION); ?>" sizes="16x16" />
|
<link rel="icon" type="image/png" href="img/favicon-16x16.png?<?php echo rawurlencode($VERSION); ?>" sizes="16x16" />
|
||||||
|
|
|
@ -50,7 +50,7 @@ endif;
|
||||||
?>
|
?>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/purify-2.0.8.js" integrity="sha512-QwcEKGuEmKtMguCO9pqNtUtZqq9b/tJ8gNr5qhY8hykq3zKTlDOvpZAmf6Rs8yH35Bz1ZdctUjj2qEWxT5aXCg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/purify-2.0.8.js" integrity="sha512-QwcEKGuEmKtMguCO9pqNtUtZqq9b/tJ8gNr5qhY8hykq3zKTlDOvpZAmf6Rs8yH35Bz1ZdctUjj2qEWxT5aXCg==" crossorigin="anonymous"></script>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/legacy.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/legacy.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-LYos+qXHIRqFf5ZPNphvtTB0cgzHUizu2wwcOwcwz/VIpRv9lpcBgPYz4uq6jx0INwCAj6Fbnl5HoKiLufS2jg==" crossorigin="anonymous"></script>
|
||||||
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-enOoc3FEmX00nbC+28Qrhjc2shbso/DWmeHVbLDy+a0jvXXweYXCr/B1PRqnXJzTBdPqVBYLVM1u6peVlTwNxg==" crossorigin="anonymous"></script>
|
<script type="text/javascript" data-cfasync="false" src="js/privatebin.js?<?php echo rawurlencode($VERSION); ?>" integrity="sha512-5GFThJ8KstWT1bNvB5JTAAXA+5QCNDv21foF7hSNoAc0oOxrHiUCP1ZlZs9zk4SbdIsmTSGL12Ecdj5CRISYxg==" crossorigin="anonymous"></script>
|
||||||
<link rel="apple-touch-icon" href="img/apple-touch-icon.png?<?php echo rawurlencode($VERSION); ?>" sizes="180x180" />
|
<link rel="apple-touch-icon" href="img/apple-touch-icon.png?<?php echo rawurlencode($VERSION); ?>" sizes="180x180" />
|
||||||
<link rel="icon" type="image/png" href="img/favicon-32x32.png?<?php echo rawurlencode($VERSION); ?>" sizes="32x32" />
|
<link rel="icon" type="image/png" href="img/favicon-32x32.png?<?php echo rawurlencode($VERSION); ?>" sizes="32x32" />
|
||||||
<link rel="icon" type="image/png" href="img/favicon-16x16.png?<?php echo rawurlencode($VERSION); ?>" sizes="16x16" />
|
<link rel="icon" type="image/png" href="img/favicon-16x16.png?<?php echo rawurlencode($VERSION); ?>" sizes="16x16" />
|
||||||
|
|
Loading…
Reference in New Issue