Module: Async::Caldav::Handlers::Move

Defined in:
lib/async/caldav/handlers/move.rb

Class Method Summary collapse

Class Method Details

.call(path:, storage:, headers: {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/async/caldav/handlers/move.rb', line 14

def call(path:, storage:, headers: {}, **)
  destination = headers['destination']
  return [400, { 'content-type' => 'text/plain' }, ['Missing Destination header']] unless destination

  to_path = URI.parse(destination).path
  overwrite = headers['overwrite'] != 'F'

  source = storage.get_item(path.to_s)
  return [404, { 'content-type' => 'text/plain' }, ['Not Found']] unless source

  existing = storage.get_item(to_path)

  if existing && !overwrite
    return [412, { 'content-type' => 'text/plain' }, ['Precondition Failed']]
  end

  # UID conflict check when destination doesn't already exist
  if !existing
    uid = extract_uid(source[:body])
    if uid
      dest_col = Protocol::Caldav::Path.new(to_path).parent.to_s
      items = storage.list_items(dest_col)
      items.each do |item_path, item_data|
        next if item_path == path.to_s
        if extract_uid(item_data[:body]) == uid
          return [409, { 'content-type' => 'text/plain' }, ['UID conflict']]
        end
      end
    end
  end

  storage.move_item(path.to_s, to_path)

  if existing
    [204, {}, ['']]
  else
    [201, {}, ['']]
  end
end