shrine-sql 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/lib/shrine/storage/sql.rb +62 -0
- data/shrine-sql.gemspec +22 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0be3b2fc90a4117a0f9ee77be1b735630c414d08
|
4
|
+
data.tar.gz: 9bc258b1ce55a676a83cbe4bd203b75e247ec02d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3f45d5520f916b766c30d9eaf22078b8d6905cf9edafd0c1824b574cd57741177eb80a84ae0a061f6aa57784d1fca76b882ff38ef189a3ff2e965a7075abba18
|
7
|
+
data.tar.gz: c614f098019442f864d3317535a1a4d57236bb47047c309a4f4ce5c633abe7e1402f7cf7a8a27093d1c36a5ecf8050fa49a4eeaf0960417de64a2df41ec3c8d8
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Janko Marohnić
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Shrine::Sql
|
2
|
+
|
3
|
+
Provides a [Shrine] storage for storing files in any SQL database. It uses
|
4
|
+
[Sequel] under the hood.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
```ruby
|
9
|
+
gem "shrine-sql"
|
10
|
+
```
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
We first need to create the table for our files, with "id" and "content" columns:
|
15
|
+
|
16
|
+
```rb
|
17
|
+
create_table :files do
|
18
|
+
primary_key :id
|
19
|
+
column :content, :text
|
20
|
+
end
|
21
|
+
```
|
22
|
+
|
23
|
+
We can now instantiate the storage with a `Sequel::Database` and the name of
|
24
|
+
the table:
|
25
|
+
|
26
|
+
```rb
|
27
|
+
require "shrine/storage/sql"
|
28
|
+
require "sequel"
|
29
|
+
|
30
|
+
DB = Sequel.connect("postgres:///my-database")
|
31
|
+
Shrine.storages[:store] = Shrine::Storage::Sql.new(database: DB, table: :files)
|
32
|
+
```
|
33
|
+
|
34
|
+
You can see [Connecting to a database] on how connect to any database with
|
35
|
+
Sequel.
|
36
|
+
|
37
|
+
### URL
|
38
|
+
|
39
|
+
The shrine-sql storage itself doesn't provide any kind of URL, but you can load
|
40
|
+
the `data_uri` plugin, which provides `UploadedFile#data_uri` which returns the
|
41
|
+
data URI of the file, which you can display in the browser:
|
42
|
+
|
43
|
+
```rb
|
44
|
+
Shrine.plugin :data_uri
|
45
|
+
```
|
46
|
+
```rb
|
47
|
+
user.avatar.data_uri #=> "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA"
|
48
|
+
```
|
49
|
+
|
50
|
+
Note that `UploadedFile#data_uri` is only available in Shrine >= 1.1.
|
51
|
+
|
52
|
+
### Indices
|
53
|
+
|
54
|
+
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.
|
57
|
+
|
58
|
+
## Contributing
|
59
|
+
|
60
|
+
You can run the tests with Rake:
|
61
|
+
|
62
|
+
```sh
|
63
|
+
$ bundle exec rake test
|
64
|
+
```
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
[MIT](http://opensource.org/licenses/MIT)
|
69
|
+
|
70
|
+
[Shrine]: https://github.com/janko-m/shrine
|
71
|
+
[Sequel]: https://github.com/janko-m/shrine
|
72
|
+
[Connecting to a database]: http://sequel.jeremyevans.net/rdoc/files/doc/opening_databases_rdoc.html
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require "sequel"
|
2
|
+
require "stringio"
|
3
|
+
|
4
|
+
class Shrine
|
5
|
+
module Storage
|
6
|
+
class Sql
|
7
|
+
attr_reader :database, :dataset
|
8
|
+
|
9
|
+
def initialize(database:, table:)
|
10
|
+
@database = database
|
11
|
+
@dataset = @database[table]
|
12
|
+
end
|
13
|
+
|
14
|
+
def upload(io, id, metadata = {})
|
15
|
+
generated_id = dataset.insert(content: io.read)
|
16
|
+
id.replace(generated_id.to_s)
|
17
|
+
end
|
18
|
+
|
19
|
+
def download(id)
|
20
|
+
tempfile = Tempfile.new("shrine", binmode: true)
|
21
|
+
File.write(tempfile.path, content(id))
|
22
|
+
tempfile
|
23
|
+
end
|
24
|
+
|
25
|
+
def open(id)
|
26
|
+
StringIO.new(content(id))
|
27
|
+
end
|
28
|
+
|
29
|
+
def read(id)
|
30
|
+
content(id)
|
31
|
+
end
|
32
|
+
|
33
|
+
def exists?(id)
|
34
|
+
this = dataset.where(id: id).limit(1)
|
35
|
+
!this.get(Sequel::SQL::AliasedExpression.new(1, :one)).nil?
|
36
|
+
end
|
37
|
+
|
38
|
+
def delete(id)
|
39
|
+
dataset.where(id: id).delete
|
40
|
+
end
|
41
|
+
|
42
|
+
def multi_delete(ids)
|
43
|
+
dataset.where(id: ids).delete
|
44
|
+
end
|
45
|
+
|
46
|
+
def url(id, options = {})
|
47
|
+
end
|
48
|
+
|
49
|
+
def clear!(confirm = nil)
|
50
|
+
raise Shrine::Confirm unless confirm == :confirm
|
51
|
+
dataset.delete
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def content(id)
|
57
|
+
record = dataset.first!(id: id)
|
58
|
+
record[:content]
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/shrine-sql.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = "shrine-sql"
|
3
|
+
gem.version = "0.1.0"
|
4
|
+
|
5
|
+
gem.required_ruby_version = ">= 2.1"
|
6
|
+
|
7
|
+
gem.summary = "Provides SQL database storage for Shrine."
|
8
|
+
gem.homepage = "https://github.com/janko-m/shrine-sql"
|
9
|
+
gem.authors = ["Janko Marohnić"]
|
10
|
+
gem.email = ["janko.marohnic@gmail.com"]
|
11
|
+
gem.license = "MIT"
|
12
|
+
|
13
|
+
gem.files = Dir["README.md", "LICENSE.txt", "lib/**/*.rb", "shrine-sql.gemspec"]
|
14
|
+
gem.require_path = "lib"
|
15
|
+
|
16
|
+
gem.add_dependency "sequel"
|
17
|
+
|
18
|
+
gem.add_development_dependency "sqlite3"
|
19
|
+
gem.add_development_dependency "rake"
|
20
|
+
gem.add_development_dependency "shrine"
|
21
|
+
gem.add_development_dependency "minitest"
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: shrine-sql
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Janko Marohnić
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sequel
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sqlite3
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: shrine
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description:
|
84
|
+
email:
|
85
|
+
- janko.marohnic@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- LICENSE.txt
|
91
|
+
- README.md
|
92
|
+
- lib/shrine/storage/sql.rb
|
93
|
+
- shrine-sql.gemspec
|
94
|
+
homepage: https://github.com/janko-m/shrine-sql
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '2.1'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.4.5
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Provides SQL database storage for Shrine.
|
118
|
+
test_files: []
|
119
|
+
has_rdoc:
|