Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Using cors proxy to save with correct file name
Browse files Browse the repository at this point in the history
  • Loading branch information
the-codeboy committed Mar 16, 2024
1 parent 9361533 commit 35ecdc7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
16 changes: 11 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use rocket::Request;
use rocket_dyn_templates::{context, Template};

enum MyResponse {
Redirect(Redirect),
Template(Template),
Redirect(Box<Redirect>),
Template(Box<Template>),
}

impl<'r, 'o: 'r> Responder<'r, 'o> for MyResponse {
Expand All @@ -30,7 +30,7 @@ impl<'r, 'o: 'r> Responder<'r, 'o> for MyResponse {
#[get("/download/<url>")]
async fn download(url: &str) -> MyResponse {
if !url.starts_with("https://www.studydrive.net/") {
return MyResponse::Redirect(Redirect::to("/not_found"));
return MyResponse::Redirect(Box::new(Redirect::to("/not_found")));
}

let doc_id = url
Expand All @@ -55,7 +55,10 @@ async fn download(url: &str) -> MyResponse {
"https://cdn.studydrive.net/d/prod/documents/{}/original/{}.{}?token={}",
doc_id, doc_id, ending, token
);
return MyResponse::Template(Template::render("download", context! { url: url }));
return MyResponse::Template(Box::new(Template::render(
"download",
context! { url: url, name: name},
)));
}
let name = data["filename"].as_str().unwrap();
let ending = name.split('.').last().unwrap();
Expand All @@ -66,7 +69,10 @@ async fn download(url: &str) -> MyResponse {
"https://cdn.studydrive.net/d/prod/documents/{}/original/{}.{}?token={}",
doc_id, doc_id, ending, token
);
return MyResponse::Template(Template::render("download", context! { url: url }));
MyResponse::Template(Box::new(Template::render(
"download",
context! { url: url, name: name },
)))
}

async fn get_token() -> Result<String, Box<dyn std::error::Error>> {
Expand Down
22 changes: 19 additions & 3 deletions templates/download.tera
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,30 @@
</div>
<div class="row">
<div class="col text-center">
<button class="btn btn-primary" onclick="window.location.href = '{{ url }}'">Click here to download</button>
<a href="{{ url }}"id="download">
<button class="btn btn-primary">Click here if nothing happens</button>
</a>
</div>
</div>
</div>
<script>
fetch("https://corsproxy.io/?"+encodeURI("{{ url }}"))
.then(response => response.blob())
.then(blob => {
let url = window.URL.createObjectURL(blob);
let a = document.createElement("a");
a.href = url;
a.download = "{{ name }}";
document.body.appendChild(a);

setTimeout(function() {
window.location.href = "{{ url }}";
}, 2000);
a.click();
a.remove();
document.querySelector("h1").textContent = "Download of file {{ name }} successful!";
document.getElementById("download").remove();
}, 1000);
})
.catch(error => console.error(error));
</script>
</body>
</html>

0 comments on commit 35ecdc7

Please sign in to comment.