Module: Neofiles

Defined in:
lib/neofiles.rb,
lib/neofiles/engine.rb,
lib/neofiles/version.rb

Defined Under Namespace

Modules: DataStore, NeofilesHelper, NotFound Classes: AdminController, AdminTestController, Engine, File, FileChunk, FilesController, Image, ImagesController, Swf

Constant Summary collapse

VERSION =
'2.1.0'
@@routes_proc =
proc do
  scope 'neofiles', module: :neofiles do
    # admin routes
    get  '/admin/file_compact/', to: 'admin#file_compact', as: :neofiles_file_compact
    post '/admin/file_save/', to: 'admin#file_save', as: :neofiles_file_save
    post '/admin/file_remove/', to: 'admin#file_remove', as: :neofiles_file_remove
    post '/admin/file_update/', to: 'admin#file_update', as: :neofiles_file_update
     # admin routes for WYSIWYG editor Redactor.js
    post '/admin/redactor-upload/', to: 'admin#redactor_upload', as: :neofiles_redactor_upload
    get  '/admin/redactor-list/:owner_type/:owner_id/:type', to: 'admin#redactor_list', as: :neofiles_redactor_list
     # web frontend for serving images and other files
    get  '/serve/:id', to: 'files#show', as: :neofiles_file
    get  '/serve-image/:id(/:format(/c:crop)(/q:quality))', to: 'images#show', as: :neofiles_image, format: false, constraints: {format: /[1-9]\d*x[1-9]\d*/, crop: /[10]/, quality: /[1-9]\d*/}
     # serve images w/o watermark - path has prefix nowm_ to let Nginx ot other web server not cache these queries,
    # unlike usual /serve-image/:id
    get  '/nowm-serve-image/:id', to: 'images#show', as: :neofiles_image_nowm, defaults: {nowm: true}
  end
end

Class Method Summary collapse

Class Method Details

.crop_requested?(params) ⇒ Boolean

Is request params hash contains crop request?



80
81
82
# File 'lib/neofiles.rb', line 80

def crop_requested?(params)
  params[:crop].present? and params[:crop] != '0'
end

.is_admin?(context) ⇒ Boolean

Is current user considered “admin”? “Admin” means the user can fetch images w/o watermarks.



95
96
97
# File 'lib/neofiles.rb', line 95

def is_admin?(context)
  Rails.application.config.neofiles.try(:current_admin).try(:call, context)
end

.quality_requested(params) ⇒ Object

The quality value requested, from request params hash.



90
91
92
# File 'lib/neofiles.rb', line 90

def quality_requested(params)
  params[:quality].to_i if params[:quality].present? and params[:quality] != '0'
end

.quality_requested?(params) ⇒ Boolean

Is request params hash contains quality request?



85
86
87
# File 'lib/neofiles.rb', line 85

def quality_requested?(params)
  !!quality_requested(params)
end

.resized_image_dimensions(image_file, width, height, resize_options) ⇒ Object

Calculate image dimensions after resize. Returns [w, h] or nil if some info is lacking (e.g. image passed as ID so no width & height available).

image_file      - Neofiles::Image, ID or Hash
width, height   - max width and height after resize
resize_options  - {crop: '1'/'0'}, @see Neofiles::ImagesController#show


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/neofiles.rb', line 38

def resized_image_dimensions(image_file, width, height, resize_options)
  # dimensions are equal to requested ones if cropping
  return width, height if crop_requested? resize_options

  # otherwise ask ImageMagick - prepare input vars...
  image_file = Neofiles::Image.find image_file if image_file.is_a?(String)
  return nil if image_file.nil?

  if image_file.is_a? Neofiles::Image
    image_file_width = image_file.width
    image_file_height = image_file.height
  elsif image_file.is_a? Hash
    image_file_width = image_file[:width]
    image_file_height = image_file[:height]
  end

  # no input, terminate
  return if image_file_width.blank? || image_file_height.blank?

  # image fits into requested dimensions, no resizing will occur
  return image_file_width, image_file_height if image_file_width <= width && image_file_height <= height

  in_aspect   = 1.0 * image_file_width / image_file_height
  out_aspect  = 1.0 * width / height

  if in_aspect > out_aspect
    # If image is more "flat", the output width will always be equal to the requested width,
    # and the output height will be less than the requested height
    height = nil
  else
    # If input image is more "stretched" vertically or its aspect ratio is equal to output aspect ratio,
    # the output height will be equal to the requested height, and the output width will be less than or equal to the requested width
    width = nil
  end

  AspectRatio.resize(image_file_width, image_file_height, width, height).map(&:to_i)

rescue
  nil
end