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



28
29
30
31
32
33
34
35
36
# File 'lib/boxlet/app/controller.rb', line 28

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.



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

def format
  @format
end

#paramsObject

Returns the value of attribute params.



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

def params
  @params
end

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

Instance Method Details

#action(action) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/boxlet/app/controller.rb', line 38

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



162
163
164
165
166
167
168
# File 'lib/boxlet/app/controller.rb', line 162

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



155
156
157
158
159
160
# File 'lib/boxlet/app/controller.rb', line 155

def file_list
  @format = :json

  uuid = @params[:uuid]
  stats.merge(assets: db.collection('assets').find({uuid: uuid}).to_a)
end

#flashbackObject



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/boxlet/app/controller.rb', line 180

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


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

def gallery
  @format = :html

  authorized_request do
    Templates.gallery
  end
end


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

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

#indexObject

actions



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

def index
  Templates.index
end

#push_filesObject



94
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
# File 'lib/boxlet/app/controller.rb', line 94

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



170
171
172
173
174
175
176
177
178
# File 'lib/boxlet/app/controller.rb', line 170

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



83
84
85
86
87
88
89
90
91
92
# File 'lib/boxlet/app/controller.rb', line 83

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