Skip to content
Kward Search API index

Class: Kward::GeneratedImages

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/generated_images.rb

Overview

Owns unsaved preview bytes. Only the frontend can export or discard them.

Defined Under Namespace

Classes: Entry, Preview

Constant Summary collapse

Error =
Class.new(StandardError)
MAX_PREVIEWS =
16
FETCH_CHUNK_BYTES =
256 * 1024
LOCK_FLAGS =
File::RDWR | File::CREAT | File::NOFOLLOW

Instance Method Summary collapse

Constructor Details

#initialize(base: File.join(Dir.tmpdir, "kward-images-#{Process.uid}")) ⇒ GeneratedImages

Returns a new instance of GeneratedImages.



17
18
19
20
21
22
# File 'lib/kward/generated_images.rb', line 17

def initialize(base: File.join(Dir.tmpdir, "kward-images-#{Process.uid}"))
  @base = base
  @entries = {}
  @mutex = Mutex.new
  reap_crashed_runs
end

Instance Method Details

#add(owner, bytes, cancellation: nil) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/kward/generated_images.rb', line 24

def add(owner, bytes, cancellation: nil)
  raise Error, "Invalid or oversized PNG." unless CodexImages.valid_png?(bytes)

  @mutex.synchronize do
    cancellation&.raise_if_cancelled!
    raise Error, "Image preview storage is closed." if @closed
    raise Error, "Too many pending images; discard a preview before generating another." if @entries.size >= MAX_PREVIEWS

    initialize_run unless @run
    id = SecureRandom.hex(16)
    preview = Preview.new(id: id, filename: "kward-image-#{id[0, 8]}.png", root: File.realpath(owner.workspace_root), size: bytes.bytesize).freeze
    path = File.join(@run, "#{id}.png")
    begin
      File.open(path, File::WRONLY | File::CREAT | File::EXCL, 0o600) { |file| file.binmode.write(bytes) }
      cancellation&.raise_if_cancelled!
      @entries[id] = Entry.new(owner: owner, preview: preview, path: path)
    ensure
      FileUtils.rm_f(path) unless @entries.key?(id)
    end
    preview
  end
rescue SystemCallError
  raise Error, "Could not store the generated image preview."
end

#close ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/kward/generated_images.rb', line 100

def close
  @mutex.synchronize do
    @closed = true
    @entries.clear
    with_base_lock { FileUtils.remove_entry_secure(@run) } if @run && File.directory?(@run)
  ensure
    @run = nil
    @run_lock&.close
    @run_lock = nil
  end
rescue Error, SystemCallError
  # A later instance will reap the run if removal could not finish.
  nil
end

#discard(owner, id) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/kward/generated_images.rb', line 80

def discard(owner, id)
  @mutex.synchronize do
    entry = entry_for(owner, id)
    FileUtils.rm_f(entry.path)
    @entries.delete(id)
  end
  true
end

#discard_owner(owner) ⇒ Object



89
90
91
92
93
94
95
96
97
98
# File 'lib/kward/generated_images.rb', line 89

def discard_owner(owner)
  @mutex.synchronize do
    @entries.delete_if do |_id, entry|
      next false unless entry.owner.equal?(owner)

      FileUtils.rm_f(entry.path)
      true
    end
  end
end

#fetch(owner, id, offset: 0) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kward/generated_images.rb', line 53

def fetch(owner, id, offset: 0)
  raise Error, "Invalid image offset." unless offset.is_a?(Integer) && offset >= 0

  @mutex.synchronize do
    entry = entry_for(owner, id)
    raise Error, "Invalid image offset." if offset > entry.preview.size
    return [entry.preview, "".b] if offset == entry.preview.size

    bytes = File.binread(entry.path, FETCH_CHUNK_BYTES, offset)
    raise Error, "Image preview is unavailable or has been discarded." unless bytes

    [entry.preview, bytes]
  end
rescue SystemCallError
  raise Error, "Image preview is unavailable or has been discarded."
end

#path(owner, id) ⇒ Object



49
50
51
# File 'lib/kward/generated_images.rb', line 49

def path(owner, id)
  @mutex.synchronize { entry_for(owner, id).path }
end

#save_to_downloads(owner, id) ⇒ Object



74
75
76
77
78
# File 'lib/kward/generated_images.rb', line 74

def save_to_downloads(owner, id)
  export(owner, id, downloads_directory)
rescue SystemCallError
  raise Error, "Could not locate the Downloads folder."
end

#save_to_project(owner, id, directory = ".") ⇒ Object



70
71
72
# File 'lib/kward/generated_images.rb', line 70

def save_to_project(owner, id, directory = ".")
  export(owner, id, directory, project: true)
end