Class: Cnvrg::Files
- Inherits:
-
Object
- Object
- Cnvrg::Files
- Defined in:
- lib/cnvrg/files.rb
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
- #end_commit(commit_sha1) ⇒ Object
-
#initialize(owner, project_slug) ⇒ Files
constructor
A new instance of Files.
- #rollback_commit(commit_sha1) ⇒ Object
- #start_commit ⇒ Object
- #upload_file(absolute_path, relative_path, commit_sha1) ⇒ Object
- #upload_s3(url, file) ⇒ Object
- #upload_url(file_path) ⇒ Object
Constructor Details
#initialize(owner, project_slug) ⇒ Files
Returns a new instance of Files.
7 8 9 10 11 |
# File 'lib/cnvrg/files.rb', line 7 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.
5 6 7 |
# File 'lib/cnvrg/files.rb', line 5 def base_resource @base_resource end |
Instance Method Details
#create_dir(absolute_path, relative_path, commit_sha1) ⇒ Object
67 68 69 70 |
# File 'lib/cnvrg/files.rb', line 67 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
62 63 64 65 |
# File 'lib/cnvrg/files.rb', line 62 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
57 58 59 60 |
# File 'lib/cnvrg/files.rb', line 57 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
93 94 95 |
# File 'lib/cnvrg/files.rb', line 93 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
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/cnvrg/files.rb', line 72 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 |
#end_commit(commit_sha1) ⇒ Object
105 106 107 108 |
# File 'lib/cnvrg/files.rb', line 105 def end_commit(commit_sha1) response = Cnvrg::API.request("#{base_resource}/commit/end", 'POST', { commit_sha1: commit_sha1 } ) return response end |
#rollback_commit(commit_sha1) ⇒ Object
109 110 111 112 |
# File 'lib/cnvrg/files.rb', line 109 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
97 98 99 100 101 102 103 |
# File 'lib/cnvrg/files.rb', line 97 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
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/cnvrg/files.rb', line 13 def upload_file(absolute_path, relative_path, commit_sha1) file_name = File.basename relative_path file_size = File.size(relative_path).to_f 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}) if Cnvrg::CLI.is_response_success(upload_resp, false) path = upload_resp["result"]["path"] s3_res = upload_s3(path,relative_path) 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"]["blob_id"]}) return true end end return false end |
#upload_s3(url, file) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/cnvrg/files.rb', line 29 def upload_s3(url,file) url = URI.parse(url) mime_type = MimeMagic.by_path(file) content_type = !mime_type.nil? ? mime_type.type : "" 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 return false end end |
#upload_url(file_path) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/cnvrg/files.rb', line 48 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 |