mirror of
https://github.com/schollz/hostyoself.git
synced 2026-01-23 02:15:14 +00:00
start
This commit is contained in:
parent
4e2ea3d663
commit
59290968eb
3 changed files with 144 additions and 0 deletions
10
go.mod
Normal file
10
go.mod
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
module github.com/schollz/userve
|
||||
|
||||
go 1.12
|
||||
|
||||
require (
|
||||
github.com/gorilla/websocket v1.4.0
|
||||
github.com/h2non/filetype v1.0.8
|
||||
github.com/schollz/logger v1.0.1
|
||||
github.com/vincent-petithory/dataurl v0.0.0-20160330182126-9a301d65acbb
|
||||
)
|
||||
8
go.sum
Normal file
8
go.sum
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q=
|
||||
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||
github.com/h2non/filetype v1.0.8 h1:le8gpf+FQA0/DlDABbtisA1KiTS0Xi+YSC/E8yY3Y14=
|
||||
github.com/h2non/filetype v1.0.8/go.mod h1:isekKqOuhMj+s/7r3rIeTErIRy4Rub5uBWHfvMusLMU=
|
||||
github.com/schollz/logger v1.0.1 h1:BuBAU+euqphM0Ny9qFVScl4RSxatis4nCHIkOxO2cUU=
|
||||
github.com/schollz/logger v1.0.1/go.mod h1:P6F4/dGMGcx8wh+kG1zrNEd4vnNpEBY/mwEMd/vn6AM=
|
||||
github.com/vincent-petithory/dataurl v0.0.0-20160330182126-9a301d65acbb h1:lyL3z7vYwTWXf4/bI+A01+cCSnfhKIBhy+SQ46Z/ml8=
|
||||
github.com/vincent-petithory/dataurl v0.0.0-20160330182126-9a301d65acbb/go.mod h1:FHafX5vmDzyP+1CQATJn7WFKc9CvnvxyvZy6I1MrG/U=
|
||||
126
templates/view.html
Normal file
126
templates/view.html
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
<!doctype html>
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<title></title>
|
||||
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<main>
|
||||
<input type="file" id="files" name="files[]" webkitdirectory mozdirectory msdirectory odirectory directory
|
||||
multiple />
|
||||
<output id="list"></output>
|
||||
</main>
|
||||
<script>
|
||||
var socket; // websocket
|
||||
|
||||
|
||||
/* websockets */
|
||||
function socketSend(data) {
|
||||
if (socket == null) {
|
||||
return
|
||||
}
|
||||
if (socket.readyState != 1) {
|
||||
return
|
||||
}
|
||||
jsonData = JSON.stringify(data);
|
||||
socket.send(jsonData);
|
||||
if (jsonData.length > 100) {
|
||||
jsonData = jsonData.substring(0, 99) + "...";
|
||||
}
|
||||
console.log("[debug] ws-> " + jsonData)
|
||||
}
|
||||
|
||||
const socketMessageListener = (event) => {
|
||||
var data = JSON.parse(event.data);
|
||||
if (!('type' in data && 'message' in data)) {
|
||||
console.log(`[warn] got bad data ${event.data}`);
|
||||
return
|
||||
}
|
||||
console.log(`[debug] ${data.type} ${data.message}`)
|
||||
if (data.type == "get") {
|
||||
var foundFile = false
|
||||
for (i = 0; i < files.length; i++) {
|
||||
if (files[i].webkitRelativePath == data.message) {
|
||||
var reader = new FileReader();
|
||||
reader.onload = function (theFile) {
|
||||
socketSend({
|
||||
type: "get",
|
||||
message: reader.result,
|
||||
success: true,
|
||||
})
|
||||
console.log(`[info] ${data.message} 200`);
|
||||
};
|
||||
reader.readAsDataURL(files[i]);
|
||||
foundFile = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (foundFile == false) {
|
||||
socketSend({
|
||||
type: "get",
|
||||
message: "not found",
|
||||
success: false,
|
||||
})
|
||||
console.log(`[info] ${data.message} 404`);
|
||||
}
|
||||
} else if (data.type == "message") {
|
||||
console.log(`[info] ${data.message}`);
|
||||
} else {
|
||||
console.log(`[debug] unknown`);
|
||||
}
|
||||
};
|
||||
const socketOpenListener = (event) => {
|
||||
console.log('[debug] connected');
|
||||
};
|
||||
|
||||
const socketCloseListener = (event) => {
|
||||
if (socket) {
|
||||
console.log('[debug] disconnected');
|
||||
}
|
||||
var url = window.origin.replace("http", "ws") + '/ws';
|
||||
try {
|
||||
socket = new WebSocket(url);
|
||||
socket.addEventListener('open', socketOpenListener);
|
||||
socket.addEventListener('message', socketMessageListener);
|
||||
socket.addEventListener('close', socketCloseListener);
|
||||
} catch (err) {
|
||||
console.log("[debug] no connection available")
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
var files;
|
||||
|
||||
function handleFileSelect(evt) {
|
||||
files = evt.target.files; // FileList object
|
||||
|
||||
// files is a FileList of File objects. List some properties.
|
||||
var output = [];
|
||||
for (var i = 0, f; f = files[i]; i++) {
|
||||
output.push('<li><strong>', escape(f.webkitRelativePath), '</strong> (', f.type || 'n/a', ') - ',
|
||||
f.size, ' bytes, last modified: ',
|
||||
f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
|
||||
'</li>');
|
||||
}
|
||||
document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
|
||||
|
||||
var reader = new FileReader();
|
||||
reader.onload = function (theFile) {
|
||||
console.log(reader);
|
||||
domain = files[0].webkitRelativePath.split("/")[0];
|
||||
socketSend({
|
||||
"type": "domain",
|
||||
"message": domain,
|
||||
})
|
||||
};
|
||||
reader.readAsDataURL(files[0]);
|
||||
}
|
||||
|
||||
document.getElementById('files').addEventListener('change', handleFileSelect, false);
|
||||
socketCloseListener();
|
||||
</script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
Loading…
Add table
Add a link
Reference in a new issue