Module: Fclay::Attachment

Extended by:
ActiveSupport::Concern
Defined in:
lib/fclay/attachment.rb

Constant Summary collapse

CALLBACKS =
[:process,:upload,:delete]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fileObject

Returns the value of attribute file.



28
29
30
# File 'lib/fclay/attachment.rb', line 28

def file
  @file
end

Class Method Details

.resolve_file_url(navigation_complex_id, type, file_name, style = nil) ⇒ Object



244
245
246
247
248
249
250
251
252
253
254
# File 'lib/fclay/attachment.rb', line 244

def self.resolve_file_url(navigation_complex_id, type, file_name, style = nil)

  return "" if file_name.nil? || type.nil?

  path = "http://s3.amazonaws.com/#{Fclay.remote_storage.bucket_name}"
  path += "/navigation_complex/#{navigation_complex_id}" if navigation_complex_id
  path += "/#{type}"
  path += "/#{style.to_s}" if style
  path += "/#{file_name}"
  path
end

.upload(type, id) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/fclay/attachment.rb', line 74

def self.upload(type, id)
  type = type.safe_constantize
  return unless type
  uploading_object = type.find_by_id(id)
  uploading_object.try(:log,"Fclay::upload() called, uploading_object: #{uploading_object}, uploading_object.need_upload: #{uploading_object.try(:need_upload)}")
  return if !uploading_object || !uploading_object.need_upload

  Fclay::RemoteStorage::Provider.upload(uploading_object)

  # class method callback
  uploading_object.class.uploaded if uploading_object.class.respond_to?(:uploaded)
end

Instance Method Details

#create_dirsObject



163
164
165
166
167
168
169
# File 'lib/fclay/attachment.rb', line 163

def create_dirs

 (self.class.fclay_options[:styles].try(:keys) || [nil]).each do |style|
   FileUtils.mkdir_p(local_file_dir(style))
 end

end

#delete_filesObject



30
31
32
33
# File 'lib/fclay/attachment.rb', line 30

def delete_files
  return unless self.file_location
  self.file_location == 'local' ? delete_local_files : delete_remote_files
end

#delete_local_filesObject



223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/fclay/attachment.rb', line 223

def delete_local_files

   begin
      (self.class.fclay_options[:styles].try(:keys) || [nil]).each do |style|
        Fclay::DeleteJob.set(wait: 5.minutes).perform_later(local_file_path(style))
      end
   rescue
      Rails.logger.info "Deleting Media #{id} sync file not found"
   end
   true

end

#delete_remote_filesObject



236
237
238
# File 'lib/fclay/attachment.rb', line 236

def delete_remote_files
  Fclay::RemoteStorage::Provider.delete_files(self)
end

#delete_tmp_fileObject



158
159
160
161
# File 'lib/fclay/attachment.rb', line 158

def delete_tmp_file
   FileUtils.rm(@file.try(:path) || @file[:path], force: true) if @file
   @file = nil
end

#fclay_attachment_presenceObject



87
88
89
# File 'lib/fclay/attachment.rb', line 87

def fclay_attachment_presence
 errors.add(:file, 'must be present') if id.blank? && !@file
end

#fetch_extensionObject



216
217
218
219
220
221
# File 'lib/fclay/attachment.rb', line 216

def fetch_extension
  ext = self.class.fclay_options[:extension]
  return nil if ext == false
  return ext.to_s if ext
  @file.original_filename.split(".").try(:last) if @file.try(:original_filename)
end

#fetch_file_nameObject



205
206
207
208
209
210
211
212
213
214
# File 'lib/fclay/attachment.rb', line 205

def fetch_file_name

  return if self.file_name.present?
  ext = fetch_extension

  self.file_name = try(:fclay_attachment_filename)
  self.file_name = SecureRandom.hex unless self.file_name
  self.file_name += ".#{ext}" if ext

end

#file_size_mbObject



91
92
93
94
95
# File 'lib/fclay/attachment.rb', line 91

def file_size_mb

  "#{((self.file_size >> 10).to_f / 1024).round(2)} Mb" if self.file_size

end

#file_url(style = nil) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fclay/attachment.rb', line 97

def file_url(style=nil)
  return '' unless self.file_location

  case self.file_location
  when "external_link"
    self.file_name
  when "local"
    local_file_url(style)
  else
    remote_file_url(style)
  end

end

#final_file_url(style = nil) ⇒ Object



111
112
113
114
115
116
117
118
# File 'lib/fclay/attachment.rb', line 111

def final_file_url(style=nil)
  return self.file_name if self.file_location == "external_link"
  if Fclay.configuration.storage_policy != :local
    remote_file_url(style)
  else
    local_file_url(style)
  end
