<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<meta http-equiv="X-UA-Compatible" content="ie=edge">
	<title>Spee.ch</title>
</head>
<body>
	<h1>spee.ch</h1>
	<p>spee.ch is a single-serving site that reads and publishes images to and from the <a href="https://lbry.io">LBRY</a> blockchain.</p>
	<h3>Examples:</h3>
	<ul>
		<li><a href="/coconuts">spee.ch/coconuts</a></li>
		<li><a href="/test">spee.ch/test</a></li>
		<li><a href="/doitlive">spee.ch/doitlive</a></li>
		<li><a href="/doitlive/all">spee.ch/doitlive/all</a></li>
		<li><a href="/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0">spee.ch/doitlive/ca3023187e901df9e9aabd95d6ae09b6cc69b3f0</a></li>
	</ul>
	<h3>Publish Your Own</h3>
	<div id="publish">
		<form id="publish-form" action="" method="" enctype="multipart/form-data">
			<input type="file" id="file-upload-input" name="file" accept="video/*,image/*" onchange="previewFile()" enctype="multipart/form-data"/>
			<br/>
			<img id="image-preview" src="" height="200" alt="Image preview..."/>
			<br/>
			Name: <input type="text" id="publish-name" name="name" value="name"/>
			<br/>
			Title: <input id="publish-title" type="text" name="title" value="title"/>
			<br/>
			Description: <input id="publish-description" type="text" name="description" value="I love spee.ch!"/>
			<br/>
			Author: <input type="text" id="publish-author" name="author" value="author"/>
			<br/>
			Language: <input type="text" id="publish-language" name="language" value="en"/>
			<br/>
			License: <select type="text" id="publish-license" name="license" value="license">
				<option value="Creative Commons">Creative Commons</option>
				<option value="Public Domain">Public Domain</option>
			</select>
			<br/>
			NSFW: <select type="text" id="publish-nsfw" name="nsfw" value="false">
				<option value="false">False</option>
				<option value="true">True</option>
			</select>
			<br/>
			<button type="submit" id="publish-submit">Submit</button>
		</form>
	</div>
	
	<h3>Help</h3>
	<h4>Site Navigation</h4>
	<ul>
		<li><strong><a href="/">spee.ch</a></strong>.
			<ul>
				<li>To publish a file, navigate to the homepage.</li>
			</ul>
		</li>
		<li><strong>spee.ch/&ltthe name of the claim&gt</strong>
			<ul>
				<li>To view the file with the largest bid at a claim.</li>
				<li>E.g. <a href="/doitlive">spee.ch/doitlive</a>.</li>
			</ul>
		</li>
		<li><strong>spee.ch/&lt the name of the claim &gt/&lt the claim_id &gt</strong>
			<ul>
				<li>To view a specific file at a claim</li>
				<li>E.g. <a href="/doitlive/c496c8c55ed79816fec39e36a78645aa4458edb5">spee.ch/doitlive/c496c8c55ed79816fec39e36a78645aa4458edb5</a></li>
			</ul>
		</li>
		<li><strong>spee.ch/&ltthe name of the claim&gt/all</strong>
			<ul>
				<li>To view a batch of files at a claim</li>
				<li>E.g. <a href="/doitlive/all">spee.ch/doitlive/all</a></li>
			</ul>
		</li>
	</ul>
	<h4>API</h4>
	<p>Note: these are being used for testing durring spee.ch development and may not be maintained</p>
	<ul>		
		<li>A GET request to <strong>spee.ch/claim_list/&ltthe name of the claim&gt</strong> 
			<ul>
				<li>Will return the claim_list for the claim in json format. </li> 
				<li>E.g. <a href="/claim_list/doitlive">spee.ch/claim_list/doitlive</a></li>
			</ul>
		</li>
	</ul>

	<script src="/socket.io/socket.io.js"></script>
	<script>
		// define global variables
		var socket = io();
		var selectedFile;
		var uploadReader = new FileReader();
		// function to handle image preview
		function previewFile(){
			var preview = document.querySelector('img'); //selects the query named img
			selectedFile = document.querySelector('input[name=file]').files[0];
			var previewReader = new FileReader();
			
			previewReader.onloadend = function () {
				preview.src = previewReader.result;
			}

			if (selectedFile) {
				previewReader.readAsDataURL(selectedFile); // reads the data as a URL
			} else {
				preview.src = "";
			};
		}
		// function for updating progress bar
		function updateProgressBar(percent){
			document.getElementById('progress-bar').style.width = percent + '%';
			document.getElementById('percent').innerHTML = (Math.round(percent*100)/100) + '%';
			document.getElementById('MB').innerHTML = Math.round(((percent / 100.0 ) * selectedFile.size) / 1048576);
		}
		// call the previewFile function
		previewFile();
		// publish through the socket
		document.getElementById('publish-submit').addEventListener('click', function(event) {
			console.log("upload event:", event);
			event.preventDefault();
			
			var fileName = document.getElementById('publish-name').value;
			var description = document.getElementById('publish-description').value;
			var title = document.getElementById('publish-title').value;
			var author = document.getElementById('publish-author').value;
			var language = document.getElementById('publish-language').value;
			var license = document.getElementById('publish-license').value;
			var nsfw = (document.getElementById('publish-nsfw').value.toLowerCase() === 'true');
			
			if (document.getElementById('file-upload-input').value != ""){
				// build the upload visualizer 
				var uploadVisualizer = '<div id="upload-visualizer">Uploading ' + fileName + '</div>';
				uploadVisualizer += '<div id="progress-container"><div id="progress-bar"></div></div><span id="percent">0%</span>';
				uploadVisualizer += "<span id='uploaded'> - <span id='MB'>0</span>/" + Math.round(selectedFile.size / 1048576) + "MB</span>";
				// place upload visualizer in the HTML
				document.getElementById("publish").innerHTML = uploadVisualizer;
				// reade the file and emit socket msg 
				uploadReader.onload = function(readerEvent){
					socket.emit('upload', {
						'name': fileName, 
						'description': description,
						'title': title,
						'author': author,
						'language': language,
						'license': license,
						'nsfw': nsfw,
						'data': readerEvent.target.result 
					});
				}
				socket.emit('upload-start', {'name': fileName, 'size' : selectedFile.size })
			} else {
				alert('Please select a file');
			};

		});
		// provide more data
		socket.on('moreData', function(data){
			console.log("moreData");
			console.log("data:", data);
			updateProgressBar(data['percent']);
			var place = data['place'] * 524288; // the next block's starting position
			var newFile; // variable that will hold the new block of data
			if (selectedFile.slice) {
				newFile = selectedFile.slice(place, place + Math.min(524288, (selectedFile.size - place)));
			} else {
				newFile = selectedFile.mozSlice(place, place + Math.min(524288, (selectedFile.size - place)));
			};
			uploadReader.readAsBinaryString(newFile);
		});
		// listen for updates
		socket.on('publish-update', function(data){
			console.log('data:', data);
			document.getElementById('status').innerHTML = data;
		})
		
	</script>
</body>
</html>