require 'open-uri' require 'base64' require 'helix_web_services_client/open_model' class HelixWebServicesClient # General file browsing method. # # The `path` parameter should be a directory location, starting with a # depot location, e.g., `my_depot/dir1`. # # When path is empty, will return the list of depots. # # Note: if path happens to be a file, instead of a directory, this is # a synonym with the file() method. # # If the path contains wildcards, the return will only be the list of # HelixWebServicesClient::Models::File instances. # # See also the HelixWebServicesClient::Models::File, HelixWebServicesClient::Models::Dir, # and HelixWebServicesClient::Models::Depot for the output types. def files(path = '') unless path.empty? path = path.split('/').map { |p| URI.encode(p) }.join('/') end arr = nil if wildcards?(path) arr = execute_method_no_body(:get, hve_path('files'), path: path) else arr = execute_method_no_body(:get, hve_path("files/#{path}")) end if arr.is_a?(Array) arr.map{ |x| OpenModel.new(x) } else m = OpenModel.new(arr) m.Content = Base64.decode64(m.content) m end end # Returns the file metadata at this location, with the `content` field # filled out. # # If path happens to be a directory, this method is synonymous with the # files() method. def file(path) files(path) end # Upload a single file's content. # # The `file` hash should contain the following fields: # # - `DepotFile`: target depot path # - `Content`: file content # # @param file [Hash] See description def upload_file(file) file = OpenModel.new(file) unless file.is_a?(OpenModel) path = encode_path(file.depot_file) body = { 'DepotFile': file.depot_file, 'Content': Base64.encode64(file.content) } execute_method_with_body(:patch, hve_path("files/#{path}"), nil, body) end # Upload multiple files # # Each file in the `Files` array should have two fields # # - `DepotFile`: target path, can be relative if `path` is indicated # - `Content`: File content # # The optional `path` parameter can indicate the root directory for all # files. # # @param files [Array] See description # @param path [String] If set, the root directory for all files # @param description [String] Informative message about the change def upload_files(files: [], path: nil, description: nil) files = files.map do |f| OpenModel.new(f) unless f.is_a?(OpenModel) end path = path ? encode_path(path) : '' obj = { 'Files' => files.map do |f| { 'DepotFile' => f.depot_file, 'Content' => Base64.encode64(f.content) } end } obj['Description'] = description if description execute_method_with_body(:patch, hve_path("files/#{path}"), nil, obj) end def delete_file(path) path = encode_path(path) execute_method_no_body(:delete, hve_path("files/#{path}")) end private def encode_path(path) path.split('/').map { |p| URI.encode(p) }.join('/') end def wildcards?(path) return !(path =~ /\.\.\./ || path =~ /\*/).nil? if path false end end