Created
January 12, 2012 09:50
-
-
Save jamiehodge/1599625 to your computer and use it in GitHub Desktop.
Sinatra API for resumable.js
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
require 'sinatra' | |
require 'slim' | |
require 'coffee-script' | |
require 'sass' | |
require 'sequel' | |
DB = Sequel.sqlite | |
DB.create_table :uploads do | |
String :id, text: true, primary_key: true | |
String :filename | |
Integer :total_num_chunks | |
index :id | |
end | |
DB.create_table :chunks do | |
primary_key :id | |
foreign_key :upload_id, :uploads, type: :text | |
Integer :number | |
index [:upload_id, :number] | |
end | |
class Chunk < Sequel::Model | |
many_to_one :upload | |
attr_accessor :tempfile | |
def after_create | |
super | |
FileUtils.cp tempfile, "uploads/#{upload_id}/#{number}" | |
end | |
end | |
class Upload < Sequel::Model | |
unrestrict_primary_key | |
one_to_many :chunks | |
plugin :association_dependencies | |
add_association_dependencies \ | |
chunks: :destroy | |
def done? | |
chunks.count == total_num_chunks | |
end | |
def after_create | |
super | |
FileUtils.mkdir "uploads/#{id}" | |
end | |
def before_destroy | |
`ls uploads/#{id} | sort -n | xargs -I {} cat uploads/#{id}/{} > "assets/#{filename}"` | |
FileUtils.remove_dir "uploads/#{id}" | |
super | |
end | |
end | |
get '/new' do | |
slim :new | |
end | |
before '/' do | |
@id = params[:resumableIdentifier] || halt(415) | |
@filename = params[:resumableFilename] | |
@chunk_number = params[:resumableChunkNumber].to_i | |
@chunk_size = params[:resumableChunkSize].to_i | |
@total_size = params[:resumableTotalSize].to_i | |
@total_num_chunks = (@total_size / @chunk_size).ceil + 1 | |
@upload = Upload.find_or_create( | |
id: @id, | |
filename: @filename, | |
total_num_chunks: @total_num_chunks | |
) | |
end | |
get '/' do | |
not_found unless @upload.chunks_dataset.first(number: @chunk_number) | |
end | |
post '/' do | |
@upload.add_chunk(number: @chunk_number, tempfile: params[:file][:tempfile]) | |
@upload.destroy if @upload.done? | |
200 | |
end | |
__END__ | |
@@ layout | |
html | |
head | |
script src='//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js' | |
script src='/js/resumable.js' | |
sass: | |
.drop | |
width: 50% | |
margin | |
left: auto | |
right: auto | |
height: 50% | |
background-color: lightgrey | |
border: 5px dashed grey | |
position: relative | |
.progress | |
width: 100% | |
background-color: green | |
position: absolute | |
bottom: 0 | |
body | |
== yield | |
coffee: | |
r = new Resumable() | |
r.assignDrop $('.drop') | |
r.on 'fileAdded', (file) -> | |
r.upload() | |
r.on 'fileSuccess', (file, message) -> | |
r.removeFile(file) | |
r.on 'fileError', (file, message) -> | |
$('.progress').css('background-color', 'red') | |
r.removeFile(file) | |
r.on 'fileProgress', (file) -> | |
$('.progress').css('height', "#{r.progress() * 100}%") | |
@@ new | |
.drop | |
Drop here | |
.progress |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment