Skip to content

Instantly share code, notes, and snippets.

@valachi
Created September 27, 2012 11:41
Show Gist options
  • Save valachi/3793582 to your computer and use it in GitHub Desktop.
Save valachi/3793582 to your computer and use it in GitHub Desktop.
$ ->
return unless $('#uploads').length
queue = do ->
data = []
current = 0
uploading = false
{
add: (item) ->
data.push item
# queue.upload()
upload: ->
return if uploading or not data[current]
uploading = true
data[current].upload ->
uploading = false
current += 1
queue.upload()
}
class UploadItem
storage = $('#upload-items')
template = storage.find('.template > *').detach()
constructor: (@video, @sub) ->
element = template.clone().appendTo storage
element.find('.name').text @video.name
@status = element.find('.status')
@episode = new Episode @video
updateStatus: (info) ->
@status.text info
upload: (callback) ->
form = new FormData
xhr = new XMLHttpRequest
xhr.open 'POST', '/upload', true
xhr.onload = (event) =>
@updateStatus if xhr.status == 200 then 'Готово' else 'Ошибка'
callback()
xhr.upload.onprogress = (progress) =>
percent = parseInt progress.loaded / progress.total * 100
@updateStatus if percent == 100 then 'Обрабатывается' else percent + '%'
form.append 'video[file]', @video
form.append 'video[subtitle]', @sub
form.append 'video[show_name]', @episode.show
form.append 'video[season]', @episode.season
form.append 'video[number]', @episode.number
xhr.send form
$('#uploads button').click ->
$('#uploads input').click()
sortFilesByName: (a, b) ->
if a.name > b.name
1
else if b.name > a.name
-1
else
0
$('#uploads input').change (event) ->
anyVideos = false
allFiles = event.target.files
videos = (file for file in allFiles when /(mkv|avi|mp4|webm)$/.test file.name)
subtitles = (file for file in allFiles when /srt$/.test file.name)
for file in videos.sort() when /(mkv|avi|mp4|webm)$/.test file.name
anyVideos = true
episode = new Episode file
subtitle = (sub for sub in subtitles when episode.isSame sub)[0]
unless episode.show
alert("Не удалось распарсить имя видео: #{file.name}")
continue
unless subtitle
alert("Видео без субтитров: #{file.name}")
continue
queue.add new UploadItem file, subtitle
unless anyVideos
alert('В этой папке нету видео файлов и субтитров')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment