Class: Wikiwiki::CLI::Commands::Attachment::Put

Inherits:
Base
  • Object
show all
Defined in:
lib/wikiwiki/cli/commands/attachment/put.rb

Overview

Upload attachment to a page

Instance Method Summary collapse

Instance Method Details

#call(page_name:, file_path:, name: nil, force: false, out: $stdout, err: $stderr) ⇒ void

This method returns an undefined value.

Execute the put command

Parameters:

  • page_name (String)

    name of the page

  • file_path (String)

    local file path to upload

  • name (String, nil) (defaults to: nil)

    optional attachment name (inferred from file_path if nil)

  • force (Boolean) (defaults to: false)

    whether to overwrite existing attachment

  • out (IO) (defaults to: $stdout)

    output stream

  • err (IO) (defaults to: $stderr)

    error stream



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/wikiwiki/cli/commands/attachment/put.rb', line 28

def call(page_name:, file_path:, name: nil, force: false, out: $stdout, err: $stderr, **)
  wiki = create_wiki(out:, err:, **)

  attachment_name = name || File.basename(file_path)
  content = File.binread(file_path)

  if content.bytesize > MAX_ATTACHMENT_SIZE
    raise ArgumentError, "File size (#{content.bytesize} bytes) exceeds maximum allowed size (#{MAX_ATTACHMENT_SIZE} bytes / 512 KiB)"
  end

  begin
    wiki.add_attachment(page_name:, attachment_name:, content:)
  rescue ConflictError
    raise ArgumentError, "Attachment '#{attachment_name}' already exists. Use --force to overwrite." unless force

    wiki.delete_attachment(page_name:, attachment_name:)
    retry
  end

  say("Attachment '#{attachment_name}' uploaded to page '#{page_name}'", out:, **)
end