github.com/cozy/cozy-stack@v0.0.0-20240603063001-31110fa4cae1/tests/system/lib/nextcloud.rb (about)

     1  class Nextcloud
     2    def initialize(inst, account_id)
     3      @inst = inst
     4      @token = @inst.token_for "io.cozy.files"
     5      @base_path = "/remote/nextcloud/#{account_id}"
     6    end
     7  
     8    def list(path)
     9      opts = {
    10        accept: :json,
    11        authorization: "Bearer #{@token}"
    12      }
    13      res = @inst.client["#{@base_path}#{encode_path path}"].get opts
    14      JSON.parse(res.body)
    15    end
    16  
    17    def move(path, to)
    18      opts = {
    19        authorization: "Bearer #{@token}"
    20      }
    21      @inst.client["#{@base_path}/move#{encode_path path}?To=#{to}"].post nil, opts
    22    end
    23  
    24    def copy(path, name)
    25      opts = {
    26        authorization: "Bearer #{@token}"
    27      }
    28      @inst.client["#{@base_path}/copy#{encode_path path}?Name=#{name}"].post nil, opts
    29    end
    30  
    31    def mkdir(path)
    32      opts = {
    33        authorization: "Bearer #{@token}"
    34      }
    35      @inst.client["#{@base_path}#{encode_path path}?Type=directory"].put nil, opts
    36    end
    37  
    38    def upload(path, filename)
    39      mime = MiniMime.lookup_by_filename(filename).content_type
    40      content = File.read filename
    41      opts = {
    42        authorization: "Bearer #{@token}",
    43        :"content-type" => mime
    44      }
    45      @inst.client["#{@base_path}#{encode_path path}?Type=file"].put content, opts
    46    end
    47  
    48    def download(path)
    49      opts = {
    50        authorization: "Bearer #{@token}"
    51      }
    52      @inst.client["#{@base_path}#{encode_path path}?Dl=1"].get(opts).body
    53    end
    54  
    55    def upstream(path, from)
    56      opts = {
    57        authorization: "Bearer #{@token}"
    58      }
    59      @inst.client["#{@base_path}/upstream#{encode_path path}?From=#{from}"].post nil, opts
    60    end
    61  
    62    def downstream(path, to)
    63      opts = {
    64        authorization: "Bearer #{@token}"
    65      }
    66      @inst.client["#{@base_path}/downstream#{encode_path path}?To=#{to}"].post nil, opts
    67    end
    68  
    69    def delete(path)
    70      opts = {
    71        authorization: "Bearer #{@token}"
    72      }
    73      @inst.client["#{@base_path}#{encode_path path}"].delete opts
    74    end
    75  
    76    def encode_path(path)
    77      path.split("/").map { |s| ERB::Util.url_encode s }.join("/")
    78    end
    79  end