contented/homepage.go
2017-10-06 20:02:57 +13:00

144 lines
3.3 KiB
Go

package contented
import (
"html"
"net/http"
"strconv"
)
func (this *Server) handleHomepage(w http.ResponseWriter) {
extraText := ""
if this.opts.MaxUploadBytes > 0 {
extraText = " (max " + strconv.Itoa(int(this.opts.MaxUploadBytes/(1024*1024))) + " MiB)"
}
w.Header().Set(`Content-Type`, `text/html;charset=UTF-8`)
w.WriteHeader(200)
w.Write([]byte(`<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>` + html.EscapeString(this.opts.AppTitle) + `</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style type="text/css">
html, body {
font-family: sans-serif;
}
#img {
width:64px;
height:64px;
}
a {
color:blue;
text-decoration:underline;
cursor:pointer;
}
form {
text-align: center;
border: 8px dashed lightgrey;
padding: 12px;
margin: 12px;
}
form .if-supports-drag {
display:none;
}
form .if-nodrag {
display:block;
}
form.supports-drag .if-supports-drag {
display:block;
}
form.supports-drag .if-nodrag {
display:none;
}
form.is-dragging {
background: lightblue;
}
</style>
</head>
<body>
<form id="form" method="POST" action="/upload" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="` + strconv.Itoa(int(this.opts.MaxUploadBytes)) + `" />
<svg id="img" viewBox="0 0 24 24">
<path fill="#000000" d="M5,20H19V18H5M19,9H15V3H9V9H5L12,16L19,9Z" />
</svg>
<div class="if-supports-drag">
<label id="form-label">Drop file(s) to upload ` + extraText + `</label>
<br>
<br>
<a id="classic-uploader">Classic uploader</a>
</div>
<div class="if-nodrag">
<input id="form-upload" name="f" type="file" multiple>
<input type="submit" value="Upload &raquo;">
</div>
</form>
<script type="text/javascript">
"use strict";
var onLoad = function() {
var form = document.getElementById("form");
var input = document.getElementById("form-upload");
var label = document.getElementById("form-label");
var makeCaptionText = function( files ) {
return files.length > 1 ? ( input.getAttribute( 'data-multiple-caption' ) || '' ).replace( '{count}', files.length ) : files[ 0 ].name;
};
var refreshCaption = function() {
label.textContent = makeCaptionText( input.files );
};
input.addEventListener('change', refreshCaption);
//
document.getElementById("classic-uploader").addEventListener("click", function() {
form.classList.remove("supports-drag");
});
var supportsDrag = ('ondrop' in window && 'FormData' in window && 'FileReader' in window);
if (supportsDrag) {
// Handle CSS
form.classList.add('supports-drag');
var handleDragState = function(event_name, classEnabled) {
form.addEventListener(event_name, function(e) {
e.preventDefault();
e.stopPropagation();
if (classEnabled) {
form.classList.add( 'is-dragover' );
} else {
form.classList.remove( 'is-dragover' );
}
});
}
handleDragState('dragover', true);
handleDragState('dragenter', true);
handleDragState('dragleave', false);
handleDragState('dragend', false);
handleDragState('drop', false);
// Handle uploads
form.addEventListener('drop', function(e) {
input.files = e.dataTransfer.files; // the files that were dropped
refreshCaption();
form.submit();
});
}
};
// Entry point
onLoad();
</script>
</body>
</html>
`))
}