Class: ActiveCanvas::Media

Inherits:
ApplicationRecord show all
Defined in:
app/models/active_canvas/media.rb

Instance Method Summary collapse

Instance Method Details

#as_json_for_editorObject



60
61
62
63
64
65
66
67
68
69
# File 'app/models/active_canvas/media.rb', line 60

def as_json_for_editor
  {
    id: id,
    src: url,
    name: filename,
    type: content_type,
    width: ["width"],
    height: ["height"]
  }
end

#metadataObject



23
24
25
# File 'app/models/active_canvas/media.rb', line 23

def 
  super || {}
end

#public_urlObject



48
49
50
51
52
53
54
55
56
57
58
# File 'app/models/active_canvas/media.rb', line 48

def public_url
  return nil unless file.attached?

  # Returns the direct public URL for cloud storage
  # or the rails blob URL for local storage
  if file.blob.service.respond_to?(:url)
    file.url(expires_in: nil) rescue file.url
  else
    url
  end
end

#urlObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/active_canvas/media.rb', line 27

def url
  return nil unless file.attached?

  # If public_uploads is enabled and blob service supports public URLs
  if ActiveCanvas.config.public_uploads &&
     file.blob.service.respond_to?(:public?) &&
     file.blob.service.public?
    file.url
  else
    # Use signed URL with expiration for better security
    Rails.application.routes.url_helpers.rails_blob_url(
      file,
      expires_in: 1.hour,
      only_path: true
    )
  end
rescue ArgumentError
  # Fallback for services that don't support expires_in
  Rails.application.routes.url_helpers.rails_blob_url(file, only_path: true)
end