Skip to content

Commit

Permalink
version 1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
hffqyd committed Jul 20, 2023
1 parent 1e16b35 commit 060b9c8
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 15 deletions.
86 changes: 86 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
function create_li(f) {
var li = document.createElement('li');
li.setAttribute('class', 'i-file entypo');
li.innerHTML = '<a href="' + f + '">' + f + '</a>';
return li;
}

function add_to_ul(f) {
var ul = document.querySelector('ul');
var li = create_li(f);
if (ul.children[0].innerText == '..') {
ul.insertBefore(li, ul.children[1]); // first is the back link if in sub-dir
} else {
ul.insertBefore(li, ul.children[0]);
}
}

function notify(note) {
var nojump = document.querySelector('#nojump');
nojump.innerHTML = note;
}

function post_file(file) {
var formdata = new FormData();
formdata.append("file", file, file.name);
var override = document.querySelector('#override_check');
if (override.checked) {
formdata.append('override', 'yes');
}
// request
var xhr = new XMLHttpRequest();
xhr.open("POST", '', true);
xhr.onreadystatechange = function () {
if (xhr.readyState != 4 || xhr.status != 200) {
notify('Error: ' + xhr.responseText);
} else {
notify('Saved to ' + xhr.responseText);
add_to_ul(xhr.responseText);
}
}
//var boundary = '-------------------' + Date.now().toString(16);
//xhr.setRequestHeader('Content-Type', 'multipart\/form-data; boundary=' + boundary);
xhr.send(formdata);
}

var submit = document.querySelector('label[type="submit"]');
submit.addEventListener('click', function(event) {
event.preventDefault();

var input = document.querySelector('input[type="file"]');
if (input.files.length < 1) {return;}
const file = input.files[0];

post_file(file);
})

// drag to upload
var target = document.querySelector('body');
var changed_color = '#dcf8c6'; //'#BEDDAA'
var original_color = target.getAttribute('background-color');
if (original_color == null) {
original_color = '';
}

target.addEventListener('drop', function(event) {
event.preventDefault();
if (event.type === 'drop') {
var file = event.dataTransfer.files[0];
post_file(file)
}
target.style.backgroundColor = original_color;
})

target.addEventListener('dragenter', function (event) {
event.preventDefault();
target.style.backgroundColor = changed_color;
})
target.addEventListener('dragover', function (event) {
event.preventDefault();
target.style.backgroundColor = changed_color;
})
target.addEventListener('dragleave', function (event) {
event.preventDefault();
target.style.backgroundColor = original_color;
})

48 changes: 48 additions & 0 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,54 @@ body {
margin: 0;
}

.button-primary {
color: white;
border-radius: 4px;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.2);
background-color: rgb(0, 120, 231);
border: none;
padding: 0.2em 0.2em;
font-family: inherit;
line-height: normal;
user-select: none;
display: inline-block;
}

.button-primary-hover,
.button-primary:hover,
.button-primary:focus {
background-image: linear-gradient(transparent, rgba(0,0,0, 0.05) 40%, rgba(0,0,0, 0.10));
}
.button-primary:focus {
outline: 0;
}
.button-primary-active,
.button-primary:active {
box-shadow: 0 0 0 1px rgba(0,0,0, 0.15) inset, 0 0 6px rgba(0,0,0, 0.20) inset;
border-color: #000;
}

.button-success {
background: rgb(28, 184, 65);
font: 20px;
/* line-height: 1.4; */
margin-left: 25%;
}

#upload_button {
display: none;
}


#upload_button2 {
/* background-color: #f2d547;
border-radius: 3px;
display: inline-block;
padding: 1px; */
margin-left: 2em;
margin-right: 25%;
}

#nojump {
display:block;
position:fixed;
Expand Down
18 changes: 12 additions & 6 deletions template.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@
</head>
<body>
<h1>$1</h1>
<h2>$3</h2>
<iframe id="nojump" name="nojump" frameborder="0" seamless></iframe>
<div id="nojump" name="nojump" frameborder="0"></div>
<form method="post" enctype="multipart/form-data" target="nojump">
<input type="file" name="file" value="file">
Override: <input type="checkbox" name="write" value="yes">
<input type="submit" name="submit" value="Submit">
<input id="upload_button" type="file" name="file" value="file">
<label class="button-primary" for="upload_button" id="upload_button2">Browse</label>
<label for="override_check">
<input type="checkbox" name="override" value="yes" id="override_check">Override
</label>
<label class="button-primary button-success" type="submit">Upload</label>
</form>
<h2>$3</h2>
$4
$5
<script>
$6
</script>
</body>
</html>
</html>
19 changes: 10 additions & 9 deletions tw5server.nim
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,16 @@ import
mimetypes

import tables, strtabs
from jester/private/utils import parseMPFD

type MultiData* = OrderedTable[string, tuple[fields: StringTableRef, body: string]]

from httpcore import HttpMethod, HttpHeaders

from parseBody import parseMPFD

const
name = "TW5 server"
version = "1.0.0"
version = "1.2.0"
style = staticRead("style.css")
temp = staticRead("template.html")
js = staticRead("main.js")

const usage = """
tw5server -a:localhost -p:8000 -d:dir -b:backup
Expand Down Expand Up @@ -61,7 +60,7 @@ type
# TODO: update web page after upload
proc h_page(settings:NimHttpSettings, content, title, subtitle: string): string =
var footer = """<div id="footer">$1 v$2</div>""" % [settings.name, settings.version]
result = temp % [title, style, subtitle, content, footer]
result = temp % [title, style, subtitle, content, footer, js]

proc relativePath(path, cwd: string): string =
var path2 = path
Expand Down Expand Up @@ -158,7 +157,7 @@ proc savePost(req: Request, path, url_path: string, log: bool): NimHttpResponse
file = body["file"]
filename = file.fields["filename"]
file_body = file.body
override = body.getOrDefault("write").body
override = body.getOrDefault("override").body

var
rsp_content = ""
Expand All @@ -173,15 +172,15 @@ proc savePost(req: Request, path, url_path: string, log: bool): NimHttpResponse
let
rsp_msg = "Save file to " & newName
msg = rsp_msg & " in " & path
rsp_content = rsp_msg
rsp_content = newName
code = Http200
logmsg(msg, log)
else:
writeFile(path / filename, file_body)
let
rsp_msg = "Save file to " & filename
msg = rsp_msg & " in " & path
rsp_content = rsp_msg
rsp_content = filename
code = Http200
logmsg(msg, log)
return (code: code, content: rsp_content, headers: {"status": "ok"}.newHttpHeaders())
Expand Down Expand Up @@ -311,6 +310,8 @@ echo(" Serving url: ", address, ":", port)
echo("Serving path: ", dir)
echo(" Backup dir: ", backup)

createDir(dir / backup)

proc handleCtrlC() {.noconv.} =
write(stdout, "\rClean backups (y to clean): ")
let clean = readLine(stdin)
Expand Down

0 comments on commit 060b9c8

Please sign in to comment.