Module: FsAttachable

Includes:
TR::CondUtils
Defined in:
lib/fs_attachable.rb,
lib/fs_attachable/version.rb,
lib/fs_attachable/attachable_config.rb

Defined Under Namespace

Modules: ClassMethods Classes: Config, ConfigDomain, ConfigError, DomainNotSet, Error, SourceFileNotFound

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.config(&block) ⇒ Object



16
17
18
# File 'lib/fs_attachable.rb', line 16

def self.config(&block)
  Config.instance.parse(&block) 
end

.included(klass) ⇒ Object



61
62
63
# File 'lib/fs_attachable.rb', line 61

def self.included(klass)
  klass.extend(ClassMethods)
end

.logger(tag = nil, &block) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/fs_attachable.rb', line 20

def self.logger(tag = nil, &block)
  if @_logger.nil?
    @_logger = TeLogger::Tlogger.new
  end

  if block
    if not_empty?(tag)
      @_logger.with_tag(tag, &block)
    else
      @_logger.with_tag(@_logger.tag, &block)
    end
  else
    if is_empty?(tag)
      @_logger.tag = :fs_att
      @_logger
    else
      # no block but tag is given? hmm
      @_logger.tag = tag
      @_logger
    end
  end

end

Instance Method Details

#find_attachment(name, opts = { raise_if_not_found: false }) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/fs_attachable.rb', line 157

def find_attachment(name, opts = { raise_if_not_found: false })

  opts = {} if opts.nil?
  raiseIfNotFound = opts[:raise_if_not_found]
  raiseIfNotFound = false if is_empty?(raiseIfNotFound) or not_bool?(raiseIfNotFound)

  objClass = opts[:object_class] || nil
  objId = opts[:object_id] || nil

  if not_empty?(objClass) and not_empty?(objId)
    attMeta = ModelFact.class_instance(:fsAtt_meta, true)
    rec = attMeta.record_with(objClass: objClass, objId: objId, name: name)
    if not_empty?(rec)
      @arec = rec.first
      pa = @arec.mstorage_path
    else
      raise SourceFileNotFound, "File '#{name}' is not found in storage of object '#{objClass}' and ID '#{objId}'"
    end
  else
    pa = File.join(config.root, name)
  end

  logger.debug "Looking for attachment at : #{pa}"
  if File.exist?(pa)
    [true, pa, @arec]
  else
    if raiseIfNotFound
      raise SourceFileNotFound, "File '#{name}' is not found in storage"
    else
      [false, nil]
    end
  end

end

#has_attachments?(name = nil, opts = {}) ⇒ Boolean Also known as: has_attachment?

Instance methods

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fs_attachable.rb', line 66

def has_attachments?(name = nil, opts = {})

  if is_empty?(name)
    tpath = config.root
    if File.exist?(tpath)
      files = Dir.entries(tpath)
      logger.debug "Files at managed storage '#{tpath}' : #{files.length}"
      files.length > 2
    else
      false
    end
  else
    res, path, _ = find_attachment(name, opts) 
    if res and File.directory?(path)
      false
    else
      res
    end
  end
end

#open_attachment(dest_file_name, opts = { }, &block) ⇒ Object

Raises:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/fs_attachable.rb', line 126

def open_attachment(dest_file_name, opts = { }, &block)

  raise Error, "Block is required for open_attachment" if not block
  raise Error, "Destination file name must be given" if is_empty?(dest_file_name)

  opts = {  } if opts.nil?

  if not_empty?(opts[:root])
    pa = File.join(config.root, opts[:root], dest_file_name)
    FileUtils.mkdir_p(File.dirname(pa)) if not File.exist?(File.dirname(pa))
  else
    pa = File.join(config.root, dest_file_name)
  end

  if block
    File.open(pa,"wb") do |f|
      block.call(f)
    end

    if not_empty?(opts[:object_class]) and not_empty?(opts[:object_id])
      attMeta = ModelFact.new_instance(:fsAtt_meta, true)
      attMeta.objClass = opts[:object_class]
      attMeta.objId = opts[:object_id]
      attMeta.mstorage_path = pa
      attMeta.save
    end
  end

  pa
end

#remove_attachment(name, opts = { }) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/fs_attachable.rb', line 192

def remove_attachment(name, opts = {  })
  
  res, path, rec = find_attachment(name, opts)
  if res
    FileUtils.remove_entry_secure(path)
    rec.destroy if not rec.nil?

    dir = File.dirname(path)
    if dir != "." and dir != config.root
      FileUtils.remove_dir(dir) if Dir.entries(dir).length == 2
    end
  end
  [res, path]
end

#remove_attachmentsObject



207
208
209
210
# File 'lib/fs_attachable.rb', line 207

def remove_attachments
  root = config.root
  FileUtils.remove_entry_secure(root, true)
end

#upload_attachment(path, opts = {}) ⇒ Object

Upload given path into managed storage

Raises:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fs_attachable.rb', line 91

def upload_attachment(path, opts = {})

  raise Error, "Source path must be given" if is_empty?(path)

  opts = {} if opts.nil?
  
  if File.exist?(path) 
  
    dest_file_name = opts[:dest_file_name]
    dest_file_name = File.basename(path) if is_empty?(dest_file_name)

    if not_empty?(opts[:root])
      pa = File.join(config.root, opts[:root], dest_file_name)
      FileUtils.mkdir_p(File.dirname(pa)) if not File.exist?(File.dirname(pa))
    else
      pa = File.join(config.root, dest_file_name)
    end

    FileUtils.cp(path, pa)

    if not_empty?(opts[:object_class]) and not_empty?(opts[:object_id])
      attMeta = ModelFact.new_instance(:fsAtt_meta, true)
      attMeta.objClass = opts[:object_class]
      attMeta.objId = opts[:object_id]
      attMeta.mstorage_path = pa
      attMeta.save
    end

    pa

  else
    raise SourceFileNotFound, "Given file '#{path}' not found"
  end
end