dato 0.7.6 → 0.7.7

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
  SHA256:
3
- metadata.gz: 118ea91dc3caa834412e342a2350b594c94880bbbafba6f37d2b6041b5570acc
4
- data.tar.gz: 64e1a53c6292a41ad4e361e4b01bdff416d09e3acebbd133afc123c2f80fe74b
3
+ metadata.gz: 93c35eede758af97a20a62da18105ecf526b0727efde0e6da8e9785da0b3fb06
4
+ data.tar.gz: 8884b702f26c094d226fcb33e6a14bb6316a06a6a434fb74f06cac9c03f495f0
5
5
  SHA512:
6
- metadata.gz: a93f7dccda38b47237a585a7274754b2a3c370918c476e5556bc42ec13a2326a53e594f25b48fdc240a456f707d71dedfe9d9587a978ff38a177c398d5627ffa
7
- data.tar.gz: b74c22160ba51d703705213b12c60d795eb499855fc6179a8b2c576ff8e32d1cd7bb94747a7e3a6a5bd957ff527e34964ca01eb47dbfdc078f16519272dfb6a4
6
+ metadata.gz: dfd86b0dd620ea5585a05d7aea34dcf6ca399436e9a532b8c9de096e16b8f43153544df8e6753b1f9da3e55275e004fd55c75b6991d7abd987dbe8acdc11d9db
7
+ data.tar.gz: 799596a6912acc9fb99bab989fb1a57deefc7d04d9a1059808eb06e431c42954ba132cdce8146b97286513d20a353aa501b258ebf534cc551af53442340f15b8
@@ -2,7 +2,7 @@
2
2
 
3
3
  require 'dato/api_client'
4
4
  require 'dato/upload/file'
5
- require 'dato/upload/image'
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 upload_file(path_or_url)
15
- file = Upload::File.new(self, path_or_url)
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::Image.new(self, path_or_url)
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
+
@@ -1,88 +1,31 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'mime/types'
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
- IMAGE_FORMATS = %w[png jpg jpeg gif].freeze
8
+ attr_reader :client, :source, :upload_attributes, :field_attributes
12
9
 
13
- attr_reader :client, :source
14
-
15
- def initialize(client, source)
10
+ def initialize(client, source, upload_attributes = {}, field_attributes = {})
16
11
  @client = client
17
12
  @source = source
18
- end
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
- upload_request = client.upload_request.create(filename: filename)
51
- uri = URI.parse(upload_request[:url])
18
+ upload_path = CreateUploadPath.new(client, source).upload_path
52
19
 
53
- mime_type = MIME::Types.of(filename).first
54
-
55
- request = Net::HTTP::Put.new(uri)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Dato
4
- VERSION = '0.7.6'
4
+ VERSION = '0.7.7'
5
5
  end
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.6
4
+ version: 0.7.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Verna
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-09 00:00:00.000000000 Z
11
+ date: 2019-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -425,6 +425,7 @@ files:
425
425
  - lib/dato/paginator.rb
426
426
  - lib/dato/repo.rb
427
427
  - lib/dato/site/client.rb
428
+ - lib/dato/upload/create_upload_path.rb
428
429
  - lib/dato/upload/file.rb
429
430
  - lib/dato/upload/image.rb
430
431
  - lib/dato/utils/favicon_tags_builder.rb