upload.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. $(function () {
  2. if (window.my_user_id < 0) // dirty hack!
  3. document.location.href = '/';
  4. $('#file_error').hide();
  5. $('#tags_error').hide();
  6. $('#captcha_error').hide();
  7. $('#upload_suggestions').hide();
  8. $('#tags_field').on('keyup', function (event) {
  9. var tags = $('#tags_field').val();
  10. if (tags[tags.length - 1] == ' ') {
  11. $('#upload_suggestions').hide();
  12. return;
  13. }
  14. tags = tags.trim();
  15. if (tags.length == 0) {
  16. $('#upload_suggestions').hide();
  17. return;
  18. }
  19. suggest('#upload_suggestions', '#upload_suggestions_content', '#tags_field', tags);
  20. });
  21. $('#upload_button').click(function () {
  22. $('#upload_button').addClass('is-loading');
  23. $('#upload_button').prop('disabled', true);
  24. var form = new FormData();
  25. form.append('video', $('#video').prop('files')[0]);
  26. form.append('tags', $('#tags_field').val());
  27. form.append('h-captcha-response', grecaptcha.getResponse());
  28. api_unchecked('upload/video', form, function (data) {
  29. $('#upload_button').removeClass('is-loading');
  30. $('#upload_button').prop('disabled', false);
  31. if (data.error !== undefined) {
  32. switch (data.error) {
  33. case 'not a valid video':
  34. case 'illegal video':
  35. show_hide('#file_error');
  36. break;
  37. case 'illegal tags':
  38. show_hide('#tags_error');
  39. break;
  40. case 'captcha check failed':
  41. show_hide('#captcha_error');
  42. break;
  43. default:
  44. popup('danger', 'Error: ' + data.error);
  45. break;
  46. }
  47. grecaptcha.reset();
  48. return;
  49. }
  50. popup('primary', 'Successfully uploaded your video!');
  51. setTimeout(function () {
  52. document.location.href = '/watch/' + String(data.video_id);
  53. }, 1500);
  54. });
  55. });
  56. });