end

#local_file_dir(style = nil) ⇒ Object



144
145
146
147
148
# File 'lib/fclay/attachment.rb', line 144

def local_file_dir(style=nil)
 dir = "#{Rails.root.to_s + Fclay.configuration.local_folder}/#{self.class.name.tableize}"
 dir += "/#{style.to_s}" if style
 dir
end

#local_file_path(style = nil) ⇒ Object



126
127
128
129
130
# File 'lib/fclay/attachment.rb', line 126

def local_file_path(style=nil)

   local_file_dir(style) + "/" + file_name

end

#local_file_url(style = nil) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/fclay/attachment.rb', line 132

def local_file_url(style=nil)
  url = Fclay.configuration.local_storage_host
  url += "#{Fclay.configuration.local_url}/#{self.class.name.tableize}"
  url += "/#{style.to_s}" if style
  url += "/#{file_name}"
  url
end

#need_uploadObject



56
57
58
# File 'lib/fclay/attachment.rb', line 56

def need_upload
  Fclay.configuration.storage_policy != :local && self.file_location == "local"
end

#process_fileObject



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/fclay/attachment.rb', line 171

def process_file
  self.try(:log,"process_file called")
  self.try(:log,"@file: #{@file.try(:to_s)}")
  return unless @file

  delete_files
  path = @file.try(:path) || @file.try(:[],:path)

  self.try(:log,"fetched path: #{path.try(:to_s)}")
  return unless path

  self.content_type = @file.try(:content_type) || @file.try(:[],:content_type) if self.respond_to?(:'content_type=')

  if path[0..3] == "http"
    self.file_status = 'idle'
    self.file_location = 'external_link'
    self.file_name = path
  else
    create_dirs
    fetch_file_name

    (self.class.fclay_options[:styles].try(:keys) || [nil]).each do |style|
      FileUtils.cp(path,local_file_path(style))
      `chmod 777 #{local_file_path(style)}`
    end
    self.original_file_name = @file.try(:original_filename) || @file.try(:[],:content_type)
    delete_tmp_file
    set_file_size self.class.fclay_options[:styles].try(:keys).try(:first)
    self.file_location = 'local'
    self.file_status = need_upload ? "processing" : "idle"
    self.try(:log,"file_processed,  file_status: #{self.file_status}")
  end
end

#process_uploadObject



47
48
49
50
51
52
53
54
# File 'lib/fclay/attachment.rb', line 47

def process_upload
  return unless need_upload
  if self.class.fclay_options[:processing] == :foreground
    upload
  else
    upload_later
  end
end

#remote_file_path(style = nil) ⇒ Object



150
151
152
153
154
155
156
# File 'lib/fclay/attachment.rb', line 150

def remote_file_path(style = nil)
  path = ""
  path += "#{self.class.name.tableize}"
  path += "/#{style.to_s}" if style
  path += "/#{file_name}"
  path
end

#remote_file_url(style = nil) ⇒ Object



120
121
122
123
124
# File 'lib/fclay/attachment.rb', line 120

def remote_file_url(style=nil)
  # host = Fclay.remote_storage.host || "https://#{Fclay.remote_storage.bucket_name}.s3.amazonaws.com"
  # "#{host}/#{remote_file_path(style)}"
  Fclay::RemoteStorage::Provider.remote_file_url(self, style)
end

#safe_delete_filesObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/fclay/attachment.rb', line 35

def safe_delete_files
  delete_files

  self.file_status = nil
  self.file_location = nil
  self.file_name = nil
  self.original_file_name = nil
  self.file_size = nil
  self.content_type = nil
  save
end

#set_file_size(style = nil) ⇒ Object



240
241
242
# File 'lib/fclay/attachment.rb', line 240

def set_file_size style=nil
  self.file_size = File.size local_file_path(style)
end

#short_local_file_url(style = nil) ⇒ Object



140
141
142
# File 'lib/fclay/attachment.rb', line 140

def short_local_file_url(style=nil)

end

#uploadObject



70
71
72
# File 'lib/fclay/attachment.rb', line 70

def upload
  Fclay::Attachment.upload self.class.name,self.id
end

#upload_laterObject



60
61
62
63
64
65
66
67
68
# File 'lib/fclay/attachment.rb', line 60

def upload_later

  self.try(:log,"upload_later() called, need_upload: #{need_upload}")
  if need_upload
    job = Fclay::UploadJob.perform_later(self.class.name,self.id)
    self.try(:log,"sheduled! job id: #{job.provider_job_id}")
  end

end