lib/zold/copies.rb
def add(content, host, port, score, time: Time.now, master: false)
      raise "Content can't be empty" if content.empty?
      raise 'TCP port must be of type Integer' unless port.is_a?(Integer)
      raise "TCP port can't be negative: #{port}" if port.negative?
      raise 'Time must be of type Time' unless time.is_a?(Time)
      raise "Time must be in the past: #{time}" if time > Time.now
      raise 'Score must be Integer' unless score.is_a?(Integer)
      raise "Score can't be negative: #{score}" if score.negative?
      FileUtils.mkdir_p(@dir)
      Futex.new(file, log: @log).open do
        list = load
        target = list.find do |s|
          f = File.join(@dir, "#{s[:name]}#{Copies::EXT}")
          digest = OpenSSL::Digest::SHA256.new(content).hexdigest
          File.exist?(f) && OpenSSL::Digest::SHA256.file(f).hexdigest == digest
        end
        if target.nil?
          max = DirItems.new(@dir).fetch
            .select { |f| File.basename(f, Copies::EXT) =~ /^[0-9]+$/ }
            .map(&:to_i)
            .max
          max = 0 if max.nil?
          name = (max + 1).to_s
          IO.write(File.join(@dir, "#{name}#{Copies::EXT}"), content)
        else
          name = target[:name]
        end
        list.reject! { |s| s[:host] == host && s[:port] == port }
        list << {
          name: name,
          host: host,
          port: port,
          score: score,
          time: time,
          master: master
        }
        save(list)
        name
      end
    end
def add(content, host, port, score, time: Time.now, master: false)
      raise "Content can't be empty" if content.empty?
      raise 'TCP port must be of type Integer' unless port.is_a?(Integer)
      raise "TCP port can't be negative: #{port}" if port.negative?
      raise 'Time must be of type Time' unless time.is_a?(Time)
      raise "Time must be in the past: #{time}" if time > Time.now
      raise 'Score must be Integer' unless score.is_a?(Integer)
      raise "Score can't be negative: #{score}" if score.negative?
      FileUtils.mkdir_p(@dir)
      Futex.new(file, log: @log).open do
        list = load
        target = list.find do |s|
          f = File.join(@dir, "#{s[:name]}#{Copies::EXT}")
          digest = OpenSSL::Digest::SHA256.new(content).hexdigest
          File.exist?(f) && OpenSSL::Digest::SHA256.file(f).hexdigest == digest
        end
        if target.nil?
          max = DirItems.new(@dir).fetch
            .select { |f| File.basename(f, Copies::EXT) =~ /^[0-9]+$/ }
            .map(&:to_i)
            .max
          max = 0 if max.nil?
          name = (max + 1).to_s
          IO.write(File.join(@dir, "#{name}#{Copies::EXT}"), content)
        else
          name = target[:name]
        end
        list.reject! { |s| s[:host] == host && s[:port] == port }
        list << {
          name: name,
          host: host,
          port: port,
          score: score,
          time: time,
          master: master
        }
        save(list)
        name
      end
    end
def add ( content , host , port , score , time : Time . now , master : false ) raise "Content can't be empty" if content . empty? raise 'TCP port must be of type Integer' unless port . is_a? ( Integer ) raise "TCP port can't be negative: #{port}" if port . negative? raise 'Time must be of type Time' unless time . is_a? ( Time ) raise "Time must be in the past: #{time}" if time > Time . now raise 'Score must be Integer' unless score . is_a? ( Integer ) raise "Score can't be negative: #{score}" if score . negative? FileUtils . mkdir_p ( @dir ) Futex . new ( file , log : @log ) . open do list = load target = list . find do | s | f = File . join ( @dir , "#{s[:name]}#{Copies::EXT}" ) digest = OpenSSL :: Digest :: SHA256 . new ( content ) . hexdigest File . exist? ( f ) && OpenSSL :: Digest :: SHA256 . file ( f ) . hexdigest == digest end if target . nil? max = DirItems . new ( @dir ) . fetch . select { | f | File . basename ( f , Copies :: EXT ) =~ / / } . map ( :to_i ) . max max = 0 if max . nil? name = ( max + 1 ) . to_s IO . write ( File . join ( @dir , "#{name}#{Copies::EXT}" ) , content ) else name = target [ :name ] end list . reject! { | s | s [ :host ] == host && s [ :port ] == port } list << { name : name , host : host , port : port , score : score , time : time , master : master } save ( list ) name end end
Returns the name of the copy
Returns the name of the copy