Reworked upload box (#2)

This commit is contained in:
imperatormk 2020-03-25 00:29:08 +01:00 committed by GitHub
parent 5803bcb6fc
commit 363f62ae5f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 409 additions and 161 deletions

BIN
static/close-icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 343 B

BIN
static/upload-icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

158
static/upload.css Normal file
View file

@ -0,0 +1,158 @@
.upload-container {
display: inline-block;
max-width: 100%;
margin-top: 50px;
}
.upload-wrapper {
padding: 20px;
background-color: #282a2e;
border: 1px solid #1e1e1e;
border-radius: 20px;
font-family: 'Trebuchet MS', Arial, sans-serif;
}
.upload-form {
border: 5px dashed #676867;
border-radius: 10px;
cursor: pointer;
}
.upload-box {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
justify-content: center;
align-items: center;
min-width: 300px;
min-height: 300px;
}
.upload-box span {
color: #c5c8c6;
font-weight: bold;
}
.upload-details {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 0 0 20px 0;
}
.upload-details > * {
margin-top: 20px;
}
.upload-details span {
color: #c5c8c6;
font-weight: bold;
}
.upload-file {
display: none;
width: 100%;
padding: 0 20px;
justify-content: space-between;
align-items: center;
}
.upload-message {
white-space: normal;
padding: 0 20px;
word-wrap: break-word;
max-width: 300px;
}
.upload-message.error {
color: #e82e57;
}
.upload-button-wrapper {
display: none;
width: 100%;
padding: 0 20px;
}
.upload-button {
border: 0;
box-shadow: none;
border-radius: 0px;
width: 100%;
padding: 15px;
background: #191919 !important;
font-family: 'Trebuchet MS', Arial, sans-serif;
color: #c5c8c6;
border-radius: 10px;
cursor: pointer;
}
.upload-button.transparent {
background: transparent !important;
padding: 10px;
}
.upload-progress-container {
display: none;
width: 100%;
padding: 0 20px;
}
.upload-progress {
height: 30px;
background: linear-gradient(45deg, #c7c7c7, #ae81ff);
border-radius: 5px;
display: flex;
justify-content: center;
align-items: center;
}
.upload-progress-label {
color: #282a2e !important;
}
/* SPINNER */
.loader,
.loader:after {
border-radius: 50%;
width: 3em;
height: 3em;
}
.loader {
font-size: 10px;
position: relative;
text-indent: -9999em;
border-top: 0.5em solid rgba(255, 255, 255, 0.2);
border-right: 0.5em solid rgba(255, 255, 255, 0.2);
border-bottom: 0.5em solid rgba(255, 255, 255, 0.2);
border-left: 0.5em solid #ffffff;
-webkit-transform: translateZ(0);
-ms-transform: translateZ(0);
transform: translateZ(0);
-webkit-animation: load8 1.1s infinite linear;
animation: load8 1.1s infinite linear;
}
@-webkit-keyframes load8 {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}
@keyframes load8 {
0% {
-webkit-transform: rotate(0deg);
transform: rotate(0deg);
}
100% {
-webkit-transform: rotate(360deg);
transform: rotate(360deg);
}
}

View file

@ -1,145 +1,237 @@
// common variables // common variables
var iBytesUploaded = 0; let iBytesUploaded = 0
var iBytesTotal = 0; let iBytesTotal = 0
var iPreviousBytesLoaded = 0; let iPreviousBytesLoaded = 0
var iMaxFilesize = 104857600; // 100MB let iMaxFilesize = 104857600 // 100MB
var oTimer = 0; let timer = 0
var sResultFileSize = ''; let uploadInProgress = 'n/a'
function secondsToTime(secs) { // we will use this function to convert seconds in normal time format let isProcessing = false
var hr = Math.floor(secs / 3600); let file = null
var min = Math.floor((secs - (hr * 3600))/60);
var sec = Math.floor(secs - (hr * 3600) - (min * 60)); /* CACHED ELEMENTS */
if (hr < 10) {hr = "0" + hr; }
if (min < 10) {min = "0" + min;} const uploadForm = document.getElementById('upload-form')
if (sec < 10) {sec = "0" + sec;} const videoInput = document.getElementById('video-input')
if (hr) {hr = "00";} const uploadMessageLabel = document.getElementById('upload-message')
return hr + ':' + min + ':' + sec; const uploadFileContainer = document.getElementById('upload-file')
}; const uploadFilenameLabel = document.getElementById('upload-filename')
function bytesToSize(bytes) { const uploadButtonWrapper = document.getElementById('upload-button-wrapper')
var sizes = ['Bytes', 'KB', 'MB']; const uploadButton = document.getElementById('upload-button')
if (bytes == 0) return 'n/a'; const uploadProgressContainer = document.getElementById('upload-progress-container')
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); const uploadProgressBar = document.getElementById('upload-progress')
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i]; const uploadProgressLabel = document.getElementById('upload-progress-label')
}; const uploadStopped = document.getElementById('upload-stopped')
function fileSelected() { const uploadStarted = document.getElementById('upload-started')
// hide different warnings
document.getElementById('upload_response').style.display = 'none'; /* HELPERS */
document.getElementById('error').style.display = 'none';
document.getElementById('error2').style.display = 'none'; const setProgress = (_progress) => {
document.getElementById('abort').style.display = 'none'; uploadProgressContainer.style.display = _progress > 0 ? 'flex' : 'none'
document.getElementById('warnsize').style.display = 'none'; uploadProgressBar.style.width = `${_progress}%`
// get selected file element uploadProgressLabel.innerText = _progress >= 15 ? `${_progress}%` : ''
var oFile = document.getElementById('image_file').files[0];
// filter for image files
var rFilter = /^(image\/bmp|image\/gif|image\/jpeg|image\/png|image\/tiff)$/i;
if (! rFilter.test(oFile.type)) {
document.getElementById('error').style.display = 'block';
return;
}
// little test for filesize
if (oFile.size > iMaxFilesize) {
document.getElementById('warnsize').style.display = 'block';
return;
}
// get preview element
var oImage = document.getElementById('preview');
// prepare HTML5 FileReader
var oReader = new FileReader();
oReader.onload = function(e){
// e.target.result contains the DataURL which we will use as a source of the image
oImage.src = e.target.result;
oImage.onload = function () { // binding onload event
// we are going to display some custom image information here
sResultFileSize = bytesToSize(oFile.size);
document.getElementById('fileinfo').style.display = 'block';
document.getElementById('filename').innerHTML = 'Name: ' + oFile.name;
document.getElementById('filesize').innerHTML = 'Size: ' + sResultFileSize;
document.getElementById('filetype').innerHTML = 'Type: ' + oFile.type;
document.getElementById('filedim').innerHTML = 'Dimension: ' + oImage.naturalWidth + ' x ' + oImage.naturalHeight;
};
};
// read selected file as DataURL
oReader.readAsDataURL(oFile);
} }
function startUploading() {
// cleanup all temp states const setMessage = (_message, isError) => {
iPreviousBytesLoaded = 0; uploadMessageLabel.style.display = _message ? 'block' : 'none'
document.getElementById('upload_response').style.display = 'none'; uploadMessageLabel.innerHTML = _message
document.getElementById('error').style.display = 'none'; if (isError) {
document.getElementById('error2').style.display = 'none'; uploadMessageLabel.classList.add('error')
document.getElementById('abort').style.display = 'none'; } else {
document.getElementById('warnsize').style.display = 'none'; uploadMessageLabel.classList.remove('error')
document.getElementById('progress_percent').innerHTML = ''; }
var oProgress = document.getElementById('progress');
oProgress.style.display = 'block';
oProgress.style.width = '0px';
// get form data for POSTing
//var vFD = document.getElementById('upload_form').getFormData(); // for FF3
var vFD = new FormData(document.getElementById('upload_form'));
// create XMLHttpRequest object, adding few event listeners, and POSTing our data
var oXHR = new XMLHttpRequest();
oXHR.upload.addEventListener('progress', uploadProgress, false);
oXHR.addEventListener('load', uploadFinish, false);
oXHR.addEventListener('error', uploadError, false);
oXHR.addEventListener('abort', uploadAbort, false);
oXHR.open('POST', '/upload');
oXHR.send(vFD);
// set inner timer
oTimer = setInterval(doInnerUpdates, 300);
} }
function doInnerUpdates() { // we will use this function to display upload speed
var iCB = iBytesUploaded; const setUploadState = (_uploadInProgress) => {
var iDiff = iCB - iPreviousBytesLoaded; uploadInProgress = _uploadInProgress
uploadStarted.style.display = _uploadInProgress ? 'inline-block' : 'none'
uploadStopped.style.display = _uploadInProgress ? 'none' : 'block'
if (_uploadInProgress) {
uploadButton.classList.add('transparent')
timer = setInterval(doInnerUpdates, 300)
} else {
uploadButton.classList.remove('transparent')
clearInterval(timer)
}
}
const secondsToTime = (secs) => {
let hr = Math.floor(secs / 3600)
let min = Math.floor((secs - (hr * 3600)) / 60)
let sec = Math.floor(secs - (hr * 3600) - (min * 60))
if (hr < 10) hr = `0${hr}`
if (min < 10) min = `0${min}`
if (sec < 10) sec = `0${sec}`
if (hr) hr = '00'
return `${hr}:${min}:${sec}`
}
const bytesToSize = (bytes) => {
const sizes = ['Bytes', 'KB', 'MB']
if (bytes == 0) return 'n/a'
const i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)))
return (bytes / Math.pow(1024, i)).toFixed(1) + ' ' + sizes[i]
}
const determineDragAndDropCapable = () => {
const div = document.createElement('div')
return (('draggable' in div)
|| ('ondragstart' in div && 'ondrop' in div))
&& 'FormData' in window
&& 'FileReader' in window
}
/* MAIN */
document.addEventListener('DOMContentLoaded', () => {
['drag', 'dragstart', 'dragend', 'dragover', 'dragenter', 'dragleave']
.forEach((evt) => {
uploadForm.addEventListener(evt, (e) => {
e.preventDefault()
e.stopPropagation()
})
})
uploadForm.addEventListener('drop', (e) => {
console.log(111)
e.preventDefault()
e.stopPropagation()
const _file = e.dataTransfer.files[0]
if (!_file) return false
fileSelected(_file)
return false
})
}, false)
const labelClicked = (e) => {
if (uploadInProgress === true) {
e.preventDefault()
return false
}
}
const fileSelected = (_file) => {
file = _file || videoInput.files[0]
if (_file) videoInput.value = ''
if (!file) return
if (file.size > iMaxFilesize) {
setMessage('Your file is very big. We can\'t accept it. Please select more small file.', true)
return
}
setMessage('')
setProgress(0)
const filename = file.name.length <= 20 ? file.name : `${file.name.substring(0, 14)}...${file.name.substring(file.name.length - 3)}`
uploadFilenameLabel.innerText = filename
uploadFileContainer.style.display = 'flex'
uploadButtonWrapper.style.display = 'block'
setUploadState(false)
}
const removeFile = (e, keepMessage) => {
if (e) e.preventDefault()
if (uploadInProgress === true) return
uploadFileContainer.style.display = 'none'
uploadButtonWrapper.style.display = 'none'
videoInput.value = ''
file = null
if (!keepMessage) setMessage('No file selected')
return false
}
const startUploading = () => {
if (uploadInProgress === true) return
if (!file) return
isProcessing = false
iPreviousBytesLoaded = 0
setMessage('')
setProgress(0)
setUploadState(true)
const formData = new FormData()
formData.append('video_file', file)
const xhr = new XMLHttpRequest()
xhr.upload.addEventListener('progress', uploadProgress, false)
xhr.addEventListener('load', uploadFinish, false)
xhr.addEventListener('error', uploadError, false)
xhr.addEventListener('abort', uploadAbort, false)
xhr.open('POST', '/upload')
xhr.send(formData)
timer = setInterval(doInnerUpdates, 300)
}
const doInnerUpdates = () => { // we will use this function to display upload speed
if (isProcessing) {
clearInterval(timer)
return
}
let iDiff = iBytesUploaded - iPreviousBytesLoaded
// if nothing new loaded - exit // if nothing new loaded - exit
if (iDiff == 0) if (iDiff == 0)
return; return
iPreviousBytesLoaded = iCB; iPreviousBytesLoaded = iBytesUploaded
iDiff = iDiff * 2; iDiff = iDiff * 2
var iBytesRem = iBytesTotal - iPreviousBytesLoaded; const iBytesRem = iBytesTotal - iPreviousBytesLoaded
var secondsRemaining = iBytesRem / iDiff; const secondsRemaining = iBytesRem / iDiff
// update speed info // update speed info
var iSpeed = iDiff.toString() + 'B/s'; let iSpeed = iDiff.toString() + 'B/s'
if (iDiff > 1024 * 1024) { if (iDiff > 1024 * 1024) {
iSpeed = (Math.round(iDiff * 100/(1024*1024))/100).toString() + 'MB/s'; iSpeed = (Math.round(iDiff * 100/(1024*1024))/100).toString() + 'MB/s'
} else if (iDiff > 1024) { } else if (iDiff > 1024) {
iSpeed = (Math.round(iDiff * 100/1024)/100).toString() + 'KB/s'; iSpeed = (Math.round(iDiff * 100/1024)/100).toString() + 'KB/s'
} }
document.getElementById('speed').innerHTML = iSpeed;
document.getElementById('remaining').innerHTML = '| ' + secondsToTime(secondsRemaining); const speedMessage = `${iSpeed} | ${secondsToTime(secondsRemaining)}`
setMessage(speedMessage)
} }
function uploadProgress(e) { // upload process in progress function uploadProgress(e) { // upload process in progress
if (e.lengthComputable) { if (e.lengthComputable) {
iBytesUploaded = e.loaded; iBytesUploaded = e.loaded
iBytesTotal = e.total; iBytesTotal = e.total
var iPercentComplete = Math.round(e.loaded * 100 / e.total);
var iBytesTransfered = bytesToSize(iBytesUploaded); const iPercentComplete = Math.round(iBytesUploaded / iBytesTotal * 100)
document.getElementById('progress_percent').innerHTML = iPercentComplete.toString() + '%'; setProgress(iPercentComplete)
document.getElementById('progress').style.width = (iPercentComplete * 4).toString() + 'px'; if (iPercentComplete === 100) {
document.getElementById('b_transfered').innerHTML = iBytesTransfered; isProcessing = true
if (iPercentComplete == 100) { setMessage('Processing video... please wait')
var oUploadResponse = document.getElementById('upload_response');
oUploadResponse.innerHTML = '<h1>Please wait...processing</h1>';
oUploadResponse.style.display = 'block';
} }
} else { } else {
document.getElementById('progress').innerHTML = 'unable to compute'; setMessage('Unable to compute progress.')
} }
} }
function uploadFinish(e) { // upload successfully finished
var oUploadResponse = document.getElementById('upload_response'); const uploadFinish = (e) => { // upload successfully finished
oUploadResponse.innerHTML = e.target.responseText; const message = e.target.responseText
oUploadResponse.style.display = 'block'; const isSuccess = e.target.status < 400
document.getElementById('progress_percent').innerHTML = '100%';
document.getElementById('progress').style.width = '400px'; setProgress(isSuccess ? 100 : 0)
document.getElementById('filesize').innerHTML = sResultFileSize; setMessage(message, !isSuccess)
document.getElementById('remaining').innerHTML = '| 00:00:00'; setUploadState(false)
clearInterval(oTimer); if (isSuccess) removeFile(null, true)
} }
function uploadError(e) { // upload error
document.getElementById('error2').style.display = 'block'; const uploadError = () => { // upload error
clearInterval(oTimer); setMessage('An error occurred while uploading the file.', true)
setProgress(0)
setUploadState(false)
} }
function uploadAbort(e) { // upload abort
document.getElementById('abort').style.display = 'block'; const uploadAbort = () => { // upload abort
clearInterval(oTimer); setMessage('The upload has been canceled by the user or the browser dropped the connection.', true)
setProgress(0)
setUploadState(false)
} }

