class Boxlet::Controller

Attributes

format[RW]
params[RW]
request[RW]

Public Class Methods

new(request) click to toggle source
# File lib/boxlet/app/controller.rb, line 29
def initialize(request)
  @request = request
  @params = Boxlet.symbolize_keys(request.params)
  Boxlet.log(:info, request.params)

  @format = :html
  @status = 200
  @headers = {}
end

Public Instance Methods

action(action) click to toggle source
# File lib/boxlet/app/controller.rb, line 39
def action(action)
  action_response = self.send(action)
  set_user if action =~ /push_files|file_list|file_info/
  
  {
    format: @format,
    content: action_response,
    status: @status,
    headers: @headers
  }
end
file_info() click to toggle source
# File lib/boxlet/app/controller.rb, line 163
def file_info
  @format = :json

  uuid = @params[:uuid]
  asset_path = @params[:asset_path]
  Boxlet::Models.file_model.merge db.collection('assets').find({asset_path: asset_path, uuid: uuid}).to_a.first || {}
end
file_list() click to toggle source
# File lib/boxlet/app/controller.rb, line 156
def file_list
  @format = :json

  uuid = @params[:uuid]
  stats.merge(assets: db.collection('assets').find({uuid: uuid}).to_a)
end
flashback() click to toggle source
# File lib/boxlet/app/controller.rb, line 181
def flashback
  @format = :json

  date = Date.parse(@params[:date])
  uuid = @params[:uuid]
  stats.merge(assets: db.collection('assets').find({
    uuid: uuid,
    asset_date: {
      '$gte' => date.to_time.strftime('%F'),
      '$lt' => (date + 1).to_time.strftime('%F')
    }
  }).to_a)
end
hello() click to toggle source
# File lib/boxlet/app/controller.rb, line 217
def hello
  'hi'
end
index() click to toggle source

actions

# File lib/boxlet/app/controller.rb, line 52
def index
  Templates.index
end
push_files() click to toggle source
# File lib/boxlet/app/controller.rb, line 95
def push_files
  @format = :json
  
  upload_path = user_upload_dir || './uploads'
  upload_file = @params[:file]
  asset_path = @params[:asset_path]
  asset_path_params = Rack::Utils.parse_nested_query(asset_path[asset_path.index('?') + 1..-1])

  new_filename = "#{asset_path_params["id"]}.#{asset_path_params["ext"]}"
  new_path = File.join(upload_path, new_filename)
  FileUtils.mv(upload_file[:tempfile].path, new_path)

  new_thumb_filename = "#{asset_path_params["id"]}-thumb.#{asset_path_params["ext"]}"
  new_thumb_path = File.join(upload_path, new_thumb_filename)

  if File.exists?(new_path)
    file = File.open(new_path, 'r')
    asset = {
      filename: new_filename,
      size: file.size,
      local_date: file.mtime.to_i,
      orientation: @params[:orientation],
      thumbnail: new_thumb_filename,
      asset_path: @params[:asset_path],
      asset_date: @params[:asset_date],
      uuid: @params[:uuid]
    }
    db.collection('assets').insert(asset)

    t = Thread.new do
      Image.resize(new_path, new_thumb_path, 150, 150)

      if Boxlet.config[:s3][:enabled]
        Boxlet.log(:debug, 'Uploading to S3...')

        s3 = AWS::S3.new(
          :access_key_id => Boxlet.config[:s3][:access_key_id],
          :secret_access_key => Boxlet.config[:s3][:secret_access_key]
        )
        if s3.buckets[Boxlet.config[:s3][:bucket]]
            .objects["#{@params[:uuid]}/#{new_filename}"]
            .write(:file => new_path) &&
          s3.buckets[Boxlet.config[:s3][:bucket]]
            .objects["#{@params[:uuid]}/#{new_thumb_filename}"]
            .write(:file => new_thumb_path)

          FileUtils.rm(new_path)
          FileUtils.rm(new_thumb_path)
          Boxlet.log(:debug, 'Uploading to S3... Done!')
        else
          Boxlet.log(:debug, 'Uploading to S3... Error!!')
        end
      end
    end

    {response: asset}
  else
    {response: false}
  end
end
resync() click to toggle source
# File lib/boxlet/app/controller.rb, line 171
def resync
  upload_dir = user_upload_dir || './uploads'
  db.collection('assets').find().each do |a|
    asset_path = a["uuid"] + "/" + a["filename"]
    if !File.exists? upload_dir + "/" + asset_path
      db.collection('assets').remove({"_id" => a["_id"]})
    end
  end
end
stats() click to toggle source
uuid = @params[:uuid]
notifications = @params[:notifications]
# @user
"notifications"

end

# File lib/boxlet/app/controller.rb, line 84
def stats
  @format = :json

  {
    capacity: Boxlet::Util.app_space_capacity,
    usage: Boxlet::Util.app_space_usage,
    free_space: free_space?,
    base_upload_path: base_upload_path
  }
end

Private Instance Methods

authorized_request(&action) click to toggle source
# File lib/boxlet/app/controller.rb, line 244
def authorized_request(&action)
  auth = Boxlet::Handlers::Auth.new(
    Boxlet.config[:gallery_username],
    Boxlet.config[:gallery_password]
  )
  @status, @headers, content = auth.authorize(request) do
    action.call
  end

  content
end
base_upload_path() click to toggle source
# File lib/boxlet/app/controller.rb, line 240
def base_upload_path
  Boxlet::Util.base_upload_path(@params[:uuid])
end
db() click to toggle source
# File lib/boxlet/app/controller.rb, line 224
def db
  Boxlet::Db.connection
end
free_space?() click to toggle source
# File lib/boxlet/app/controller.rb, line 236
def free_space?
  Boxlet::Util.free_space > 50
end
set_user() click to toggle source
# File lib/boxlet/app/controller.rb, line 228
def set_user
  Boxlet::Models.user_model.merge(db.collection('users').find({uuid: @params[:uuid]}).to_a.first || {})
end
user_upload_dir() click to toggle source
# File lib/boxlet/app/controller.rb, line 232
def user_upload_dir
  Boxlet::Util.user_upload_dir(@params[:uuid])
end