Skip to content

Commit

Permalink
Fix:Shares not working with timeouts longer than 23 days #3164
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Jul 27, 2024
1 parent ee53086 commit 88693d7
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
12 changes: 7 additions & 5 deletions server/managers/ShareManager.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
const Database = require('../Database')
const Logger = require('../Logger')
const SocketAuthority = require('../SocketAuthority')
const LongTimeout = require('../utils/longTimeout')
const { elapsedPretty } = require('../utils/index')

/**
* @typedef OpenMediaItemShareObject
* @property {string} id
* @property {import('../models/MediaItemShare').MediaItemShareObject} mediaItemShare
* @property {NodeJS.Timeout} timeout
* @property {LongTimeout} timeout
*/

class ShareManager {
Expand Down Expand Up @@ -118,13 +120,13 @@ class ShareManager {
this.destroyMediaItemShare(mediaItemShare.id)
return
}

const timeout = setTimeout(() => {
const timeout = new LongTimeout()
timeout.set(() => {
Logger.info(`[ShareManager] Removing expired media item share "${mediaItemShare.id}"`)
this.removeMediaItemShare(mediaItemShare.id)
}, expiresAtDuration)
this.openMediaItemShares.push({ id: mediaItemShare.id, mediaItemShare: mediaItemShare.toJSON(), timeout })
Logger.info(`[ShareManager] Scheduled media item share "${mediaItemShare.id}" to expire in ${expiresAtDuration}ms`)
Logger.info(`[ShareManager] Scheduled media item share "${mediaItemShare.id}" to expire in ${elapsedPretty(expiresAtDuration / 1000)}`)
}

/**
Expand All @@ -149,7 +151,7 @@ class ShareManager {
if (!mediaItemShare) return

if (mediaItemShare.timeout) {
clearTimeout(mediaItemShare.timeout)
mediaItemShare.timeout.clear()
}

this.openMediaItemShares = this.openMediaItemShares.filter((s) => s.id !== mediaItemShareId)
Expand Down
26 changes: 21 additions & 5 deletions server/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,39 @@ module.exports.getId = (prepend = '') => {
return _id
}

/**
*
* @param {number} seconds
* @returns {string}
*/
function elapsedPretty(seconds) {
if (seconds > 0 && seconds < 1) {
return `${Math.floor(seconds * 1000)} ms`
}
if (seconds < 60) {
return `${Math.floor(seconds)} sec`
}
var minutes = Math.floor(seconds / 60)
let minutes = Math.floor(seconds / 60)
if (minutes < 70) {
return `${minutes} min`
}
var hours = Math.floor(minutes / 60)
let hours = Math.floor(minutes / 60)
minutes -= hours * 60
if (!minutes) {
return `${hours} hr`

let days = Math.floor(hours / 24)
hours -= days * 24

const timeParts = []
if (days) {
timeParts.push(`${days} d`)
}
if (hours || (days && minutes)) {
timeParts.push(`${hours} hr`)
}
if (minutes) {
timeParts.push(`${minutes} min`)
}
return `${hours} hr ${minutes} min`
return timeParts.join(' ')
}
module.exports.elapsedPretty = elapsedPretty

Expand Down
36 changes: 36 additions & 0 deletions server/utils/longTimeout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Handle timeouts greater than 32-bit signed integer
*/
class LongTimeout {
constructor() {
this.timeout = 0
this.timer = null
}

clear() {
clearTimeout(this.timer)
}

/**
*
* @param {Function} fn
* @param {number} timeout
*/
set(fn, timeout) {
const maxValue = 2147483647

const handleTimeout = () => {
if (this.timeout > 0) {
let delay = Math.min(this.timeout, maxValue)
this.timeout = this.timeout - delay
this.timer = setTimeout(handleTimeout, delay)
return
}
fn()
}

this.timeout = timeout
handleTimeout()
}
}
module.exports = LongTimeout

0 comments on commit 88693d7

Please sign in to comment.