files.rb #1

  • //
  • guest/
  • ptomiak/
  • hws/
  • source/
  • helix_web_services_client/
  • lib/
  • helix_web_services_client/
  • files.rb
  • View
  • Commits
  • Open Download .zip Download (3 KB)
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
# Change User Description Committed
#1 15741 ptomiak Branch HWS for my use.
//guest/perforce_software/helix-web-services/main/source/helix_web_services_client/lib/helix_web_services_client/files.rb
#1 15622 tjuricek Move source code to 'source/' subdirectory of branch.

build/ will remain where it is.
//guest/perforce_software/helix-web-services/main/helix_web_services_client/lib/helix_web_services_client/files.rb
#4 15240 tjuricek Set api level via request path on all Helix Versioning Engine methods.

This will allow migration of applications to different P4D versions. Our internal methods (like project API) should attempt to handle backward compatibility similarly.

P4WEBAPI-118
#3 15189 tjuricek Update files implementation, testing, and documentation.
#2 15110 tjuricek Revise changes methods for new p4 connection handling, add server specs, remove model references in client, and update asciidoc documentation.
#1 13808 tjuricek Finish converting most of the core p4d API into the new monolithic system.
//guest/perforce_software/helix-web-services/main/p4_web_api/clients/ruby/p4_web_api_client/lib/p4_web_api_client/client/files.rb
#1 13412 tjuricek Initial version of the web-services mainline.

This is a collection of several projects, that will likely often get released together, though many of them may not always be relevant.

See the README for more information.