summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/controllers/application_controller.rb4
-rw-r--r--app/controllers/downloads_controller.rb22
-rw-r--r--app/views/downloads/index.html.erb44
-rw-r--r--config/routes.rb3
4 files changed, 39 insertions, 34 deletions
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb
index d83690e..a994170 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -2,4 +2,8 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
+
+ def not_found
+ raise ActionController::RoutingError.new('Not Found')
+ end
end
diff --git a/app/controllers/downloads_controller.rb b/app/controllers/downloads_controller.rb
index 300d0b1..2681b0a 100644
--- a/app/controllers/downloads_controller.rb
+++ b/app/controllers/downloads_controller.rb
@@ -1,3 +1,6 @@
+require 'aws-sdk-core/s3'
+require 'fileutils'
+
class DownloadsController < ApplicationController
before_action :set_download, only: [:show, :edit, :update, :destroy]
@@ -7,6 +10,25 @@ class DownloadsController < ApplicationController
@downloads = Download.all
end
+ # GET /downloads/:filename
+ def download
+ download = Download.find_by(filename: params[:filename]) or not_found
+
+ s3client = Aws::S3::Client.new()
+ s3obj = Aws::S3::Object.new(ENV['S3_DOWNLOADS_BUCKET'], download.filename, client: s3client)
+ resp = s3obj.get
+
+ FileUtils.mkdir_p(Rails.root.join('tmp', 'downloads'))
+
+ cached_filename = Rails.root.join('tmp', 'downloads', download.filename)
+
+ File.open(cached_filename, 'wb') do |file|
+ file.write resp.body.read
+ end
+
+ send_file cached_filename
+ end
+
# GET /downloads/1
# GET /downloads/1.json
def show
diff --git a/app/views/downloads/index.html.erb b/app/views/downloads/index.html.erb
index c017aab..0f0afc9 100644
--- a/app/views/downloads/index.html.erb
+++ b/app/views/downloads/index.html.erb
@@ -1,35 +1,13 @@
<p id="notice"><%= notice %></p>
-<h1>Listing Downloads</h1>
-
-<table>
- <thead>
- <tr>
- <th>Name</th>
- <th>Filename</th>
- <th>Type</th>
- <th>Description</th>
- <th>Hits</th>
- <th colspan="3"></th>
- </tr>
- </thead>
-
- <tbody>
- <% @downloads.each do |download| %>
- <tr>
- <td><%= download.name %></td>
- <td><%= download.filename %></td>
- <td><%= download.type %></td>
- <td><%= download.description %></td>
- <td><%= download.hits %></td>
- <td><%= link_to 'Show', download %></td>
- <td><%= link_to 'Edit', edit_download_path(download) %></td>
- <td><%= link_to 'Destroy', download, method: :delete, data: { confirm: 'Are you sure?' } %></td>
- </tr>
- <% end %>
- </tbody>
-</table>
-
-<br>
-
-<%= link_to 'New Download', new_download_path %>
+<% @downloads.each do |download| %>
+ <article>
+ <h3><%= download.name %></h3>
+ <p><%= download.description %></p>
+ <p class="filedetails">
+ <span>Type:</span> <%= download.type %>
+ <span>Downloads:</span> <%= download.hits %>
+ </p>
+ <p><%= link_to "Download", download_path(download.filename) %></p>
+ </article>
+<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index 1486509..33b0274 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
- resources :downloads
+ get 'downloads' => 'downloads#index'
+ get 'downloads/:filename' => 'downloads#download', :filename => /.+/, :as => 'download'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".