2023-10-17 17:36:08 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class AssetSyncToken < ApplicationRecord
|
|
|
|
belongs_to :user
|
|
|
|
belongs_to :asset
|
|
|
|
|
|
|
|
after_initialize :generate_token
|
|
|
|
after_initialize :set_default_expiration
|
|
|
|
|
|
|
|
validates :token, uniqueness: true, presence: true
|
|
|
|
|
|
|
|
def version_token
|
2024-01-04 17:40:28 +08:00
|
|
|
asset.file.checksum
|
2023-10-17 17:36:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def token_valid?
|
|
|
|
!revoked_at? && expires_at > Time.current
|
|
|
|
end
|
|
|
|
|
|
|
|
def conflicts?(token)
|
2023-12-05 21:47:46 +08:00
|
|
|
asset.locked? || version_token != token
|
2023-10-17 17:36:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def generate_token
|
|
|
|
self.token ||= SecureRandom.urlsafe_base64(32)
|
|
|
|
end
|
|
|
|
|
|
|
|
def set_default_expiration
|
|
|
|
self.expires_at ||= Constants::ASSET_SYNC_TOKEN_EXPIRATION.from_now
|
|
|
|
end
|
|
|
|
end
|