postgresql_lo_streamer 1.1.0 → 1.2.0

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: bbfc0cb9ae078d03087880f6b0ad6c6f32ca4f66
4
- data.tar.gz: 55c72abfb7103f6df8a18bea5dd164fdf7be2e86
3
+ metadata.gz: 0b15445e68664011f4a6c5004276249dfc86c5dc
4
+ data.tar.gz: 65d66610979783285de42eb754f81bd1724d1fde
5
5
  SHA512:
6
- metadata.gz: 876522e92fe8318eeae03d0630093e0052d5ed537127710fcd1909e39588e6014503f3aa46982ec6630bbfaaa806875593d9844fa82ba77c6aa605924617df69
7
- data.tar.gz: 371246ea78633323dda68052baaa9e68748733c54645b6c0ac6419c77ee95b367c644edc014e84014bdaf981f0c8bfbcc2ab748a36bf6f73abe83375187e5e9e
6
+ metadata.gz: 339e58f1db995b003206e860a20c29649e9d1b90059372d99cec8bef585ac39e6b423d04675c7b4a02349f11deb4e6f6f4aa3d86613f770f23d4eca0cd2fd54c
7
+ data.tar.gz: ff139796cfc79a93b34c0cd12129845629ff0a721672072bf7385bf6f76445ad22174d7a3347fd0128bbfffbf5f6b8bb7baea85492c89f11f28bebacf898e63a
data/README.md CHANGED
@@ -18,12 +18,30 @@ You can adjust default file streaming headers by tweaking configuration options:
18
18
  config.options = {:type => 'image/png', :disposition => 'inline'}
19
19
  end
20
20
 
21
-
22
21
  Add to your `config/routes.rb` the mount url where you are going to retrieve the files.
23
22
  The following example will create a route `/image_file/:id` where :id is the oid of the Large Object in the database:
24
23
 
25
24
  mount PostgresqlLoStreamer::Engine => "/image_file"
26
25
 
26
+ ## Passing mime-type with url
27
+
28
+ If you specify an extension with your url, the default file streaming headers will be overridden.
29
+
30
+ /file/38681 #=> image/png
31
+ /file/38681.jpg #=> image/jpeg
32
+ /file/38681.css #=> text/css
33
+
34
+ This allows you to dynamically stream resources of different types and send appropriate headers. Please note that only the following types will be sent with an inline disposition, otherwise, it will be sent as an attachment, forcing a download.
35
+
36
+ image/jpeg
37
+ image/png
38
+ image/gif
39
+ image/svg+xml
40
+ text/css
41
+ text/plain
42
+
43
+ In order for this to work in practice, you will likely need to store the extension or the url in the database alongside the oid when saving the file.
44
+
27
45
  ## Using it with carrierwave-postgresql
28
46
 
29
47
  If you are storing your files in the database using the [carrierwave-postgresql](http://diogob.github.com/carrierwave-postgresql/) gem, then your model will generate an URL like `/<mode_name>_<attribute_name>/<oid>`.
@@ -31,7 +49,7 @@ If you are storing your files in the database using the [carrierwave-postgresql]
31
49
  So, for our previous example, if you have a model called `Image`, with and attribute called `file` (which is the oid referencing the Large Object), then the model will generate the URL matching our example. If you have more than one large object attribute in your database you can mount this engine multiple times with different URLs.
32
50
 
33
51
  ## Contributing to postgresql_lo_streamer
34
-
52
+
35
53
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
36
54
  * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
37
55
  * Fork the project.
@@ -3,7 +3,7 @@
3
3
  module PostgresqlLoStreamer
4
4
  class LoController < ActionController::Base
5
5
  def stream
6
- send_file_headers! configuration.options
6
+ send_file_headers!(headers_for_extension_or_default)
7
7
 
8
8
  object_identifier = params[:id].to_i
9
9
  if !object_exists?(object_identifier)
@@ -28,7 +28,7 @@ module PostgresqlLoStreamer
28
28
  end
29
29
 
30
30
  private
31
-
31
+
32
32
  def object_exists?(identifier)
33
33
  begin
34
34
  connection.lo_open(identifier, ::PG::INV_READ)
@@ -36,15 +36,44 @@ module PostgresqlLoStreamer
36
36
  if e.to_s.include? "does not exist"
37
37
  return false
38
38
  end
39
-
39
+
40
40
  raise
41
41
  end
42
42
 
43
43
  return true
44
44
  end
45
-
45
+
46
46
  def configuration
47
47
  PostgresqlLoStreamer.configuration
48
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
55
+ {type: type, disposition: disposition_from_type(type) }
56
+ else
57
+ configuration.options #fallback
58
+ end
59
+ end
60
+
61
+ def disposition_from_type(type)
62
+ inline_types = [
63
+ "image/jpeg",
64
+ "image/png",
65
+ "image/gif",
66
+ "image/svg+xml",
67
+ "text/css",
68
+ "text/plain"
69
+ ]
70
+ case type
71
+ when *inline_types
72
+ "inline"
73
+ else
74
+ "attachment" #fallback
75
+ end
76
+ end
77
+
49
78
  end
50
79
  end
@@ -1,3 +1,3 @@
1
1
  module PostgresqlLoStreamer
2
- VERSION = "1.1.0"
2
+ VERSION = "1.2.0"
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.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Diogo Biazus
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-11-11 00:00:00.000000000 Z
11
+ date: 2017-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
103
103
  version: '0'
104
104
  requirements: []
105
105
  rubyforge_project:
106
- rubygems_version: 2.5.1
106
+ rubygems_version: 2.6.8
107
107
  signing_key:
108
108
  specification_version: 4
109
109
  summary: A Rails engine to stream PostgreSQL Large Objects to clients