wolf_core 1.0.51 → 1.0.53

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: 37020ba24d3ca8b8e13d037e8db9dc989337d64b50e621d43610fc1f70dc15e6
4
- data.tar.gz: a7b52dd9118488af8b28924e6bb443a5891ab2adbec588e6c26fccbe8a37212c
3
+ metadata.gz: c18b0fcfb7ec3e6a05a8bdb147c43414754460c7e2c0d8f4023c2bb915847275
4
+ data.tar.gz: af53acdd4f7fbe265515c04398d95ff88c807f99586e6578724cdd121a32dddf
5
5
  SHA512:
6
- metadata.gz: f75446e2b98cb703a622c4002c1258427bdc292bb9c0f645b3902bb41c4882d55850a699bef39501fb33ee04aef4c7b3292b1a08bb7229259cc7258ab1df909d
7
- data.tar.gz: 11f65c8704286eccc44628859515427ae8aca5da0a7b4f6776eb68d9a13c8019ebbd4016d9d76779632042e45ab9fc72c3784fa813f20ae7e6a6ef5d09a18ec3
6
+ metadata.gz: 4c87bca3e3d0ad50d5abc09bf8a368b63469bc6d37632c2bcff5d98de43c6089919db1886aa72c8c2d73796dab0d235f549ffa6e194af007369c0ce0321c14ed
7
+ data.tar.gz: b2aca4b3223a0b596d02ac3b33461cc3cc68a6ac5af86501c6e29a7a76689d022c2296fe34b940b1c99bba61d6845f2363497e7e880c54c9ad9e552433d391f4
@@ -1,8 +1,10 @@
1
- require_relative "logging_utils" # rubocop:disable Style/FrozenStringLiteralComment
1
+ require_relative "logging_utils"
2
+ require_relative "string_utils"
2
3
 
3
4
  module WolfCore
4
- module FileUtils # rubocop:disable Style/Documentation
5
+ module FileUtils
5
6
  extend WolfCore::LoggingUtils
7
+ extend WolfCore::StringUtils
6
8
 
7
9
  module_function
8
10
 
@@ -85,28 +87,59 @@ module WolfCore
85
87
  end
86
88
 
87
89
  def generate_url_from_file(file:, api_key:)
88
- url = URI.parse("https://cdn.filestackcontent.com/#{api_key}/upload")
89
- form_data = [
90
- ['file', file.tempfile, file.original_filename]
91
- ]
92
-
90
+ url = URI.parse("https://www.filestackapi.com/api/store/S3?key=#{api_key}")
93
91
  request = Net::HTTP::Post.new(url)
94
- request.set_form(form_data, 'multipart/form-data')
92
+ request["Content-Type"] = "application/octet-stream"
93
+ request.body = bytes_from_file(file)
95
94
 
96
95
  response = Net::HTTP.start(url.hostname, url.port, use_ssl: true) do |http|
97
96
  http.request(request)
98
97
  end
99
98
 
100
- return response['url'] if response.success?
99
+ parse_json_error = nil
100
+ if response.is_a?(Net::HTTPSuccess)
101
+ begin
102
+ response_body = JSON.parse(response.body)
103
+ rescue => e
104
+ parse_json_error = {
105
+ message: e.message,
106
+ backtrace: e.backtrace,
107
+ }
108
+ end
109
+ return response_body['url'] if response_body['url'].present?
110
+ end
101
111
 
102
112
  raise_service_error({
103
113
  message: "Failed to generate url from file",
104
114
  url: url,
105
115
  file: file,
106
- form_data: form_data,
107
116
  request: request,
108
- response: response
117
+ response: response,
118
+ response_body: response.body,
119
+ parse_json_error: parse_json_error,
109
120
  })
110
121
  end
122
+
123
+ def bytes_from_file(file)
124
+ if file.is_a?(ActionDispatch::Http::UploadedFile)
125
+ file.read
126
+ elsif base64_encoded?(file)
127
+ base64_string_to_bytes(file)
128
+ else
129
+ raise_service_error({
130
+ message: "Can not generate bytes from this file",
131
+ file: file,
132
+ })
133
+ end
134
+ end
135
+
136
+ def base64_string_to_bytes(base64_string)
137
+ begin
138
+ bytes = Base64.strict_decode64(base64_string)
139
+ bytes.bytes
140
+ rescue ArgumentError
141
+ raise_service_error({ message: 'Failed to decode base64 string', base64_string: base64_string })
142
+ end
143
+ end
111
144
  end
112
145
  end
@@ -39,5 +39,12 @@ module WolfCore
39
39
  rescue URI::InvalidURIError
40
40
  false
41
41
  end
42
+
43
+ def base64_encoded?(str)
44
+ return false unless str.is_a?(String) && str.length % 4 == 0
45
+
46
+ base64_regex = /^[A-Za-z0-9+\/]+={0,2}$/
47
+ str.match?(base64_regex)
48
+ end
42
49
  end
43
50
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module WolfCore
4
- VERSION = "1.0.51"
4
+ VERSION = "1.0.53"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wolf_core
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.51
4
+ version: 1.0.53
5
5
  platform: ruby
6
6
  authors:
7
7
  - Javier Roncallo
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-12-24 00:00:00.000000000 Z
11
+ date: 2024-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty