shrine-sql 0.1.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0be3b2fc90a4117a0f9ee77be1b735630c414d08
4
- data.tar.gz: 9bc258b1ce55a676a83cbe4bd203b75e247ec02d
3
+ metadata.gz: e3ce6de1e86bfa4b3bbcc7f3e6f066d826b6f946
4
+ data.tar.gz: e3e3233a2f828548bc775c04b4a2e8071ae9dccd
5
5
  SHA512:
6
- metadata.gz: 3f45d5520f916b766c30d9eaf22078b8d6905cf9edafd0c1824b574cd57741177eb80a84ae0a061f6aa57784d1fca76b882ff38ef189a3ff2e965a7075abba18
7
- data.tar.gz: c614f098019442f864d3317535a1a4d57236bb47047c309a4f4ce5c633abe7e1402f7cf7a8a27093d1c36a5ecf8050fa49a4eeaf0960417de64a2df41ec3c8d8
6
+ metadata.gz: f25aa9d884a6393c7f0c4e87dbbae48fbeb13e4f5816f39efc6e202ff6ab346837bd36449c4352a6b81de8384f59b93ea00a3ddc0fd188cdb5e37d3fadf60e25
7
+ data.tar.gz: 03bc507da8227201add9b53a8bc29b66f7dbcccc426a1b7f22ae73a8800f65855411f22c15ecf29148d219d2ead2a3d3d437ea5249c9a786c8e259140919974a
data/README.md CHANGED
@@ -16,7 +16,8 @@ We first need to create the table for our files, with "id" and "content" columns
16
16
  ```rb
17
17
  create_table :files do
18
18
  primary_key :id
19
- column :content, :text
19
+ column :content, :text # :bytea for PostgreSQL, :blob for MySQL
20
+ column :metadata, :text # :varchar
20
21
  end
21
22
  ```
22
23
 
@@ -47,13 +48,12 @@ Shrine.plugin :data_uri
47
48
  user.avatar.data_uri #=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA"
48
49
  ```
49
50
 
50
- Note that `UploadedFile#data_uri` is only available in Shrine >= 1.1.
51
+ Note that `UploadedFile#data_uri` is available starting from Shrine 1.1.
51
52
 
52
53
  ### Indices
53
54
 
54
55
  It is recommended that you add a unique index to the "id" column, for faster
55
- lookups. Depending on your needs, you can also make the primary key column a
56
- UUID type, if your database supports it.
56
+ lookups.
57
57
 
58
58
  ## Contributing
59
59
 
@@ -1,5 +1,6 @@
1
1
  require "sequel"
2
2
  require "stringio"
3
+ require "json"
3
4
 
4
5
  class Shrine
5
6
  module Storage
@@ -12,12 +13,14 @@ class Shrine
12
13
  end
13
14
 
14
15
  def upload(io, id, metadata = {})
15
- generated_id = dataset.insert(content: io.read)
16
+ generated_id = store(io, id, metadata)
16
17
  id.replace(generated_id.to_s)
17
18
  end
18
19
 
19
20
  def download(id)
20
- tempfile = Tempfile.new("shrine", binmode: true)
21
+ metadata = JSON.parse(metadata(id))
22
+ extname = File.extname(metadata["filename"].to_s)
23
+ tempfile = Tempfile.new(["shrine", extname], binmode: true)
21
24
  File.write(tempfile.path, content(id))
22
25
  tempfile
23
26
  end
@@ -53,9 +56,33 @@ class Shrine
53
56
 
54
57
  private
55
58
 
59
+ def store(io, id, metadata)
60
+ if copyable?(io, id)
61
+ copy(io, id, metadata)
62
+ else
63
+ insert(io, id, metadata)
64
+ end
65
+ end
66
+
67
+ def insert(io, id, metadata)
68
+ dataset.insert(content: io.read, metadata: metadata.to_json)
69
+ end
70
+
71
+ def copy(io, id, metadata)
72
+ record = io.storage.dataset.where(id: io.id).select(:content, :metadata)
73
+ dataset.insert([:content, :metadata], record)
74
+ end
75
+
76
+ def copyable?(io, id)
77
+ io.is_a?(UploadedFile) && io.storage.is_a?(Storage::Sql)
78
+ end
79
+
56
80
  def content(id)
57
- record = dataset.first!(id: id)
58
- record[:content]
81
+ dataset.where(id: id).get(:content)
82
+ end
83
+
84
+ def metadata(id)
85
+ dataset.where(id: id).get(:metadata)
59
86
  end
60
87
  end
61
88
  end
data/shrine-sql.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |gem|
2
2
  gem.name = "shrine-sql"
3
- gem.version = "0.1.0"
3
+ gem.version = "1.0.0"
4
4
 
5
5
  gem.required_ruby_version = ">= 2.1"
6
6
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shrine-sql
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janko Marohnić
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-12-11 00:00:00.000000000 Z
11
+ date: 2015-12-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel
@@ -116,4 +116,3 @@ signing_key:
116
116
  specification_version: 4
117
117
  summary: Provides SQL database storage for Shrine.
118
118
  test_files: []
119
- has_rdoc: