Class: Service::PostgreSQLService

Inherits:
Service
  • Object
show all
Defined in:
lib/active_storage/service/postgresql_service.rb

Overview

Wraps a PostgreSQL database as an Active Storage service. See ActiveStorage::Service for the generic API documentation that applies to all services.

Instance Method Summary collapse

Constructor Details

#initialize(public: false, **options) ⇒ PostgreSQLService

Returns a new instance of PostgreSQLService.



9
10
11
# File 'lib/active_storage/service/postgresql_service.rb', line 9

def initialize(public: false, **options)
  @public = public
end

Instance Method Details

#delete(key) ⇒ Object



46
47
48
49
50
# File 'lib/active_storage/service/postgresql_service.rb', line 46

def delete(key)
  instrument :delete, key: key do
    ActiveStorage::PostgreSQL::File.find_by(key: key).try(&:destroy)
  end
end

#delete_prefixed(prefix) ⇒ Object



60
61
62
63
64
# File 'lib/active_storage/service/postgresql_service.rb', line 60

def delete_prefixed(prefix)
  instrument :delete_prefixed, prefix: prefix do
    ActiveStorage::PostgreSQL::File.prefixed_with(prefix).destroy_all
  end
end

#download(key) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/active_storage/service/postgresql_service.rb', line 19

def download(key)
  if block_given?
    instrument :streaming_download, key: key do
      ActiveStorage::PostgreSQL::File.open(key) do |file|
        while data = file.read(5.megabytes)
          yield data
        end
      end
    end
  else
    instrument :download, key: key do
      ActiveStorage::PostgreSQL::File.open(key) do |file|
        file.read
      end
    end
  end
end

#download_chunk(key, range) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/active_storage/service/postgresql_service.rb', line 37

def download_chunk(key, range)
  instrument :download_chunk, key: key, range: range do
    ActiveStorage::PostgreSQL::File.open(key) do |file|
      file.seek(range.first)
      file.read(range.size)
    end
  end
end

#exist?(key) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/active_storage/service/postgresql_service.rb', line 52

def exist?(key)
  instrument :exist, key: key do |payload|
    answer = ActiveStorage::PostgreSQL::File.where(key: key).exists?
    payload[:exist] = answer
    answer
  end
end

#generate_url(key, expires_in:, filename:, disposition:, content_type:) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/active_storage/service/postgresql_service.rb', line 84

def generate_url(key, expires_in:, filename:, disposition:, content_type:)
  instrument :url, key: key do |payload|
    content_disposition = content_disposition_with(type: disposition, filename: filename)
    verified_key_with_expiration = ActiveStorage.verifier.generate(
      {
        key: key,
        disposition: content_disposition,
        content_type: content_type
      },
      expires_in: expires_in,
      purpose: :blob_key
    )

    generated_url = url_helpers.rails_postgresql_service_url(verified_key_with_expiration,
      **url_options,
      disposition: content_disposition,
      content_type: content_type,
      filename: filename
    )
    payload[:url] = generated_url

    generated_url
  end
end

#headers_for_direct_upload(key, content_type:) ⇒ Object



130
131
132
# File 'lib/active_storage/service/postgresql_service.rb', line 130

def headers_for_direct_upload(key, content_type:, **)
  { "Content-Type" => content_type }
end

#private_url(key, expires_in:, filename:, content_type:, disposition:) ⇒ Object



66
67
68
# File 'lib/active_storage/service/postgresql_service.rb', line 66

def private_url(key, expires_in:, filename:, content_type:, disposition:, **)
  generate_url(key, expires_in: expires_in, filename: filename, content_type: content_type, disposition: disposition)
end

#public_url(key, filename:, content_type: nil, disposition: :attachment) ⇒ Object



70
71
72
# File 'lib/active_storage/service/postgresql_service.rb', line 70

def public_url(key, filename:, content_type: nil, disposition: :attachment, **)
  generate_url(key, expires_in: nil, filename: filename, content_type: content_type, disposition: disposition)
end

#upload(key, io, checksum: nil) ⇒ Object



13
14
15
16
17
# File 'lib/active_storage/service/postgresql_service.rb', line 13

def upload(key, io, checksum: nil, **)
  instrument :upload, key: key, checksum: checksum do
    ActiveStorage::PostgreSQL::File.create!(key: key, io: io, checksum: checksum)
  end
end

#url(key, **options) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/active_storage/service/postgresql_service.rb', line 74

def url(key, **options)
  super
rescue NotImplementedError, ArgumentError
  if @public
    public_url(key, **options)
  else
    private_url(key, **options)
  end
end

#url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/active_storage/service/postgresql_service.rb', line 109

def url_for_direct_upload(key, expires_in:, content_type:, content_length:, checksum:, custom_metadata: {})
  instrument :url, key: key do |payload|
    verified_token_with_expiration = ActiveStorage.verifier.generate(
      {
        key: key,
        content_type: content_type,
        content_length: content_length,
        checksum: checksum
      },
      expires_in: expires_in,
      purpose: :blob_token
    )

    generated_url = url_helpers.update_rails_postgresql_service_url(verified_token_with_expiration, **url_options)

    payload[:url] = generated_url

    generated_url
  end
end