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

     1  class Folder
     2    ROOT_DIR = "io.cozy.files.root-dir".freeze
     3    TRASH_DIR = "io.cozy.files.trash-dir".freeze
     4    TRASH_PATH = "/.cozy_trash/".freeze
     5    SHARED_WITH_ME_DIR = "io.cozy.files.shared-with-me-dir".freeze
     6    NO_LONGER_SHARED_DIR = "io.cozy.files.no-longer-shared-dir".freeze
     7    CANCELLED_SUFFIX = "(partage annulé)".freeze
     8  
     9    include Model::Files
    10  
    11    attr_reader :name, :dir_id, :children, :path, :restore_path, :cozy_metadata
    12  
    13    def self.load_from_url(inst, path)
    14      opts = {
    15        content_type: :json,
    16        accept: :json,
    17        authorization: "Bearer #{inst.token_for doctype}"
    18      }
    19      res = inst.client[path].get opts
    20      j = JSON.parse(res.body)["data"]
    21      id = j["id"]
    22      rev = j.dig "meta", "rev"
    23      j = j["attributes"]
    24      f = Folder.new(
    25        name: j["name"],
    26        dir_id: j["dir_id"],
    27        path: j["path"],
    28        restore_path: j["restore_path"],
    29        cozy_metadata: j["cozyMetadata"]
    30      )
    31      f.couch_id = id
    32      f.couch_rev = rev
    33      f
    34    end
    35  
    36    def self.children(inst, path)
    37      path = "/files/metadata?Path=#{CGI.escape path}"
    38      opts = {
    39        content_type: :json,
    40        accept: :json,
    41        authorization: "Bearer #{inst.token_for doctype}"
    42      }
    43      res = inst.client[path].get opts
    44      j = JSON.parse(res.body)["included"]
    45  
    46      dirs = []
    47      files = []
    48      (j || []).map do |child|
    49        id = child["id"]
    50        rev = child.dig "meta", "rev"
    51        child = child["attributes"]
    52        type = child["type"]
    53        if type == "directory"
    54          f = Folder.new(
    55            name: child["name"],
    56            dir_id: child["dir_id"],
    57            path: child["path"],
    58            restore_path: child["restore_path"],
    59            cozy_metadata: child["cozyMetadata"]
    60          )
    61          dirs << f
    62        else
    63          f = CozyFile.new(
    64            name: child["name"],
    65            dir_id: child["dir_id"],
    66            trashed: child["trashed"],
    67            md5sum: child["md5sum"],
    68            size: child["size"],
    69            executable: child["executable"],
    70            file_class: child["class"],
    71            metadata: child["metadata"],
    72            cozy_metadata: child["cozyMetadata"]
    73          )
    74          files << f
    75        end
    76        f.couch_id = id
    77        f.couch_rev = rev
    78      end
    79      [dirs, files]
    80    end
    81  
    82    def self.clear_trash(inst)
    83      opts = {
    84        authorization: "Bearer #{inst.token_for doctype}"
    85      }
    86      inst.client["/files/trash"].delete opts
    87    end
    88  
    89    def restore_from_trash(inst)
    90      opts = {
    91        authorization: "Bearer #{inst.token_for doctype}"
    92      }
    93      res = inst.client["/files/trash/#{@couch_id}"].post nil, opts
    94      j = JSON.parse(res.body)["data"]
    95      @restore_path = nil
    96      @path = j.dig "attributes", "path"
    97      @dir_id = j.dig "attributes", "dir_id"
    98    end
    99  
   100    def initialize(opts = {})
   101      @name = opts[:name] || Faker::Internet.slug
   102      @dir_id = opts[:dir_id] || ROOT_DIR
   103      @path = opts[:path] || "/#{@name}"
   104      @restore_path = opts[:restore_path]
   105      @cozy_metadata = opts[:cozy_metadata]
   106      @children = []
   107    end
   108  
   109    def save(inst)
   110      opts = {
   111        accept: "application/vnd.api+json",
   112        authorization: "Bearer #{inst.token_for doctype}"
   113      }
   114      encoded_name = URI.encode_www_form_component(name)
   115      res = inst.client["/files/#{dir_id}?Type=directory&Name=#{encoded_name}"].post nil, opts
   116      j = JSON.parse(res.body)["data"]
   117      @couch_id = j["id"]
   118      @couch_rev = j["rev"]
   119      @cozy_metadata = j["attributes"]["cozyMetadata"]
   120    end
   121  
   122    def trashed
   123      @path.start_with? TRASH_PATH
   124    end
   125  
   126    def not_synchronized_on(inst, client_id)
   127      opts = {
   128        accept: "application/vnd.api+json",
   129        :"content-type" => "application/vnd.api+json",
   130        authorization: "Bearer #{inst.token_for doctype}"
   131      }
   132      body = JSON.generate data: [{ type: "io.cozy.oauth.clients", id: client_id }]
   133      url = "/files/#{@couch_id}/relationships/not_synchronized_on"
   134      inst.client[url].post body, opts
   135    end
   136  
   137    def synchronized_on(inst, client_id)
   138      opts = {
   139        accept: "application/vnd.api+json",
   140        :"content-type" => "application/vnd.api+json",
   141        authorization: "Bearer #{inst.token_for doctype}"
   142      }
   143      body = JSON.generate data: [{ type: "io.cozy.oauth.clients", id: client_id }]
   144      url = "http://#{inst.domain}/files/#{@couch_id}/relationships/not_synchronized_on"
   145      RestClient::Request.execute method: :delete, url: url, payload: body, headers: opts
   146    end
   147  end