aboutsummaryrefslogtreecommitdiffhomepage
path: root/public/javascripts/index.js
blob: 3a02367c866d61c618d394403dc5bb9c5112f669 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
;(function () {
  'use strict'

  var $ = require('jquery')
  require('blueimp-file-upload')

  var WebTorrent = require('webtorrent')
  var client = new WebTorrent({ dht: false })

  var $content = $('#ajax_load')

  // Webtorrent events
  client.on('error', function (err) {
    console.error(err)
  })

  client.on('warning', function (err) {
    console.warning(err)
  })

  // Events of the panel
  $('#panel_get_videos').on('click', function () {
    getVideos()
  })

  $('#panel_upload_video').on('click', function () {
    uploadVideo()
  })

  $('#panel_make_friends').on('click', function () {
    makeFriends()
  })

  $('#panel_quit_friends').on('click', function () {
    quitFriends()
  })

  $('#search-video').on('keyup', function (e) {
    var search = $(this).val()

    if (search === '') return

    if (e.keyCode === 13) {
      $.ajax({
        url: '/api/v1/videos/search/' + search,
        type: 'GET',
        dataType: 'json',
        success: function (videos) {
          printVideos(videos)
        }
      })
    }
  })

  // Join a new network
  function makeFriends () {
    $.ajax({
      url: '/api/v1/pods/makefriends',
      type: 'GET',
      dataType: 'json',
      statusCode: {
        409: function () {
          alert('Already made friends.')
        }
      },
      success: function () {
        alert('Made friends!')
      }
    })
  }

  function quitFriends () {
    $.ajax({
      url: '/api/v1/pods/quitfriends',
      type: 'GET',
      dataType: 'json',
      success: function () {
        alert('Quit friends!')
      }
    })
  }

  function printVideos (videos) {
    $content.empty()

    if (videos.length === 0) {
      $content.text('There is no videos.')
    }

    videos.forEach(function (video) {
      var $video = $('<div></div>').addClass('video')

      var $video_name = $('<span></span>').addClass('video_name').text(video.name)
      var $video_pod = $('<span></span>').addClass('video_pod_url').text(video.podUrl)
      var $header = $('<div></div>').append([ $video_name, $video_pod ])

      if (video.namePath !== null) {
        var $remove = $('<span></span>').addClass('span_action glyphicon glyphicon-remove')

        // Remove the video
        $remove.on('click', function () {
          // TODO
          if (!confirm('Are you sure ?')) return

          removeVideo(video)
        })

        $header.append($remove)
      }

      var $video_description = $('<div></div>').addClass('video_description').text(video.description)

      // Get the video
      $video_name.on('click', function () {
        getVideo(video)
      })

      if (!video.magnetUri) {
        $remove.css('display', 'none')
      }

      $video.append([ $header, $video_description ])
      $content.append($video)
    })
  }

  // Upload the video, the server will seed it
  function uploadVideo () {
    // Creating all the elements
    var $video_label = $('<label></label>').attr('for', 'name').text('Video name')
    var $video_name = $('<input></input>').addClass('form-control').attr({
      name: 'name',
      id: 'name'
    })
    var $video_block = $('<div></div>').addClass('form-group').append([ $video_label, $video_name ])

    var $title = $('<h3></h3>').text('Upload a video')

    var $button_text = $('<span></span>').text('Select the video...')
    var $input_video = $('<input></input>').attr({
      type: 'file',
      name: 'input_video',
      id: 'input_video'
    })
    var $button = $('<div></div>').addClass('btn btn-default btn-file').append([ $button_text, $input_video ])

    var $description_label = $('<label></label>').attr('for', 'description').text('Description')
    var $description_text = $('<textarea></textarea>').addClass('form-control').attr({
      name: 'description',
      id: 'description',
      placeholder: 'Description...'
    })
    var $description = $('<div></div>').addClass('form-group').append([ $description_label, $description_text ])

    var $bar = $('<progress></progress').attr('value', '0').css('display', 'none')
    var $progress_bar = $('<div><div>').attr('id', 'progress').append($bar)

    var $input_submit = $('<input></input>').addClass('btn btn-default').attr({
      type: 'button',
      value: 'Upload'
    })

    // JQuery plugin
    var $form_video = $('<form></form>').append([ $video_block, $button, $progress_bar, $description, $input_submit ])
    $form_video.fileupload({
      singleFileUploads: true,
      multipart: true,
      url: '/api/v1/videos',
      autoupload: false,
      add: function (e, data) {
        var $text = $('<span></span>').addClass('name_file').text(data['files'][0]['name'])
        $text.insertAfter($button)
        $input_submit.off('click').on('click', function () {
          $bar.css('display', 'block')
          data.formData = $form_video.serializeArray()
          data.submit()
        })
      },
      progressall: function (e, data) {
        $bar.attr({
          value: data.loaded,
          max: data.total
        })
      },
      done: function (e, data) {
        // Print all the videos once it's finished
        getVideos()
      }
    })

    $content.empty()
    $content.append([ $title, $form_video ])
  }

  // Print the list of all the videos
  function getVideos () {
    $.ajax({
      url: '/api/v1/videos/',
      dataType: 'json',
      type: 'GET',
      success: function (videos) {
        printVideos(videos)
      }
    })
  }

  function removeVideo (video) {
    $.ajax({
      url: '/api/v1/videos/' + video._id,
      type: 'DELETE',
      success: function (response, status) {
        getVideos()
      }
    })
  }

  // Get the video: add the torrent file and stream it into a video tag
  function getVideo (video) {
    var $waiting = $('<img></img>').addClass('center-block loading').attr('src', '/images/loading.gif')
    $content.empty()
    $content.append($waiting)

    console.log('Getting ' + video)
    client.add(video.magnetUri, function (torrent) {
      var $embed = $('<div></div>').addClass('embed-responsive embed-responsive-16by9')

      $content.empty()
      $content.append($embed)

      // Got torrent metadata!
      console.log('Torrent info hash:', torrent.infoHash)

      // Let's say the first file is a webm (vp8) or mp4 (h264) video...
      var file = torrent.files[0]

      file.appendTo($embed.get(0), function (err) {
        if (err) {
          alert('Cannot append the file.')
          console.error(err)
        }
      })
    })
  }

  getVideos()
})()