Skip to content
This repository has been archived by the owner on Jan 30, 2018. It is now read-only.

Commit

Permalink
Initial public download view for Folders
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Villalba authored and Artur Szwed committed Sep 2, 2011
1 parent 48baf7d commit 9c466b4
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 23 deletions.
23 changes: 16 additions & 7 deletions app/controllers/public_downloads_controller.rb
Expand Up @@ -12,11 +12,14 @@ class PublicDownloadsController < ApplicationController
:load_community_organization,
:add_chrome_frame_header

before_filter :get_upload_by_token
# TODO before_filter :set_headers no store, no cache
before_filter :get_upload_by_token, :only => [:download, :download_send]
before_filter :get_folder_by_token, :only => [:folder]

def download_send
def download
end

def download_send
# OPTIMIZE copied from uploads controller, may be encapsulated in a module for DRY

if !!Teambox.config.amazon_s3
Expand All @@ -33,16 +36,16 @@ def download_send
end

mime_type = File.mime_type?(@upload.asset_file_name)

mime_type = 'application/octet-stream' if mime_type == 'unknown/unknown'

send_file_options = {:type => mime_type}.merge(@extra_sendfile_options || {})

response.headers['Cache-Control'] = 'private, max-age=31557600'

send_file(path, send_file_options)
end
end

def folder
@folders = @folder.folders
@uploads = @folder.uploads
end

private
Expand All @@ -53,4 +56,10 @@ def get_upload_by_token
end
end

end
def get_folder_by_token
unless @folder = Folder.first
render :template => "public_downloads/not_found", :layout => "public_downloads", :status => :not_found and return
end
end

end
62 changes: 56 additions & 6 deletions app/styles/public_downloads.sass
@@ -1,5 +1,7 @@
@import shared

$upload_icon_width: 16px

body
background-color: #eee
font-family: 'Helvetica Neue', Helvetica, Arial, Geneva, sans-serif
Expand All @@ -25,19 +27,19 @@ body
&:hover
color: black

#content
#download_file
display: block
background: white
border: 1px solid #ccc
border-radius: 5px 5px
margin: 80px 0
+rounded(5px 5px)
margin: 10px 0 80px
padding: 15px
height: 80px
height: 50px
-webkit-user-select: none
&:active
position: relative
bottom: -2px
.file
.file_pane
position: relative
.info
display: inline-block
Expand Down Expand Up @@ -80,11 +82,59 @@ body
cursor: pointer
.fade
display: none
.file
.file_pane
.filename
a, p
color: white

h1
font-size: 20px
margin: 50px 0 10px
text-shadow: white 1px 1px 1px

#file_list
display: block
background: white
border: 1px solid #ccc
+rounded(5px 5px)
margin: 10px 0 80px
padding: 15px
-webkit-user-select: none

.upload
position: relative
font-size: 12px
padding: 4px 0 8px 5px
display: block
margin: 0
height: auto
&.alternate
background: lighten($navbar, 30)
&.selected
background-color: #ffe
.icon, .file, .size
display: inline-block
overflow: hidden
white-space: nowrap
text-overflow: ellipsis
.icon
width: $upload_icon_width
position: relative
top: 4px
.file
width: 290px
padding-left: 10px
a
color: black
text-decoration: none
&:hover
text-decoration: underline
.size
width: 75px
font-size: 10px
color: #777
margin: 0px 5px

#promo
text-align: center
a
Expand Down
20 changes: 11 additions & 9 deletions app/views/public_downloads/download.haml
@@ -1,13 +1,15 @@
- upload = @upload
- icon_path = "file_icons/48px/#{h upload.file_type}.png"

.file
.fade
- if Rails.root.join("public/images/#{icon_path}").exist?
.info
= link_to image_tag(icon_path), public_send_file_path(upload.token), :class => 'file_icon', :rel => (upload.image? ? 'facebox' : '')
%h1= t('.download_file')

.filename
%p.title= t('.download_file')
= link_to upload.asset_file_name, public_send_file_path(upload.token)
%p== #{number_to_human_size(upload.size)}, by #{upload.user.name}
#download_file
.file_pane
.fade
- if Rails.root.join("public/images/#{icon_path}").exist?
.info
= link_to image_tag(icon_path), public_send_file_path(upload.token), :class => 'file_icon', :rel => (upload.image? ? 'facebox' : '')

.filename
= link_to upload.asset_file_name, public_send_file_path(upload.token)
%p== #{number_to_human_size(upload.size)}, by #{upload.user.name}
13 changes: 13 additions & 0 deletions app/views/public_downloads/folder.haml
@@ -0,0 +1,13 @@
%h1= @folder
#file_list
- for folder in @folders
.upload{ :class => cycle('alternate', '') }
.icon= image_tag '/famfamfam/folder.png'
.file= link_to folder.name, public_download_folder_path(folder.token)
.size= pluralize (folder.uploads_count + folder.folders_count), 'file'
- for upload in @uploads
.upload.file_upload{ :class => cycle('alternate', '') }
.icon= image_tag file_icon_path(upload, '16px')
.file= link_to upload.asset_file_name, public_send_file_path(upload.token)
.size= number_to_human_size upload.size

1 change: 1 addition & 0 deletions config/routes.rb
Expand Up @@ -414,6 +414,7 @@ def self.matches?(request)

match 'downloads/:id(/:style)/:filename' => 'uploads#download', :constraints => {:filename => /.*/}, :via => :get

match 'f/:token' => 'public_downloads#folder', :via => :get, :as => :public_download_folder
match 'd/:token' => 'public_downloads#download', :via => :get, :as => :public_download_file
match 'send/:token' => 'public_downloads#download_send', :via => :get, :as => :public_send_file

Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20110826144729_add_token_to_folders.rb
@@ -0,0 +1,11 @@
class AddTokenToFolders < ActiveRecord::Migration
def self.up
add_column :folders, :token, :string
add_index :folders, :token
end

def self.down
remove_index :folders, :token
remove_column :folders, :token
end
end
5 changes: 4 additions & 1 deletion db/schema.rb
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20110818091722) do
ActiveRecord::Schema.define(:version => 20110826144729) do

create_table "activities", :force => true do |t|
t.integer "user_id"
Expand Down Expand Up @@ -188,8 +188,11 @@
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "deleted", :default => false, :null => false
t.string "token"
end

add_index "folders", ["token"], :name => "index_folders_on_token"

create_table "google_docs", :force => true do |t|
t.integer "project_id"
t.integer "user_id"
Expand Down
10 changes: 10 additions & 0 deletions lib/tokenized.rb
Expand Up @@ -4,6 +4,16 @@ def self.included(base)
base.before_validation :generate_token, :on => :create, :unless => lambda {self.token.present?}
end

# Generate a new token if undefined
def token
t = read_attribute(:token)
if t.nil? && !new_record?
generate_token
update_attribute :token, t
end
t
end

private

def generate_token
Expand Down

0 comments on commit 9c466b4

Please sign in to comment.