Skip to content

Commit

Permalink
Restructure code and remove unused HTML snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Mar 9, 2024
1 parent db5f501 commit 2981778
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ pub async fn start() -> io::Result<()> {
.service(routes::media::track)
.service(routes::media::stream)
.service(routes::media::streaming_endpoint)
.service(routes::uploads::upload_files)
.service(routes::uploads::save_files)
.service(routes::upload::upload_files)
.service(routes::upload::save_files)
};
let server = HttpServer::new(application)
.workers(config.workers as usize)
Expand Down
2 changes: 1 addition & 1 deletion src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ pub mod media;
/// Module for `/home`, `/login`, `/logout` and `/error` entrypoints.
pub mod auth;
/// Module to handle upload entrypoint.
pub mod uploads;
pub mod upload;
14 changes: 5 additions & 9 deletions src/routes/uploads.rs → src/routes/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,15 @@ pub async fn save_files(request: HttpRequest,
if !auth_response.ok {
return routes::auth::failed_auth(auth_response, &config);
}
let mut source_path = config.media_source.clone();
for file in form.files {
let filename = file.file_name.unwrap();
let path = format!("uploads/{}", filename);
log::info!("Saving to {path}");
source_path.push(&filename);
let path = source_path.to_string_lossy().to_string();
log::info!("Saving '{filename}'");
file.file.persist(path).unwrap();
}
let html = r#"<html>
<head><title>Upload Test</title></head>
<body>
<h3>Files have been uploaded successfully!!</h3>
</body>
</html>"#;
HttpResponse::Ok().body(html)
HttpResponse::Ok().body("Success")
}

