Skip to main content

max / makenotwork

3.4 KB · 74 lines History Blame Raw
1 (function() {
2 'use strict';
3 var itemId = document.getElementById('wizard-item-content-video-cfg').dataset.itemId;
4 var dropArea = document.getElementById('video-upload-area');
5 var fileInput = document.getElementById('video-file-input');
6 var statusEl = document.getElementById('video-upload-status');
7
8 initDropzone(dropArea, fileInput, function(file) {
9 if (file.type.startsWith('video/') || file.name.match(/\.(mp4|webm|mov)$/i)) {
10 uploadVideo(file);
11 }
12 });
13
14 function uploadVideo(file) {
15 var contentType = file.type || 'video/mp4';
16 statusEl.innerHTML =
17 '<div class="upload-status">' +
18 '<div class="upload-status-row">' +
19 '<span id="video-upload-filename"></span>' +
20 '<span id="video-upload-percent">0%</span></div>' +
21 '<div class="progress-bar-container progress-bar-container--slim progress-bar-container--rounded">' +
22 '<div id="video-progress-bar" class="progress-bar progress-bar--highlight"></div></div></div>';
23
24 var uploader = new S3Uploader({
25 filenameEl: document.getElementById('video-upload-filename'),
26 percentEl: document.getElementById('video-upload-percent'),
27 progressBar: document.getElementById('video-progress-bar'),
28 });
29
30 fetch('/api/upload/presign', {
31 method: 'POST',
32 headers: Object.assign({'Content-Type': 'application/json'}, csrfHeaders()),
33 body: JSON.stringify({ item_id: itemId, file_type: 'video', file_name: file.name, content_type: contentType, file_size_bytes: file.size })
34 })
35 .then(function(res) {
36 if (!res.ok) return res.json().catch(function() { return {}; }).then(function(d) {
37 throw new Error(d.error || 'Failed to get upload URL');
38 });
39 return res.json();
40 })
41 .then(function(data) {
42 return uploader.upload(data.upload_url, file, data.s3_key, contentType, data.cache_control);
43 })
44 .then(function(s3Key) {
45 return fetch('/api/upload/confirm', {
46 method: 'POST',
47 headers: Object.assign({'Content-Type': 'application/json'}, csrfHeaders()),
48 body: JSON.stringify({ item_id: itemId, file_type: 'video', s3_key: s3Key })
49 });
50 })
51 .then(function(res) {
52 if (!res.ok) return res.json().catch(function() { return {}; }).then(function(d) {
53 throw new Error(d.error || 'Failed to confirm upload');
54 });
55 return res.json().catch(function() { return {}; });
56 })
57 .then(function(result) {
58 if (result && result.pending_review) {
59 statusEl.innerHTML = '<div class="upload-status-msg is-warning">Upload accepted but held for review — our scanner flagged it.</div>';
60 } else {
61 statusEl.innerHTML = '<div class="upload-status-msg is-success">Upload complete</div>';
62 }
63 })
64 .catch(function(err) {
65 // Server error text is set via textContent, not innerHTML, so a
66 // message echoed from an API response can't inject markup.
67 var msg = document.createElement('div');
68 msg.className = 'upload-status-msg is-error';
69 msg.textContent = err.message || 'Upload failed';
70 statusEl.replaceChildren(msg);
71 });
72 }
73 })();
74