Sha256: 6a23161cc49ac23400e7498d7c568d9532b0b0e83569b63d863e478a914b63bf

Contents?: true

Size: 1.9 KB

Versions: 8

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module Decidim
  module Cdtb
    module Storage
      # Given that all assets has already been copied from S3 to storage/,
      # this task performs the sharding of the downloaded files.
      #
      # This step is required because in S3 all assets are stored flat at the same level (directory),
      # but local service stores the files with sharding.
      class LocalSharding < ::Decidim::Cdtb::Task
        def initialize
          progress_bar= { title: "ActiveStorage::Blob" }
          super("S3 to local: DO SHARDING", progress_bar: progress_bar)
        end

        def prepare_execution(_ctx)
          @num_blobs= ActiveStorage::Blob.count
          log_task_info("Checking #{@num_blobs} blobs...")
        end

        def total_items
          @num_blobs
        end

        def do_execution(context)
          progress_bar= context[:progress_bar]

          ActiveStorage::Blob.find_each do |blob|
            path= ActiveStorage::Blob.service.path_for(blob.key)
            src_file= Rails.root.join("tmp/storage", blob.key)
            if File.exist?(src_file)
              shard_asset(blob, path)
              @num_applied+= 1
            else
              logger.warn "File Not Found or directory: #{path}"
            end
            progress_bar.increment
          end
        end

        def end_execution(_ctx)
          log_task_info("#{@num_applied} blobs sharded")
        end

        #----------------------------------------------------------------

        private

        #----------------------------------------------------------------

        def shard_asset(blob, path)
          blob_dir= File.dirname path
          logger.info "Creating dir: #{blob_dir}"
          FileUtils.mkdir_p(blob_dir)
          FileUtils.mv Rails.root.join("tmp/storage", blob.key), path, force: true
          logger.info "Sharding for file: #{path}"
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
decidim-cdtb-0.3.0 lib/decidim/cdtb/storage/local_sharding.rb
decidim-cdtb-0.2.1 lib/decidim/cdtb/storage/local_sharding.rb
decidim-cdtb-0.1.8 lib/decidim/cdtb/storage/local_sharding.rb
decidim-cdtb-0.1.7 lib/decidim/cdtb/storage/local_sharding.rb
decidim-cdtb-0.1.6 lib/decidim/cdtb/storage/local_sharding.rb
decidim-cdtb-0.1.4 lib/decidim/cdtb/storage/local_sharding.rb
decidim-cdtb-0.1.3 lib/decidim/cdtb/storage/local_sharding.rb
decidim-cdtb-0.1.2 lib/decidim/cdtb/storage/local_sharding.rb