#[get("/upload")]
Expand Down
6 changes: 3 additions & 3 deletions src/templates/landing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub fn get_content() -> String {
.upload {
position: absolute;
top: 3.8%;
right: 315px;
right: 313px;
border: none;
padding: 10px 14px;
font-size: 16px;
Expand All @@ -49,7 +49,7 @@ pub fn get_content() -> String {
.home {
position: absolute;
top: 3.8%;
right: 218px;
right: 217px;
border: none;
padding: 10px 14px;
font-size: 16px;
Expand All @@ -58,7 +58,7 @@ pub fn get_content() -> String {
.back {
position: absolute;
top: 3.8%;
right: 130px;
right: 132px;
border: none;
padding: 10px 14px;
font-size: 16px;
Expand Down
6 changes: 3 additions & 3 deletions src/templates/listing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn get_content() -> String {
.upload {
position: absolute;
top: 3.8%;
right: 315px;
right: 313px;
border: none;
padding: 10px 14px;
font-size: 16px;
Expand All @@ -46,7 +46,7 @@ pub fn get_content() -> String {
.home {
position: absolute;
top: 3.8%;
right: 218px;
right: 217px;
border: none;
padding: 10px 14px;
font-size: 16px;
Expand All @@ -55,7 +55,7 @@ pub fn get_content() -> String {
.back {
position: absolute;
top: 3.8%;
right: 130px;
right: 132px;
border: none;
padding: 10px 14px;
font-size: 16px;
Expand Down
109 changes: 87 additions & 22 deletions src/templates/upload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub fn get_content() -> String {
/* optional google fonts */
@import url('https://fonts.googleapis.com/css2?family=Ubuntu:wght@400;500;700&display=swap');
body {
background-color: #7494EC;
background-color: #6c7dac;
padding: 30px;
margin: 0px;
margin: 0;
}
* {
font-family: 'Ubuntu', sans-serif;
Expand All @@ -42,17 +42,17 @@ pub fn get_content() -> String {
margin: auto;
background-color: white;
border-radius: 16px;
box-shadow: rgba(255, 255, 255, 0.1) 0px 1px 1px 0px inset, rgba(50, 50, 93, 0.25) 0px 50px 100px -20px, rgba(0, 0, 0, 0.3) 0px 30px 60px -30px;
box-shadow: rgba(255, 255, 255, 0.1) 0 1px 1px 0 inset, rgba(50, 50, 93, 0.25) 0 50px 100px -20px, rgba(0, 0, 0, 0.3) 0 30px 60px -30px;
}
.header-section {
padding: 25px 0px;
padding: 25px 0;
}
.header-section h1 {
font-weight: 500;
font-size: 1.7rem;
text-transform: uppercase;
color: #707EA0;
margin: 0px;
margin: 0;
margin-bottom: 8px;
}
.header-section p {
Expand Down Expand Up @@ -81,10 +81,10 @@ pub fn get_content() -> String {
font-weight: 700;
color: #c0cae1;
position: absolute;
top: 0px;
bottom: 0px;
left: 0px;
right: 0px;
top: 0;
bottom: 0;
left: 0;
right: 0;
margin: auto;
width: 200px;
height: 55px;
Expand Down Expand Up @@ -126,15 +126,15 @@ pub fn get_content() -> String {
border-radius: 8px;
margin-top: 20px;
cursor: pointer;
box-shadow: rgba(50, 50, 93, 0.25) 0px 13px 27px -5px, rgba(0, 0, 0, 0.3) 0px 8px 16px -8px;
box-shadow: rgba(50, 50, 93, 0.25) 0 13px 27px -5px, rgba(0, 0, 0, 0.3) 0 8px 16px -8px;
}
.drop-section input {
display: none;
}
.list-section {
display: none;
text-align: left;
margin: 0px 35px;
margin: 0 35px;
padding-bottom: 20px;
}
.list-section .list-title {
Expand All @@ -143,14 +143,14 @@ pub fn get_content() -> String {
}
.list-section li {
display: flex;
margin: 15px 0px;
margin: 15px 0;
padding-top: 4px;
padding-bottom: 2px;
border-radius: 8px;
transition-duration: 0.2s;
}
.list-section li:hover {
box-shadow: #E3EAF9 0px 0px 4px 0px, #E3EAF9 0px 12px 16px 0px;
box-shadow: #E3EAF9 0 0 4px 0, #E3EAF9 0 12px 16px 0;
}
.list-section li .col {
flex: .1;
Expand Down Expand Up @@ -221,6 +221,44 @@ pub fn get_content() -> String {
display: none;
}
</style>
<style>
.upload {
position: absolute;
top: 3.8%;
right: 313px;
border: none;
padding: 10px 14px;
font-size: 16px;
cursor: pointer;
}
.home {
position: absolute;
top: 3.8%;
right: 217px;
border: none;
padding: 10px 14px;
font-size: 16px;
cursor: pointer;
}
.back {
position: absolute;
top: 3.8%;
right: 132px;
border: none;
padding: 10px 14px;
font-size: 16px;
cursor: pointer;
}
.logout {
position: absolute;
top: 3.8%;
right: 30px;
border: none;
padding: 10px 14px;
font-size: 16px;
cursor: pointer;
}
</style>
</head>
<noscript>
<style>
Expand All @@ -242,6 +280,10 @@ pub fn get_content() -> String {
</div>
</noscript>
<body>
<button class="upload" onclick="upload()"><i class="fa-solid fa-cloud-arrow-up"></i> Upload</button>
<button class="home" onclick="goHome()"><i class="fa fa-home"></i> Home</button>
<button class="back" onclick="goBack()"><i class="fa fa-backward"></i> Back</button>
<button class="logout" onclick="logOut()"><i class="fa fa-sign-out"></i> Logout</button>
<div class="container">
<div class="header-section">
<h1>Upload Files</h1>
Expand Down Expand Up @@ -284,7 +326,7 @@ pub fn get_content() -> String {
}
// Check the file type
function typeValidation(type){
function typeValidation(type) {
var splitType = type.split('/')[0]
if (type == 'application/pdf' || splitType == 'image' || splitType == 'video') {
return true
Expand Down Expand Up @@ -326,7 +368,7 @@ pub fn get_content() -> String {
}
}
// upload file function
function uploadFile(file){
function uploadFile(file) {
listSection.style.display = 'block'
var li = document.createElement('li')
li.classList.add('in-prog')
Expand All @@ -342,7 +384,7 @@ pub fn get_content() -> String {
<div class="file-progress">
<span></span>
</div>
<div class="file-size">${(file.size/(1024*1024)).toFixed(2)} MB</div>
<div class="file-size">${(file.size / (1024 * 1024)).toFixed(2)} MB</div>
</div>
<div class="col">
<svg xmlns="http://www.w3.org/2000/svg" class="cross" height="20" width="20"><path d="m5.979 14.917-.854-.896 4-4.021-4-4.062.854-.896 4.042 4.062 4-4.062.854.896-4 4.062 4 4.021-.854.896-4-4.063Z"/></svg>
Expand All @@ -354,17 +396,26 @@ pub fn get_content() -> String {
var data = new FormData()
data.append('file', file)
http.onload = () => {
li.classList.add('complete')
li.classList.remove('in-prog')
if (http.status === 200) {
// Successful response from the server
li.classList.add('complete');
li.classList.remove('in-prog');
} else {
// Handle error responses
alert('Error uploading file. Status:' + http.status);
return false;
}
}
http.onerror = (error) => {
// Handle network errors
alert('Network error during file upload.');
return false;
};
http.upload.onprogress = (e) => {
var percent_complete = (e.loaded / e.total)*100
var percent_complete = (e.loaded / e.total) * 100
li.querySelectorAll('span')[0].innerHTML = Math.round(percent_complete) + '%'
li.querySelectorAll('span')[1].style.width = percent_complete + '%'
}
console.log("sending data...");
console.log(window.location.origin + '/upload');
console.log(data);
http.open('POST', window.location.origin + '/upload', true);
http.send(data)
li.querySelector('.cross').onclick = () => http.abort()
Expand All @@ -377,6 +428,20 @@ pub fn get_content() -> String {
return splitType + '.png'
}
</script>
<script>
function goHome() {
window.location.href = window.location.origin + "/home";
}
function logOut() {
window.location.href = window.location.origin + "/logout";
}
function upload() {
window.location.href = window.location.origin + "/upload";
}
function goBack() {
window.history.back();
}
</script>
</body>
</html>"#.to_string()
}

0 comments on commit 2981778

Please sign in to comment.