View file

@ -6,6 +6,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/x-icon" href="/static/favicon.ico"> <link rel="shortcut icon" type="image/x-icon" href="/static/favicon.ico">
<link rel="stylesheet" type="text/css" href="/static/theme.css"> <link rel="stylesheet" type="text/css" href="/static/theme.css">
<link rel="stylesheet" type="text/css" href="/static/upload.css">
{{ template "stylesheets" . }} {{ template "stylesheets" . }}
{{ template "css" . }} {{ template "css" . }}
@ -14,7 +15,7 @@
<body> <body>
<nav> <nav>
<a href="/">Tube</a> <a href="/">Tube</a>
<a class="centered" href="/upload">Upload</a> <a class="centered" style="text-indent: 0;" href="/upload">Upload</a>
</nav> </nav>
<main> <main>
{{template "content" .}} {{template "content" .}}

View file

@ -1,39 +1,36 @@
{{define "content"}} {{define "content"}}
<div id="upload"> <div style="text-align: center;">
<form id="upload_form" enctype="multipart/form-data" method="POST" action="/upload"> <label class="upload-container" onclick="labelClicked(event)">
<div> <div class="upload-wrapper">
<label class="filebutton"> <form id="upload-form" class="upload-form" enctype="multipart/form-data" method="POST" action="/upload">
Select a video to upload... <input id="video-input" type="file" accept="video/*" onchange="fileSelected()" style="display: none;"/>
<span><input type="file" name="video_file" id="video_file" accept="video/*" onchange="fileSelected();" /></span> <div class="upload-box">
</label> <img width="100" src="/static/upload-icon.png"/>
<span>Click to browse or drop file here</span>
</div> </div>
<div class="upload-details">
<div> <div id="upload-file" class="upload-file">
<input type="button" value="Upload" onclick="startUploading()" /> <span id="upload-filename"></span>
<img width="20" src="/static/close-icon.png" onclick="removeFile(event)"/>
</div> </div>
<span id="upload-message" class="upload-message">No file selected</span>
<div id="error">You should select valid image files only!</div> <div id="upload-button-wrapper" class="upload-button-wrapper">
<div id="error2">An error occurred while uploading the file</div> <button id="upload-button" class="upload-button" onclick="startUploading()" type="button">
<div id="abort">The upload has been canceled by the user or the browser dropped the connection</div> <span id="upload-stopped">Upload</span>
<div id="warnsize">Your file is very big. We can't accept it. Please select more small file</div> <span id="upload-started" class="loader" style="display: none;"></span>
</button>
<div id="progress_info">
<div id="progress"></div>
<div id="progress_percent">&nbsp;</div>
<div class="clear_both"></div>
<div>
<div id="speed">&nbsp;</div>
<div id="remaining">&nbsp;</div>
<div id="b_transfered">&nbsp;</div>
<div class="clear_both"></div>
</div> </div>
<div id="upload-progress-container" class="upload-progress-container">
<div id="upload_response"> <div id="upload-progress" class="upload-progress">
<div class="lds-ellipsis"><div></div><div></div><div></div><div></div></div> <span id="upload-progress-label" class="upload-progress-label"></span>
</div>
<div style="flex-grow: 1;"></div>
</div> </div>
</div> </div>
</form> </form>
</div> </div>
</label>
</div>
{{end}} {{end}}
{{define "scripts"}} {{define "scripts"}}
<script type="application/javascript" src="/static/upload.js"></script> <script type="application/javascript" src="/static/upload.js"></script>