Class: ActiveStorageDashboard::BlobsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/active_storage_dashboard/blobs_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#paginate

Instance Method Details

#downloadObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/controllers/active_storage_dashboard/blobs_controller.rb', line 25

def download
  @blob = ActiveStorage::Blob.find(params[:id])
  
  # Determine the disposition (inline for preview, attachment for download)
  # Params can be route params or query params
  disposition = params[:disposition] || 'attachment'
  
  # Different approaches depending on Rails version
  if @blob.respond_to?(:open)
    # Rails 6.0+: Use the open method to get the file
    begin
      @blob.open do |file|
        send_data file.read, 
                  filename: @blob.filename.to_s, 
                  type: @blob.content_type || 'application/octet-stream', 
                  disposition: disposition
      end
    rescue => e
      Rails.logger.error("Failed to download blob: #{e.message}")
      redirect_to blob_path(@blob), alert: "Download failed: #{e.message}"
    end
  elsif @blob.respond_to?(:download)
    # Alternative approach: Use the download method
    begin
      send_data @blob.download, 
                filename: @blob.filename.to_s, 
                type: @blob.content_type || 'application/octet-stream', 
                disposition: disposition
    rescue => e
      Rails.logger.error("Failed to download blob: #{e.message}")
      redirect_to blob_path(@blob), alert: "Download failed: #{e.message}"
    end
  else
    # Fallback: Redirect to main app blob path
    disposition_param = disposition == 'inline' ? { disposition: 'inline' } : { disposition: 'attachment' }
    redirect_to main_app.rails_blob_path(@blob, disposition_param)
  end
end

#indexObject



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'app/controllers/active_storage_dashboard/blobs_controller.rb', line 5

def index
  @blobs = ActiveStorage::Blob.order(created_at: :desc)
  
  # Get content types for filter dropdown
  @content_types = ActiveStorage::Blob.distinct.pluck(:content_type).compact.sort
  
  # Apply filters
  apply_filters

  # Pagination after filters
  @total_count = @blobs.count
  @blobs = paginate(@blobs)
end

#showObject



19
20
21
22
23
# File 'app/controllers/active_storage_dashboard/blobs_controller.rb', line 19

def show
  @blob = ActiveStorage::Blob.find(params[:id])
  @attachments = @blob.attachments
  @variant_records = defined?(ActiveStorage::VariantRecord) ? @blob.variant_records : []
end