Class: Cnvrg::Files
- Inherits:
-
Object
- Object
- Cnvrg::Files
- Defined in:
- lib/cnvrg/files.rb
Constant Summary collapse
- LARGE_FILE =
1024*1024*100
Instance Attribute Summary collapse
-
#base_resource ⇒ Object
readonly
Returns the value of attribute base_resource.
Instance Method Summary collapse
- #create_dir(absolute_path, relative_path, commit_sha1) ⇒ Object
- #delete_dir(absolute_path, relative_path, commit_sha1) ⇒ Object
- #delete_file(absolute_path, relative_path, commit_sha1) ⇒ Object
- #download_dir(absolute_path, relative_path, project_home) ⇒ Object
- #download_file(absolute_path, relative_path, project_home, conflict = false) ⇒ Object
- #download_image(file_path_to_store, image_slug, owner) ⇒ Object
- #end_commit(commit_sha1) ⇒ Object
-
#initialize(owner, project_slug) ⇒ Files
constructor
A new instance of Files.
- #revoke_download_dir(absolute_path, relative_path, project_home) ⇒ Object
- #revoke_download_file(project_home, absolute_path, filename, conflict = false) ⇒ Object
- #rollback_commit(commit_sha1) ⇒ Object
- #start_commit ⇒ Object
- #upload_file(absolute_path, relative_path, commit_sha1) ⇒ Object
- #upload_image(absolute_path, image_name, owner) ⇒ Object
- #upload_large_files_s3(upload_resp, file_path) ⇒ Object
- #upload_small_files_s3(url, file, content_type) ⇒ Object
- #upload_url(file_path) ⇒ Object
Constructor Details
#initialize(owner, project_slug) ⇒ Files
12 13 14 15 16 |
# File 'lib/cnvrg/files.rb', line 12 def initialize(owner, project_slug) @project_slug = project_slug @owner = owner @base_resource = "users/#{owner}/projects/#{project_slug}/" end |
Instance Attribute Details
#base_resource ⇒ Object (readonly)
Returns the value of attribute base_resource.
10 11 12 |
# File 'lib/cnvrg/files.rb', line 10 def base_resource @base_resource end |
Instance Method Details
#create_dir(absolute_path, relative_path, commit_sha1) ⇒ Object
160 161 162 163 |
# File 'lib/cnvrg/files.rb', line 160 def create_dir(absolute_path, relative_path, commit_sha1) response = Cnvrg::API.request(@base_resource + "create_dir", 'POST', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1}) return Cnvrg::CLI.is_response_success(response, false) end |
#delete_dir(absolute_path, relative_path, commit_sha1) ⇒ Object
155 156 157 158 |
# File 'lib/cnvrg/files.rb', line 155 def delete_dir(absolute_path, relative_path, commit_sha1) response = Cnvrg::API.request(@base_resource + "delete_dir", 'DELETE', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1}) return Cnvrg::CLI.is_response_success(response, false) end |
#delete_file(absolute_path, relative_path, commit_sha1) ⇒ Object
150 151 152 153 |
# File 'lib/cnvrg/files.rb', line 150 def delete_file(absolute_path, relative_path, commit_sha1) response = Cnvrg::API.request(@base_resource + "delete_file", 'DELETE', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1}) return Cnvrg::CLI.is_response_success(response, false) end |
#download_dir(absolute_path, relative_path, project_home) ⇒ Object
186 187 188 |
# File 'lib/cnvrg/files.rb', line 186 def download_dir(absolute_path, relative_path, project_home) FileUtils.mkdir_p("#{project_home}/#{absolute_path}") end |
#download_file(absolute_path, relative_path, project_home, conflict = false) ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/cnvrg/files.rb', line 165 def download_file(absolute_path, relative_path, project_home, conflict=false) res = Cnvrg::API.request(@base_resource + "download_file", 'POST', {absolute_path: absolute_path, relative_path: relative_path}) Cnvrg::CLI.is_response_success(res, false) if res["result"] res = res["result"] return false if res["link"].empty? or res["filename"].empty? filename = res["filename"] file_location = absolute_path.gsub(/#{filename}\/?$/, "") FileUtils.mkdir_p project_home + "/" + file_location filename += ".conflict" if conflict File.open("#{project_home}/#{file_location}/#{filename}", "wb") do |file| file.write open(res["link"]).read end else return false end return true end |
#download_image(file_path_to_store, image_slug, owner) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/cnvrg/files.rb', line 67 def download_image(file_path_to_store, image_slug, owner) download_resp = Cnvrg::API.request("users/#{owner}/images/#{image_slug}/" + "download", 'GET') path = download_resp["result"]["path"] if Cnvrg::CLI.is_response_success(download_resp, false) begin open(file_path_to_store, 'wb') do |file| file << open(path).read end rescue return false end return true else return false end end |
#end_commit(commit_sha1) ⇒ Object
213 214 215 216 |
# File 'lib/cnvrg/files.rb', line 213 def end_commit(commit_sha1) response = Cnvrg::API.request("#{base_resource}/commit/end", 'POST', {commit_sha1: commit_sha1}) return response end |
#revoke_download_dir(absolute_path, relative_path, project_home) ⇒ Object
189 190 191 |
# File 'lib/cnvrg/files.rb', line 189 def revoke_download_dir(absolute_path, relative_path, project_home) puts FileUtils.rmtree("#{absolute_path}") end |
#revoke_download_file(project_home, absolute_path, filename, conflict = false) ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/cnvrg/files.rb', line 193 def revoke_download_file(project_home,absolute_path,filename,conflict=false) begin file_location = absolute_path.gsub(/#{filename}\/?$/, "") filename += ".conflict" if conflict FileUtils.remove("#{file_location}/#{filename}") return true rescue return false end end |
#rollback_commit(commit_sha1) ⇒ Object
218 219 220 221 |
# File 'lib/cnvrg/files.rb', line 218 def rollback_commit(commit_sha1) response = Cnvrg::API.request("#{base_resource}/commit/rollback", 'POST', {commit_sha1: commit_sha1}) Cnvrg::CLI.is_response_success(response, false) end |
#start_commit ⇒ Object
205 206 207 208 209 210 211 |
# File 'lib/cnvrg/files.rb', line 205 def start_commit response = Cnvrg::API.request("#{base_resource}/commit/start", 'POST', {project_slug: @project_slug, username: @owner}) Cnvrg::CLI.is_response_success(response) return response end |
#upload_file(absolute_path, relative_path, commit_sha1) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/cnvrg/files.rb', line 18 def upload_file(absolute_path, relative_path, commit_sha1) file_name = File.basename relative_path file_size = File.size(absolute_path).to_f mime_type = MimeMagic.by_path(absolute_path) content_type = !(mime_type.nil? or mime_type.text?) ? mime_type.type : "text/plain" upload_resp = Cnvrg::API.request(@base_resource + "upload_file", 'POST_FILE', {absolute_path: absolute_path, relative_path: relative_path, commit_sha1: commit_sha1, file_name: file_name, file_size: file_size, file_content_type: content_type}) if Cnvrg::CLI.is_response_success(upload_resp, false) if file_size.to_f>= Cnvrg::Files::LARGE_FILE.to_f s3_res = upload_large_files_s3(upload_resp, absolute_path) else path = upload_resp["result"]["path"] s3_res = upload_small_files_s3(path, absolute_path, content_type) end if s3_res Cnvrg::API.request(@base_resource + "update_s3", 'POST', {path: path, commit_id: upload_resp["result"]["commit_id"], blob_id: upload_resp["result"]["id"]}) return true end end return false end |
#upload_image(absolute_path, image_name, owner) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/cnvrg/files.rb', line 43 def upload_image(absolute_path, image_name, owner) file_name = File.basename absolute_path file_size = File.size(absolute_path).to_f content_type = "application/gzip" upload_resp = Cnvrg::API.request("users/#{owner}/images/" + "upload", 'POST_FILE', {relative_path: absolute_path, file_name: file_name, image_name: image_name, file_size: file_size, file_content_type: content_type}) # puts upload_resp if Cnvrg::CLI.is_response_success(upload_resp, false) s3_res = upload_large_files_s3(upload_resp, absolute_path) if s3_res commit_resp = Cnvrg::API.request("users/#{owner}/images/#{upload_resp["result"]["id"]}/" + "commit", 'GET') if Cnvrg::CLI.is_response_success(commit_resp, false) return true else return false end end end return false end |
#upload_large_files_s3(upload_resp, file_path) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cnvrg/files.rb', line 90 def upload_large_files_s3(upload_resp, file_path) begin sts_path = upload_resp["result"]["path_sts"] uri = URI.parse(sts_path) http_object = Net::HTTP.new(uri.host, uri.port) http_object.use_ssl = true if uri.scheme == 'https' request = Net::HTTP::Get.new(sts_path) body = "" http_object.start do |http| response = http.request request body = response.read_body end URLcrypt::key = [body].pack('H*') s3 = Aws::S3::Resource.new( :access_key_id => URLcrypt.decrypt(upload_resp["result"]["sts_a"]), :secret_access_key => URLcrypt.decrypt(upload_resp["result"]["sts_s"]), :session_token => URLcrypt.decrypt(upload_resp["result"]["sts_st"]), :region => URLcrypt.decrypt(upload_resp["result"]["region"])) resp = s3.bucket(URLcrypt.decrypt(upload_resp["result"]["bucket"])).object(File.basename(file_path)).upload_file(file_path) return resp rescue => e puts e return false end return true end |
#upload_small_files_s3(url, file, content_type) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/cnvrg/files.rb', line 121 def upload_small_files_s3(url, file, content_type) url = URI.parse(url) file = File.open(file, "rb") body = file.read begin Net::HTTP.start(url.host) do |http| http.send_request("PUT", url.request_uri, body, { "content-type" => content_type, }) end return true rescue Interrupt return false rescue => e puts e return false end end |
#upload_url(file_path) ⇒ Object
140 141 142 143 144 145 146 147 148 |
# File 'lib/cnvrg/files.rb', line 140 def upload_url(file_path) response = Cnvrg::API.request(@base_resource + "upload_url", 'POST', {file_s3_path: file_path}) if Cnvrg::CLI.is_response_success(response, false) return response else return nil end end |