Class: Mailscope::Storage::Filesystem

Inherits:
Base
  • Object
show all
Defined in:
lib/mailscope/storage/filesystem.rb

Overview

One directory per message:

<location>/<id>/metadata.json   index used by the list view
<location>/<id>/mail.eml        pristine RFC822 source

Ids are lexicographically sortable by time, so listing never stats files.

Constant Summary collapse

ID_FORMAT =
/\A[0-9]{8}-[0-9]{6}-[0-9]{6}-[0-9a-f]{8}\z/
LEGACY_ID_FORMAT =

letter_opener / letter_opener_web laid out directories like "1358825621_ba83a22"; we still accept those for read-only migration.

/\A[0-9]+_[0-9]+_?[0-9a-f]+\z|\A[0-9]+_[0-9a-f]+\z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(location:) ⇒ Filesystem

Returns a new instance of Filesystem.



23
24
25
26
# File 'lib/mailscope/storage/filesystem.rb', line 23

def initialize(location:)
  super()
  @location = Pathname(location.to_s)
end

Instance Attribute Details

#locationObject (readonly)

Returns the value of attribute location.



21
22
23
# File 'lib/mailscope/storage/filesystem.rb', line 21

def location
  @location
end

Instance Method Details

#allObject



28
29
30
# File 'lib/mailscope/storage/filesystem.rb', line 28

def all
  ids.map { |id| build(id) }.compact
end

#clearObject



70
71
72
73
74
75
76
77
78
# File 'lib/mailscope/storage/filesystem.rb', line 70

def clear
  return 0 unless location.directory?

  removed = ids.size
  # Empty the directory instead of removing it, so a watcher holding the
  # path (or a mounted volume) keeps working.
  Dir.children(location).each { |child| FileUtils.rm_rf(location.join(child).to_s) }
  removed
end

#delete(id) ⇒ String?

Returns the id that was removed, or nil if it was gone.

Returns:

  • (String, nil)

    the id that was removed, or nil if it was gone



62
63
64
65
66
67
68
# File 'lib/mailscope/storage/filesystem.rb', line 62

def delete(id)
  dir = dir_for(id)
  return nil unless dir&.directory?

  FileUtils.rm_rf(dir.to_s)
  id
end

#find(id) ⇒ Object



32
33
34
35
# File 'lib/mailscope/storage/filesystem.rb', line 32

def find(id)
  id = validate_id!(id)
  build(id)
end

#prune(max_letters: nil, max_age: nil) ⇒ Object



80
81
82
83
# File 'lib/mailscope/storage/filesystem.rb', line 80

def prune(max_letters: nil, max_age: nil)
  removed = max_age ? prune_by_age(max_age) : 0
  removed + (max_letters ? prune_by_count(max_letters) : 0)
end

#read_attachment(id, attachment_id) ⇒ Object



57
58
59
# File 'lib/mailscope/storage/filesystem.rb', line 57

def read_attachment(id, attachment_id)
  find(id)&.attachment_body(attachment_id)
end

#read_raw(id) ⇒ Object



50
51
52
53
54
55
# File 'lib/mailscope/storage/filesystem.rb', line 50

def read_raw(id)
  path = path_for(id, 'mail.eml')
  return File.binread(path).force_encoding(Encoding::UTF_8).scrub if path&.file?

  legacy_raw(id)
end

#revisionObject



85
86
87
# File 'lib/mailscope/storage/filesystem.rb', line 85

def revision
  "#{ids.size}:#{ids.first}"
end

#store(mail) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mailscope/storage/filesystem.rb', line 37

def store(mail)
  id  = generate_id
  dir = location.join(id)
  FileUtils.mkdir_p(dir)

  raw = mail.encoded
  File.binwrite(dir.join('mail.eml'), raw)

   = Message.(mail, id: id, size: raw.bytesize)
  write_json(dir.join('metadata.json'), )
  id
end