postgresql_lo_streamer 1.2.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0b15445e68664011f4a6c5004276249dfc86c5dc
4
- data.tar.gz: 65d66610979783285de42eb754f81bd1724d1fde
3
+ metadata.gz: 9ed951b55d69c47c78e0bb532c588d767f103001
4
+ data.tar.gz: 6d459d8a27c0fdbc555c2969ff85bad967e22ad7
5
5
  SHA512:
6
- metadata.gz: 339e58f1db995b003206e860a20c29649e9d1b90059372d99cec8bef585ac39e6b423d04675c7b4a02349f11deb4e6f6f4aa3d86613f770f23d4eca0cd2fd54c
7
- data.tar.gz: ff139796cfc79a93b34c0cd12129845629ff0a721672072bf7385bf6f76445ad22174d7a3347fd0128bbfffbf5f6b8bb7baea85492c89f11f28bebacf898e63a
6
+ metadata.gz: bca54aa210afd1b84ceb633e1a9c8ef90e53daa4419a1590ea23bdd2de148047d2b14ebdaa41c48d6dc92fb96a97f85df35404de001f0d2b913cb2aed962bec3
7
+ data.tar.gz: 22c17d889d92c8f1b42d981597155ecde4587d224d67c17be35075fcee2c5d228abe30fb43384df96b8217184698f432c128092c4d5a45152d57f1025e22fe63
@@ -2,25 +2,21 @@
2
2
 
3
3
  module PostgresqlLoStreamer
4
4
  class LoController < ActionController::Base
5
+
5
6
  def stream
6
- send_file_headers!(headers_for_extension_or_default)
7
7
 
8
- object_identifier = params[:id].to_i
9
- if !object_exists?(object_identifier)
10
- self.status = 404
11
- render :nothing => true and return
8
+ streamer = PostgresqlLoStreamer::Streamer.new(
9
+ connection,
10
+ params[:id].to_i
11
+ )
12
+ send_file_headers!(default_headers_for(params[:format]))
13
+ if !streamer.object_exists?
14
+ head 404, default_headers_for(params[:format])
15
+ return
12
16
  end
13
-
14
17
  self.status = 200
15
- self.response_body = Enumerator.new do |y|
16
- connection.transaction do
17
- lo = connection.lo_open(object_identifier, ::PG::INV_READ)
18
- while data = connection.lo_read(lo, 4096) do
19
- y << data
20
- end
21
- connection.lo_close(lo)
22
- end
23
- end
18
+ self.response_body = streamer.stream
19
+
24
20
  end
25
21
 
26
22
  def connection
@@ -29,29 +25,9 @@ module PostgresqlLoStreamer
29
25
 
30
26
  private
31
27
 
32
- def object_exists?(identifier)
33
- begin
34
- connection.lo_open(identifier, ::PG::INV_READ)
35
- rescue PG::Error => e
36
- if e.to_s.include? "does not exist"
37
- return false
38
- end
39
-
40
- raise
41
- end
42
-
43
- return true
44
- end
45
-
46
- def configuration
47
- PostgresqlLoStreamer.configuration
48
- end
49
-
50
- def headers_for_extension_or_default
51
- #extension provided and recognized
52
- #Mime is built into rails
53
- if params[:format].present? && Mime::Type.lookup_by_extension(params[:format]).present?
54
- type = Mime::Type.lookup_by_extension(params[:format]).to_s
28
+ def default_headers_for(extension)
29
+ if extension.present? && Mime::Type.lookup_by_extension(extension).present?
30
+ type = Mime::Type.lookup_by_extension(extension).to_s
55
31
  {type: type, disposition: disposition_from_type(type) }
56
32
  else
57
33
  configuration.options #fallback
@@ -75,5 +51,9 @@ module PostgresqlLoStreamer
75
51
  end
76
52
  end
77
53
 
54
+ def configuration
55
+ PostgresqlLoStreamer.configuration
56
+ end
57
+
78
58
  end
79
59
  end
@@ -1,5 +1,6 @@
1
1
  require "postgresql_lo_streamer/engine"
2
2
  require "postgresql_lo_streamer/configuration"
3
+ require "postgresql_lo_streamer/streamer"
3
4
 
4
5
  module PostgresqlLoStreamer
5
6
  class << self
@@ -0,0 +1,33 @@
1
+ class PostgresqlLoStreamer::Streamer
2
+ attr_accessor :connection, :object_identifier
3
+
4
+ def initialize(connection, object_identifier)
5
+ @connection = connection
6
+ @object_identifier = object_identifier
7
+ end
8
+
9
+ def object_exists?
10
+ begin
11
+ @connection.lo_open(@object_identifier, ::PG::INV_READ)
12
+ rescue PG::Error => e
13
+ if e.to_s.include? "does not exist"
14
+ return false
15
+ end
16
+ raise
17
+ end
18
+ return true
19
+ end
20
+
21
+ def stream
22
+ return Enumerator.new do |y|
23
+ @connection.transaction do
24
+ lo = @connection.lo_open(@object_identifier, ::PG::INV_READ)
25
+ while data = connection.lo_read(lo, 4096) do
26
+ y << data
27
+ end
28
+ @connection.lo_close(lo)
29
+ end
30
+ end
31
+ end
32
+
33
+ end
@@ -1,3 +1,3 @@
1
1
  module PostgresqlLoStreamer
2
- VERSION = "1.2.0"
2
+ VERSION = "1.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postgresql_lo_streamer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diogo Biazus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-21 00:00:00.000000000 Z
11
+ date: 2017-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -82,6 +82,7 @@ files:
82
82
  - lib/postgresql_lo_streamer.rb
83
83
  - lib/postgresql_lo_streamer/configuration.rb
84
84
  - lib/postgresql_lo_streamer/engine.rb
85
+ - lib/postgresql_lo_streamer/streamer.rb
85
86
  - lib/postgresql_lo_streamer/version.rb
86
87
  - lib/tasks/postgresql_lo_streamer_tasks.rake
87
88
  homepage: https://github.com/diogob/postgresql_lo_streamer