dato 0.7.6 → 0.7.13
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +47 -0
- data/LICENSE +21 -0
- data/lib/dato.rb +1 -0
- data/lib/dato/api_client.rb +21 -22
- data/lib/dato/cli.rb +3 -0
- data/lib/dato/local/field_type/file.rb +142 -7
- data/lib/dato/local/loader.rb +9 -2
- data/lib/dato/site/client.rb +10 -5
- data/lib/dato/upload/create_upload_path.rb +81 -0
- data/lib/dato/upload/file.rb +10 -67
- data/lib/dato/utils/build_modular_block.rb +31 -0
- data/lib/dato/utils/meta_tags/base.rb +1 -1
- data/lib/dato/version.rb +1 -1
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8911d925c9122207b1b12804415839e1a60c6f42212523f8a9d8590a0ac3b609
|
4
|
+
data.tar.gz: fcb4dfbed9102fd7c037c635bb0a436ea8c5671964e51e7dcf1c9b7345f25471
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 127d2b647b57bbf530f3def61fefd302f763ec0ead035dc0f7a3d53cb6d6646948a994a75c5b6f22533e815e35a1a27a1d1f6cb0b85f2eedb86e8985971bbc87
|
7
|
+
data.tar.gz: 9709363be3b119354890d3d1de8a16bd8a020faf3e58692022e50de1ba25dcd4af6c49f1962e03cd8e8b82f35334140efa61cd7ff9f3f9835b39c5901b3e904b
|
data/CHANGELOG.md
CHANGED
@@ -1,3 +1,50 @@
|
|
1
|
+
# 0.7.13
|
2
|
+
|
3
|
+
Add option to pass a project's environment:
|
4
|
+
|
5
|
+
```ruby
|
6
|
+
require "dato"
|
7
|
+
client = Dato::Site::Client.new("YOUR-API-KEY", environment: 'sandbox-foobar')
|
8
|
+
```
|
9
|
+
|
10
|
+
# 0.7.12
|
11
|
+
|
12
|
+
Introduces `Dato::Utils::BuildModularBlock` class to help creating modular blocks.
|
13
|
+
|
14
|
+
An example usage can be:
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
description = [
|
18
|
+
Dato::Utils::BuildModularBlock.build(
|
19
|
+
item_type: "1235",
|
20
|
+
name: "Best dog in the world",
|
21
|
+
year: "2020",
|
22
|
+
picture: picture
|
23
|
+
)
|
24
|
+
]
|
25
|
+
|
26
|
+
record = client.items.create(
|
27
|
+
item_type: "1234", # model ID
|
28
|
+
name: "Gigio",
|
29
|
+
description: description
|
30
|
+
)
|
31
|
+
```
|
32
|
+
|
33
|
+
Find more info [on the documentation](https://www.datocms.com/docs/content-management-api/resources/item/create).
|
34
|
+
|
35
|
+
# 0.7.11 (not released)
|
36
|
+
|
37
|
+
* Updated specs cassettes after `appeareance -> appearance` typo fix
|
38
|
+
* Some style changes
|
39
|
+
|
40
|
+
# 0.7.10
|
41
|
+
|
42
|
+
* Fixed SEO title retrieval. Now it fallbacks to default SEO title is item title is blank
|
43
|
+
|
44
|
+
# 0.7.9
|
45
|
+
|
46
|
+
* Added new attributes to uploads
|
47
|
+
|
1
48
|
# 0.7.0
|
2
49
|
|
3
50
|
* Real-time events are now much more granular and the gem avoids downloading all the content every time a change occurs
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2019 Cantiere Creativo SRL
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/lib/dato.rb
CHANGED
data/lib/dato/api_client.rb
CHANGED
@@ -20,7 +20,7 @@ module Dato
|
|
20
20
|
base.extend ClassMethods
|
21
21
|
|
22
22
|
base.class_eval do
|
23
|
-
attr_reader :token, :base_url, :schema, :extra_headers
|
23
|
+
attr_reader :token, :environment, :base_url, :schema, :extra_headers
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -29,27 +29,28 @@ module Dato
|
|
29
29
|
define_method(:initialize) do |token, options = {}|
|
30
30
|
@token = token
|
31
31
|
@base_url = options[:base_url] || "https://#{subdomain}.datocms.com"
|
32
|
+
@environment = options[:environment]
|
32
33
|
@extra_headers = options[:extra_headers] || {}
|
33
34
|
end
|
34
35
|
|
36
|
+
# FOR DEV
|
37
|
+
# "http://#{subdomain}.lvh.me:3001/docs/#{subdomain}-hyperschema.json"
|
35
38
|
response = Faraday.get(
|
36
|
-
# FOR DEV
|
37
|
-
# "http://#{subdomain}.lvh.me:3001/docs/#{subdomain}-hyperschema.json"
|
38
39
|
"https://#{subdomain}.datocms.com/docs/#{subdomain}-hyperschema.json"
|
39
40
|
)
|
40
41
|
|
41
42
|
schema = JsonSchema.parse!(JSON.parse(response.body))
|
42
43
|
schema.expand_references!
|
43
44
|
|
44
|
-
schema.definitions.each do |type,
|
45
|
-
is_collection =
|
45
|
+
schema.definitions.each do |type, obj|
|
46
|
+
is_collection = obj.links.select { |x| x.rel == 'instances' }.any?
|
46
47
|
namespace = is_collection ? type.pluralize : type
|
47
48
|
|
48
49
|
define_method(namespace) do
|
49
50
|
instance_variable_set(
|
50
51
|
"@#{namespace}",
|
51
52
|
instance_variable_get("@#{namespace}") ||
|
52
|
-
Dato::Repo.new(self, type,
|
53
|
+
Dato::Repo.new(self, type, obj)
|
53
54
|
)
|
54
55
|
end
|
55
56
|
end
|
@@ -76,16 +77,10 @@ module Dato
|
|
76
77
|
method, absolute_path, body, params = args
|
77
78
|
|
78
79
|
response = connection.send(method, absolute_path, body) do |c|
|
79
|
-
if params
|
80
|
-
c.params = params
|
81
|
-
end
|
80
|
+
c.params = params if params
|
82
81
|
end
|
83
82
|
|
84
|
-
if response.body.is_a?(Hash)
|
85
|
-
response.body.with_indifferent_access
|
86
|
-
else
|
87
|
-
nil
|
88
|
-
end
|
83
|
+
response.body.with_indifferent_access if response.body.is_a?(Hash)
|
89
84
|
rescue Faraday::SSLError => e
|
90
85
|
raise e if ENV['SSL_CERT_FILE'] == Cacert.pem
|
91
86
|
|
@@ -101,14 +96,14 @@ module Dato
|
|
101
96
|
sleep(to_wait + 1)
|
102
97
|
request(*args)
|
103
98
|
elsif e.response[:status] == 422 && batch_data_validation?(e.response)
|
104
|
-
puts
|
99
|
+
puts 'Validating items, waiting 1 second and retrying...'
|
105
100
|
sleep(1)
|
106
101
|
request(*args)
|
107
102
|
else
|
108
103
|
error = ApiError.new(e.response)
|
109
|
-
puts
|
104
|
+
puts '===='
|
110
105
|
puts error.message
|
111
|
-
puts
|
106
|
+
puts '===='
|
112
107
|
raise error
|
113
108
|
end
|
114
109
|
end
|
@@ -123,12 +118,12 @@ module Dato
|
|
123
118
|
end
|
124
119
|
|
125
120
|
return false unless body
|
126
|
-
return false unless body[
|
121
|
+
return false unless body['data']
|
127
122
|
|
128
|
-
body[
|
129
|
-
e[
|
123
|
+
body['data'].any? do |e|
|
124
|
+
e['attributes']['code'] == 'BATCH_DATA_VALIDATION_IN_PROGRESS'
|
130
125
|
end
|
131
|
-
rescue
|
126
|
+
rescue StandardError
|
132
127
|
false
|
133
128
|
end
|
134
129
|
|
@@ -141,9 +136,13 @@ module Dato
|
|
141
136
|
'X-Api-Version' => '3'
|
142
137
|
}
|
143
138
|
|
139
|
+
if environment
|
140
|
+
default_headers.merge!('X-Environment' => environment)
|
141
|
+
end
|
142
|
+
|
144
143
|
options = {
|
145
144
|
url: base_url,
|
146
|
-
headers: default_headers.merge(extra_headers)
|
145
|
+
headers: default_headers.merge(extra_headers)
|
147
146
|
}
|
148
147
|
|
149
148
|
@connection ||= Faraday.new(options) do |c|
|
data/lib/dato/cli.rb
CHANGED
@@ -11,8 +11,10 @@ module Dato
|
|
11
11
|
desc 'dump', 'dumps DatoCMS content into local files'
|
12
12
|
option :config, default: 'dato.config.rb'
|
13
13
|
option :token, default: ENV['DATO_API_TOKEN'], required: true
|
14
|
+
option :environment, type: :string, required: false
|
14
15
|
option :preview, default: false, type: :boolean
|
15
16
|
option :watch, default: false, type: :boolean
|
17
|
+
|
16
18
|
def dump
|
17
19
|
config_file = File.expand_path(options[:config])
|
18
20
|
watch_mode = options[:watch]
|
@@ -20,6 +22,7 @@ module Dato
|
|
20
22
|
|
21
23
|
client = Dato::Site::Client.new(
|
22
24
|
options[:token],
|
25
|
+
environment: options[:environment],
|
23
26
|
extra_headers: {
|
24
27
|
'X-Reason' => 'dump',
|
25
28
|
'X-SSG' => Dump::SsgDetector.new(Dir.pwd).detect
|
@@ -38,6 +38,10 @@ module Dato
|
|
38
38
|
@imgix_host = imgix_host
|
39
39
|
end
|
40
40
|
|
41
|
+
def id
|
42
|
+
@upload.id
|
43
|
+
end
|
44
|
+
|
41
45
|
def path
|
42
46
|
@upload.path
|
43
47
|
end
|
@@ -70,19 +74,138 @@ module Dato
|
|
70
74
|
@upload.copyright
|
71
75
|
end
|
72
76
|
|
77
|
+
def filename
|
78
|
+
@upload.filename
|
79
|
+
end
|
80
|
+
|
81
|
+
def basename
|
82
|
+
@upload.basename
|
83
|
+
end
|
84
|
+
|
73
85
|
def alt
|
74
|
-
default_metadata = @upload.default_field_metadata.deep_stringify_keys
|
75
|
-
|
86
|
+
default_metadata = @upload.default_field_metadata.deep_stringify_keys
|
87
|
+
.fetch(I18n.locale.to_s, {})
|
88
|
+
@alt || default_metadata['alt']
|
76
89
|
end
|
77
90
|
|
78
91
|
def title
|
79
|
-
default_metadata = @upload.default_field_metadata.deep_stringify_keys
|
80
|
-
|
92
|
+
default_metadata = @upload.default_field_metadata.deep_stringify_keys
|
93
|
+
.fetch(I18n.locale.to_s, {})
|
94
|
+
@title || default_metadata['title']
|
81
95
|
end
|
82
96
|
|
83
97
|
def custom_data
|
84
|
-
default_metadata = @upload.default_field_metadata.deep_stringify_keys
|
85
|
-
|
98
|
+
default_metadata = @upload.default_field_metadata.deep_stringify_keys
|
99
|
+
.fetch(I18n.locale.to_s, {})
|
100
|
+
@custom_data.merge(default_metadata.fetch('custom_data', {}))
|
101
|
+
end
|
102
|
+
|
103
|
+
def tags
|
104
|
+
@upload.tags
|
105
|
+
end
|
106
|
+
|
107
|
+
def smart_tags
|
108
|
+
@upload.smart_tags
|
109
|
+
end
|
110
|
+
|
111
|
+
def is_image
|
112
|
+
@upload.is_image
|
113
|
+
end
|
114
|
+
|
115
|
+
def exif_info
|
116
|
+
@upload.exif_info
|
117
|
+
end
|
118
|
+
|
119
|
+
def mime_type
|
120
|
+
@upload.mime_type
|
121
|
+
end
|
122
|
+
|
123
|
+
def colors
|
124
|
+
@upload.colors.map { |color| Color.parse(color, nil) }
|
125
|
+
end
|
126
|
+
|
127
|
+
def blurhash
|
128
|
+
@upload.blurhash
|
129
|
+
end
|
130
|
+
|
131
|
+
class VideoAttributes
|
132
|
+
def initialize(upload)
|
133
|
+
@upload = upload
|
134
|
+
end
|
135
|
+
|
136
|
+
def mux_playback_id
|
137
|
+
@upload.mux_playback_id
|
138
|
+
end
|
139
|
+
|
140
|
+
def frame_rate
|
141
|
+
@upload.frame_rate
|
142
|
+
end
|
143
|
+
|
144
|
+
def duration
|
145
|
+
@upload.duration
|
146
|
+
end
|
147
|
+
|
148
|
+
def streaming_url
|
149
|
+
"https://stream.mux.com/#{@upload.mux_playback_id}.m3u8"
|
150
|
+
end
|
151
|
+
|
152
|
+
def thumbnail_url(format = :jpg)
|
153
|
+
if format == :gif
|
154
|
+
"https://image.mux.com/#{@upload.mux_playback_id}/animated.gif"
|
155
|
+
else
|
156
|
+
"https://image.mux.com/#{@upload.mux_playback_id}/thumbnail.#{format}"
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
def mp4_url(options = nil)
|
161
|
+
@upload.mux_mp4_highest_res or
|
162
|
+
return nil
|
163
|
+
|
164
|
+
if options && options[:exact_res]
|
165
|
+
if options[:exact_res] == :low
|
166
|
+
raw_mp4_url("low")
|
167
|
+
elsif options[:exact_res] == :medium
|
168
|
+
if %w[medium high].include?(@upload.mux_mp4_highest_res)
|
169
|
+
raw_mp4_url("medium")
|
170
|
+
end
|
171
|
+
elsif @upload.mux_mp4_highest_res == :high
|
172
|
+
raw_mp4_url("high")
|
173
|
+
end
|
174
|
+
elsif options && options[:res] == :low
|
175
|
+
raw_mp4_url("low")
|
176
|
+
elsif options && options[:res] == :medium
|
177
|
+
if %w[low medium].include?(@upload.mux_mp4_highest_res)
|
178
|
+
raw_mp4_url(@upload.mux_mp4_highest_res)
|
179
|
+
else
|
180
|
+
raw_mp4_url("medium")
|
181
|
+
end
|
182
|
+
else
|
183
|
+
raw_mp4_url(@upload.mux_mp4_highest_res)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
def to_hash
|
188
|
+
{
|
189
|
+
mux_playback_id: mux_playback_id,
|
190
|
+
frame_rate: frame_rate,
|
191
|
+
duration: duration,
|
192
|
+
streaming_url: streaming_url,
|
193
|
+
thumbnail_url: thumbnail_url,
|
194
|
+
mp4_url: mp4_url,
|
195
|
+
}
|
196
|
+
end
|
197
|
+
|
198
|
+
private
|
199
|
+
|
200
|
+
def raw_mp4_url(res)
|
201
|
+
"https://stream.mux.com/#{@upload.mux_playback_id}/#{res}.mp4"
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def video
|
206
|
+
if @upload.mux_playback_id
|
207
|
+
VideoAttributes.new(@upload)
|
208
|
+
end
|
86
209
|
end
|
87
210
|
|
88
211
|
def file
|
@@ -99,6 +222,7 @@ module Dato
|
|
99
222
|
|
100
223
|
def to_hash(*_args)
|
101
224
|
{
|
225
|
+
id: id,
|
102
226
|
format: format,
|
103
227
|
size: size,
|
104
228
|
width: width,
|
@@ -106,7 +230,18 @@ module Dato
|
|
106
230
|
alt: alt,
|
107
231
|
title: title,
|
108
232
|
custom_data: custom_data,
|
109
|
-
url: url
|
233
|
+
url: url,
|
234
|
+
copyright: copyright,
|
235
|
+
tags: tags,
|
236
|
+
smart_tags: smart_tags,
|
237
|
+
filename: filename,
|
238
|
+
basename: basename,
|
239
|
+
is_image: is_image,
|
240
|
+
exif_info: exif_info,
|
241
|
+
mime_type: mime_type,
|
242
|
+
colors: colors.map(&:to_hash),
|
243
|
+
blurhash: blurhash,
|
244
|
+
video: video && video.to_hash
|
110
245
|
}
|
111
246
|
end
|
112
247
|
end
|
data/lib/dato/local/loader.rb
CHANGED
@@ -43,7 +43,13 @@ module Dato
|
|
43
43
|
|
44
44
|
return if pusher && pusher.connected
|
45
45
|
|
46
|
-
|
46
|
+
channel_name = if client.environment
|
47
|
+
"private-site-#{site_id}-environment-#{environment}"
|
48
|
+
else
|
49
|
+
"private-site-#{site_id}"
|
50
|
+
end
|
51
|
+
|
52
|
+
pusher.subscribe(channel_name)
|
47
53
|
|
48
54
|
bind_on_site_upsert(&block)
|
49
55
|
bind_on_item_destroy(&block)
|
@@ -66,7 +72,8 @@ module Dato
|
|
66
72
|
bind_on("site:upsert", block) do |data|
|
67
73
|
threads = [
|
68
74
|
Thread.new { Thread.current[:output] = site },
|
69
|
-
Thread.new { Thread.current[:output] = all_items }
|
75
|
+
Thread.new { Thread.current[:output] = all_items },
|
76
|
+
Thread.new { Thread.current[:output] = all_uploads }
|
70
77
|
]
|
71
78
|
|
72
79
|
results = threads.map do |t|
|
data/lib/dato/site/client.rb
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'dato/api_client'
|
4
4
|
require 'dato/upload/file'
|
5
|
-
require 'dato/upload/
|
5
|
+
require 'dato/upload/create_upload_path'
|
6
6
|
|
7
7
|
module Dato
|
8
8
|
module Site
|
@@ -11,13 +11,18 @@ module Dato
|
|
11
11
|
|
12
12
|
json_schema 'site-api'
|
13
13
|
|
14
|
-
def
|
15
|
-
file = Upload::
|
14
|
+
def create_upload_path(path_or_url)
|
15
|
+
file = Upload::CreateUploadPath.new(self, path_or_url)
|
16
|
+
file.upload_path
|
17
|
+
end
|
18
|
+
|
19
|
+
def upload_file(path_or_url, upload_attributes = {}, field_attributes = {})
|
20
|
+
file = Upload::File.new(self, path_or_url, upload_attributes, field_attributes)
|
16
21
|
file.upload
|
17
22
|
end
|
18
23
|
|
19
|
-
def upload_image(path_or_url)
|
20
|
-
file = Upload::
|
24
|
+
def upload_image(path_or_url, upload_attributes = {}, field_attributes = {})
|
25
|
+
file = Upload::File.new(self, path_or_url, upload_attributes, field_attributes)
|
21
26
|
file.upload
|
22
27
|
end
|
23
28
|
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'mime/types'
|
4
|
+
require 'tempfile'
|
5
|
+
require 'addressable'
|
6
|
+
require 'net/http'
|
7
|
+
|
8
|
+
module Dato
|
9
|
+
module Upload
|
10
|
+
class CreateUploadPath
|
11
|
+
attr_reader :client, :source
|
12
|
+
|
13
|
+
def initialize(client, source)
|
14
|
+
@client = client
|
15
|
+
@source = source
|
16
|
+
end
|
17
|
+
|
18
|
+
def file
|
19
|
+
@file ||= if http_source?
|
20
|
+
uri = Addressable::URI.parse(source)
|
21
|
+
ext = ::File.extname(uri.path).downcase
|
22
|
+
tempfile = Tempfile.new(['file', ext])
|
23
|
+
tempfile.binmode
|
24
|
+
tempfile.write(download_file(source))
|
25
|
+
tempfile.rewind
|
26
|
+
tempfile
|
27
|
+
else
|
28
|
+
::File.new(::File.expand_path(source))
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def http_source?
|
33
|
+
uri = Addressable::URI.parse(source)
|
34
|
+
uri.scheme == 'http' || uri.scheme == 'https'
|
35
|
+
rescue Addressable::URI::InvalidURIError
|
36
|
+
false
|
37
|
+
end
|
38
|
+
|
39
|
+
def filename
|
40
|
+
if http_source?
|
41
|
+
::File.basename(source)
|
42
|
+
else
|
43
|
+
::File.basename(file.path)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def upload_path
|
48
|
+
upload_request = client.upload_request.create(filename: filename)
|
49
|
+
uri = URI.parse(upload_request[:url])
|
50
|
+
|
51
|
+
mime_type = MIME::Types.of(filename).first
|
52
|
+
|
53
|
+
request = Net::HTTP::Put.new(uri)
|
54
|
+
if mime_type
|
55
|
+
request.add_field("Content-Type", mime_type.to_s)
|
56
|
+
end
|
57
|
+
request.body = file.read
|
58
|
+
|
59
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
60
|
+
http.use_ssl = true
|
61
|
+
|
62
|
+
http.request(request)
|
63
|
+
|
64
|
+
upload_request[:id]
|
65
|
+
end
|
66
|
+
|
67
|
+
def download_file(url)
|
68
|
+
connection = Faraday.new do |c|
|
69
|
+
c.response :raise_error
|
70
|
+
c.use FaradayMiddleware::FollowRedirects
|
71
|
+
c.adapter :net_http
|
72
|
+
end
|
73
|
+
connection.get(url).body
|
74
|
+
rescue Faraday::Error => e
|
75
|
+
puts "Error during uploading #{url}"
|
76
|
+
raise e
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
data/lib/dato/upload/file.rb
CHANGED
@@ -1,88 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
4
|
-
require 'tempfile'
|
5
|
-
require 'addressable'
|
6
|
-
require 'net/http'
|
3
|
+
require 'dato/upload/create_upload_path'
|
7
4
|
|
8
5
|
module Dato
|
9
6
|
module Upload
|
10
7
|
class File
|
11
|
-
|
8
|
+
attr_reader :client, :source, :upload_attributes, :field_attributes
|
12
9
|
|
13
|
-
|
14
|
-
|
15
|
-
def initialize(client, source)
|
10
|
+
def initialize(client, source, upload_attributes = {}, field_attributes = {})
|
16
11
|
@client = client
|
17
12
|
@source = source
|
18
|
-
|
19
|
-
|
20
|
-
def file
|
21
|
-
@file ||= if http_source?
|
22
|
-
uri = Addressable::URI.parse(source)
|
23
|
-
ext = ::File.extname(uri.path).downcase
|
24
|
-
tempfile = Tempfile.new(['file', ext])
|
25
|
-
tempfile.binmode
|
26
|
-
tempfile.write(download_file(source))
|
27
|
-
tempfile.rewind
|
28
|
-
tempfile
|
29
|
-
else
|
30
|
-
::File.new(::File.expand_path(source))
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def http_source?
|
35
|
-
uri = Addressable::URI.parse(source)
|
36
|
-
uri.scheme == 'http' || uri.scheme == 'https'
|
37
|
-
rescue Addressable::URI::InvalidURIError
|
38
|
-
false
|
39
|
-
end
|
40
|
-
|
41
|
-
def filename
|
42
|
-
if http_source?
|
43
|
-
::File.basename(source)
|
44
|
-
else
|
45
|
-
::File.basename(file.path)
|
46
|
-
end
|
13
|
+
@upload_attributes = upload_attributes
|
14
|
+
@field_attributes = field_attributes
|
47
15
|
end
|
48
16
|
|
49
17
|
def upload
|
50
|
-
|
51
|
-
uri = URI.parse(upload_request[:url])
|
18
|
+
upload_path = CreateUploadPath.new(client, source).upload_path
|
52
19
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
if mime_type
|
57
|
-
request.add_field("Content-Type", mime_type.to_s)
|
58
|
-
end
|
59
|
-
request.body = file.read
|
60
|
-
|
61
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
62
|
-
http.use_ssl = true
|
63
|
-
|
64
|
-
http.request(request)
|
65
|
-
|
66
|
-
upload = client.uploads.create(path: upload_request[:id])
|
20
|
+
upload = client.uploads.create(
|
21
|
+
upload_attributes.merge(path: upload_path)
|
22
|
+
)
|
67
23
|
|
68
24
|
{
|
69
|
-
upload_id: upload['id'],
|
70
25
|
alt: nil,
|
71
26
|
title: nil,
|
72
27
|
custom_data: {},
|
73
|
-
}
|
74
|
-
end
|
75
|
-
|
76
|
-
def download_file(url)
|
77
|
-
connection = Faraday.new do |c|
|
78
|
-
c.response :raise_error
|
79
|
-
c.use FaradayMiddleware::FollowRedirects
|
80
|
-
c.adapter :net_http
|
81
|
-
end
|
82
|
-
connection.get(url).body
|
83
|
-
rescue Faraday::Error => e
|
84
|
-
puts "Error during uploading #{url}"
|
85
|
-
raise e
|
28
|
+
}.merge(field_attributes).merge(upload_id: upload['id'])
|
86
29
|
end
|
87
30
|
end
|
88
31
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'dato/json_api_serializer'
|
4
|
+
|
5
|
+
module Dato
|
6
|
+
module Utils
|
7
|
+
module BuildModularBlock
|
8
|
+
def self.build(unserialized_body)
|
9
|
+
json_api_serializer = JsonApiSerializer.new('item', nil)
|
10
|
+
attributes = json_api_serializer.serialized_attributes(unserialized_body)
|
11
|
+
|
12
|
+
payload = {
|
13
|
+
type: 'item',
|
14
|
+
attributes: attributes,
|
15
|
+
relationships: {
|
16
|
+
item_type: {
|
17
|
+
data: {
|
18
|
+
id: unserialized_body[:item_type],
|
19
|
+
type: 'item_type',
|
20
|
+
},
|
21
|
+
},
|
22
|
+
},
|
23
|
+
}
|
24
|
+
|
25
|
+
payload[:id] = unserialized_body[:id] if unserialized_body[:id]
|
26
|
+
|
27
|
+
payload
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/dato/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dato
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.7.
|
4
|
+
version: 0.7.13
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stefano Verna
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-05-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -362,6 +362,7 @@ files:
|
|
362
362
|
- CHANGELOG.md
|
363
363
|
- CODE_OF_CONDUCT.md
|
364
364
|
- Gemfile
|
365
|
+
- LICENSE
|
365
366
|
- LICENSE.txt
|
366
367
|
- README.md
|
367
368
|
- Rakefile
|
@@ -425,8 +426,10 @@ files:
|
|
425
426
|
- lib/dato/paginator.rb
|
426
427
|
- lib/dato/repo.rb
|
427
428
|
- lib/dato/site/client.rb
|
429
|
+
- lib/dato/upload/create_upload_path.rb
|
428
430
|
- lib/dato/upload/file.rb
|
429
431
|
- lib/dato/upload/image.rb
|
432
|
+
- lib/dato/utils/build_modular_block.rb
|
430
433
|
- lib/dato/utils/favicon_tags_builder.rb
|
431
434
|
- lib/dato/utils/locale_value.rb
|
432
435
|
- lib/dato/utils/meta_tags/article_modified_time.rb
|