Sha256: fded50330574f066598a5291a215ce8832fc1fd99affd965e61532f54302c659

Contents?: true

Size: 1.51 KB

Versions: 28

Compression:

Stored size: 1.51 KB

Contents

require "digest"

# 1. md5sum the file or zipfile - shared
# 2. add prefix and md5sum - shared, lets break out prefix adding logic to elsewhere

# Using md5sum on the zipped file will an always changing value since the timestamp and other metadata of the zipfile
# changes whenever we create it.
#
# This is custom checksum logic that only accounts for the contents of the files in the directory.
module Lono
  class Md5
    class << self
      extend Memoist

      # This checksum within the file name is to mainly make sure that Lambda::Function resources "change" and an update is triggered.
      # There's another checksum in the upload code that makes sure we don't upload the code again to speed up things.
      def sum(path)
        files = Dir["#{path}/**/*"]
        files = files.reject { |f| File.directory?(f) }
                     .reject { |f| File.symlink?(f) }
        files.sort!
        content = files.map do |f|
          Digest::MD5.file(f).to_s[0..7]
        end.join

        md5 = Digest::MD5.new
        md5.update(content)
        md5.hexdigest.to_s[0..7]
      end
      memoize :sum

      def name(path)
        relative_path = name_without_md5(path)
        relative_path = relative_path.sub(/\.(\w+)$/,'') # strip extension
        ext = File.directory?(path) ? "zip" : $1
        md5 = sum(path)
        "#{relative_path}-#{md5}.#{ext}"
      end

      def name_without_md5(path)
        regexp = %r{.*/output/[\w_-]+/files/}
        path.sub(regexp, '') # relative_path w/o md5
      end
    end
  end
end

Version data entries

28 entries across 28 versions & 1 rubygems

Version Path
lono-6.1.8 lib/lono/md5.rb
lono-6.1.7 lib/lono/md5.rb
lono-6.1.6 lib/lono/md5.rb
lono-6.1.5 lib/lono/md5.rb
lono-6.1.4 lib/lono/md5.rb
lono-6.1.3 lib/lono/md5.rb
lono-6.1.2 lib/lono/md5.rb
lono-6.1.1 lib/lono/md5.rb
lono-6.1.0 lib/lono/md5.rb
lono-6.0.1 lib/lono/md5.rb
lono-6.0.0 lib/lono/md5.rb
lono-5.3.4 lib/lono/md5.rb
lono-5.3.3 lib/lono/md5.rb
lono-5.3.2 lib/lono/md5.rb
lono-5.3.1 lib/lono/md5.rb
lono-5.3.0 lib/lono/md5.rb
lono-5.2.8 lib/lono/md5.rb
lono-5.2.7 lib/lono/md5.rb
lono-5.2.6 lib/lono/md5.rb
lono-5.2.5 lib/lono/md5.rb