Class: Boxlet::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/boxlet/app/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request) ⇒ Controller

Returns a new instance of Controller.



29
30
31
32
33
34
35
36
37
# 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

Instance Attribute Details

#formatObject

Returns the value of attribute format.



27
28
29
# File 'lib/boxlet/app/controller.rb', line 27

def format
  @format
end

#paramsObject

Returns the value of attribute params.



27
28
29
# File 'lib/boxlet/app/controller.rb', line 27

def params
  @params
end

#requestObject

Returns the value of attribute request.



27
28
29
# File 'lib/boxlet/app/controller.rb', line 27

def request
  @request
end

Instance Method Details

#action(action) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# 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_infoObject



163
164
165
166
167
168
169
# 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_listObject



156
157
158
159
160
161
# 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

#flashbackObject



181
182
183
184
185
186
187
188
189
190
191
192
193
# 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


195
196
197
198
199
200
201
# File 'lib/boxlet/app/controller.rb', line 195

def gallery
  @format = :html

  authorized_request do
    Templates.gallery
  end
end


203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/boxlet/app/controller.rb', line 203

def gallery_images
  @format = :json

  authorized_request do
    limit = (@params[:limit] || 50).to_i
    skip = ((params[:page] || 1).to_i - 1) * limit
    {
      count: db.collection('assets').count(),
      base_path: base_upload_path,
      images: db.collection('assets').find().limit(limit).skip(skip).to_a
    }
  end
end

#helloObject



217
218
219
# File 'lib/boxlet/app/controller.rb', line 217

def hello
  'hi'
end

#indexObject

actions



52
53
54
# File 'lib/boxlet/app/controller.rb', line 52

def index
  Templates.index
end

#push_filesObject



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
125
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
# 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

#resyncObject



171
172
173
174
175
176
177
178
179
# 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

#statsObject

uuid = @params

notifications = @params[:notifications]
# @user
"notifications"

end



84
85
86
87
88
89
90
91
92
93
# 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