-
Notifications
You must be signed in to change notification settings - Fork 403
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds basic file upload widget, dependencies and resources for attachm…
…ents Signed-off-by: Akash Manohar J <akash@akash.im>
- Loading branch information
Showing
9 changed files
with
161 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
class Kandan.Data.Channels | ||
@active_channel_id: ()-> | ||
Kandan.Helpers.Channels.get_active_channel_id() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/assets/javascripts/backbone/plugins/attachments.js.coffee
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class Kandan.Plugins.Attachments | ||
|
||
@plugin_namespace: "Kandan.Plugins.Attachments" | ||
|
||
@template: _.template(''' | ||
<form accept-charset="UTF-8" action="/channels/<%= channel_id %>/attachments.js" data-remote="true" html="{:multipart=>true}" id="file_upload" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓"> | ||
<input name="<%=csrf_param %>" type="hidden" value="<%= csrf_token %>"></div> | ||
<input id="channel_id_<%= channel_id %>" name="channel_id[<%= channel_id %>]" type="hidden"> | ||
<input id="file" name="file" type="file"> | ||
<input name="commit" type="submit" value="Upload"> | ||
</form> | ||
''') | ||
|
||
@channel_id: ()-> | ||
Kandan.Data.Channels.active_channel_id() | ||
|
||
@csrf_param: -> | ||
$('meta[name=csrf-param]').attr('content') | ||
|
||
|
||
@csrf_token: -> | ||
$('meta[name=csrf-token]').attr('content') | ||
|
||
|
||
@render: ($widget_el)-> | ||
$upload_form = @template({ | ||
channel_id: @channel_id(), | ||
csrf_param: @csrf_param(), | ||
csrf_token: @csrf_token() | ||
}) | ||
$widget_el.append($upload_form) | ||
# $widget_el.append($file_list) | ||
|
||
|
||
@init: ()-> | ||
Kandan.Widgets.register "attachments", @plugin_namespace | ||
|
||
|
||
Kandan.Plugins.register "Kandan.Plugins.Attachments" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# -*- coding: utf-8 -*- | ||
class AttachmentsController < ApplicationController | ||
|
||
before_filter :authenticate_user! | ||
|
||
# GET /attachments | ||
# GET /attachments.json | ||
def index | ||
@channel = Channel.find_by_name(params[:channel_id]) | ||
@attachments = @channel.attachments.order("created_at DESC") | ||
|
||
respond_to do |format| | ||
format.json { render :json => @attachments.to_json(:methods => :url) } | ||
end | ||
end | ||
|
||
# POST /attachments | ||
# POST /attachments.json | ||
def create | ||
@channel = Channel.find_by_name(params[:channel_id]) | ||
@attachment = Attachment.new(params[:attachment]) | ||
|
||
@attachment.user = current_user | ||
@attachment.channel = Channel.find_by_name(params[:channel_id]) | ||
@attachment.file = params[:file] | ||
|
||
respond_to do |format| | ||
if @attachment.save | ||
format.html { } | ||
format.js | ||
format.json { render json: @attachment, status: :created } | ||
else | ||
format.html { render action: "new" } | ||
format.js | ||
format.json { render json: @attachment.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# PUT /attachments/1 | ||
# PUT /attachments/1.json | ||
def update | ||
@attachment = Attachment.find_by_name(params[:id]) | ||
|
||
respond_to do |format| | ||
if @attachment.update_attributes(params[:attachment]) | ||
format.html { redirect_to @attachment, notice: 'Attachment was successfully updated.' } | ||
format.json { head :ok } | ||
else | ||
format.html { render action: "edit" } | ||
format.json { render json: @attachment.errors, status: :unprocessable_entity } | ||
end | ||
end | ||
end | ||
|
||
# DELETE /attachments/1 | ||
# DELETE /attachments/1.json | ||
def destroy | ||
@attachment = Attachment.find_by_name(params[:id]) | ||
@attachment.destroy | ||
|
||
respond_to do |format| | ||
format.html { redirect_to attachments_url } | ||
format.json { head :ok } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require 'paperclip' | ||
|
||
class Attachment < ActiveRecord::Base | ||
belongs_to :channel | ||
belongs_to :user | ||
belongs_to :message | ||
|
||
has_attached_file :file, | ||
:storage => :s3, | ||
:s3_credentials => { | ||
:access_key_id => ENV['S3_ACCESS_KEY_ID'], | ||
:secret_access_key => ENV['S3_SECRET_ACCESS_KEY'], | ||
:session_token => ENV['STS_SESSION_TOKEN'] | ||
}, | ||
:bucket => ENV['S3_BUCKET'], | ||
:url => "/:attachment/:id/:style/:basename.:extension", | ||
:path => "#{ENV['S3_PREFIX']}/:attachment/:id/:style/:basename.:extension" | ||
|
||
attr_accessible :file | ||
|
||
def url | ||
file.to_s | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
access_key_id: <%= ENV['S3_ACCESS_KEY_ID'] %> | ||
secret_access_key: <%= ENV['S3_SECRET_ACCESS_KEY'] %> | ||
bucket: <%= ENV['S3_BUCKET'] %> | ||
session_token: <%= ENV['STS_SESSION_TOKEN'] %> |