Class: Platform::Media::Media

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/platform/media/media.rb

Overview

– Copyright © 2011 Michael Berkovich

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Direct Known Subclasses

Image

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.generate_local_dirObject



57
58
59
# File 'app/models/platform/media/media.rb', line 57

def self.generate_local_dir
  rand(2 ** 32).to_s(16).rjust(8, "0").scan(/.{2}/).join("/")
end

Instance Method Details

#local_dirObject



27
28
29
# File 'app/models/platform/media/media.rb', line 27

def local_dir
  @local_dir ||= [Platform::Config.media_path, file_location].join("/")
end

#local_pathObject



31
32
33
# File 'app/models/platform/media/media.rb', line 31

def local_path
  @local_path ||= [local_dir, file_name].join("/")
end

#urlObject



35
36
37
# File 'app/models/platform/media/media.rb', line 35

def url
  [Platform::Config.media_directory, file_location, file_name].join("/")
end

#write(file, opts = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/platform/media/media.rb', line 39

def write(file, opts = {})
  self.content_type = 'image/png' 
  self.file_location = self.class.generate_local_dir
  self.file_name = "#{Platform::RandomPasswordGenerator.random_password}.png" 
  
  FileUtils.mkdir_p(local_dir) unless File.exist?(local_dir)

  # you could force resize the image, but not the job of this plugin
  # image = Magick::ImageList.new(file.path)
  # image = image.resize(opts[:size], opts[:size]) if opts[:size]
  # image.write(local_path)
  
  FileUtils.cp(file.path, local_path)
  FileUtils.chmod(0644, local_path)

  save
end