Skip to content

Commit

Permalink
Merge pull request #151 from frap129/short-path
Browse files Browse the repository at this point in the history
Add short-path for changing URL provided by copy buttons
  • Loading branch information
szabodanika committed Apr 20, 2023
2 parents 0dbe949 + 95aa514 commit b6841a5
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct Args {
#[clap(long, env="MICROBIN_PUBLIC_PATH", default_value_t = PublicUrl(String::from("")))]
pub public_path: PublicUrl,

#[clap(long, env="MICROBIN_SHORT_PATH", default_value_t = PublicUrl(String::from("")))]
pub short_path: PublicUrl,

#[clap(long, env = "MICROBIN_READONLY")]
pub readonly: bool,

Expand Down
27 changes: 23 additions & 4 deletions src/endpoints/pasta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ struct PastaTemplate<'a> {
args: &'a Args,
}

#[get("/pasta/{id}")]
pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
fn pastaresponse(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
// get access to the pasta collection
let mut pastas = data.pastas.lock().unwrap();

Expand Down Expand Up @@ -82,8 +81,17 @@ pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpR
.body(ErrorTemplate { args: &ARGS }.render().unwrap())
}

#[get("/url/{id}")]
pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
#[get("/pasta/{id}")]
pub async fn getpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
pastaresponse(data, id)
}

#[get("/p/{id}")]
pub async fn getshortpasta(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
pastaresponse(data, id)
}

fn urlresponse(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
// get access to the pasta collection
let mut pastas = data.pastas.lock().unwrap();

Expand Down Expand Up @@ -149,6 +157,17 @@ pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> Ht
.body(ErrorTemplate { args: &ARGS }.render().unwrap())
}


#[get("/url/{id}")]
pub async fn redirecturl(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
urlresponse(data, id)
}

#[get("/u/{id}")]
pub async fn shortredirecturl(data: web::Data<AppState>, id: web::Path<String>) -> HttpResponse {
urlresponse(data, id)
}

#[get("/raw/{id}")]
pub async fn getrawpasta(data: web::Data<AppState>, id: web::Path<String>) -> String {
// get access to the pasta collection
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ async fn main() -> std::io::Result<()> {
.service(create::index)
.service(info::info)
.service(pasta_endpoint::getpasta)
.service(pasta_endpoint::getshortpasta)
.service(pasta_endpoint::getrawpasta)
.service(pasta_endpoint::redirecturl)
.service(pasta_endpoint::shortredirecturl)
.service(edit::get_edit)
.service(edit::post_edit)
.service(static_resources::static_resources)
Expand Down
6 changes: 3 additions & 3 deletions templates/pasta.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@
const copyTextBtn = document.getElementById("copy-text-button")
const copyRedirectBtn = document.getElementById("copy-redirect-button")
const content = `{{ pasta.content_escaped() }}`
const url = `{{ args.public_path }}/pasta/{{pasta.id_as_animals()}}`
const redirect_url = `{{ args.public_path }}/url/{{pasta.id_as_animals()}}`
const url = (`{{ args.short_path }}` === "") ? `{{ args.public_path }}/pasta/{{pasta.id_as_animals()}}` : `{{ args.short_path }}/p/{{pasta.id_as_animals()}}`
const redirect_url = (`{{ args.short_path }}` === "") ? `{{ args.public_path }}/url/{{pasta.id_as_animals()}}` : `{{ args.short_path }}/u/{{pasta.id_as_animals()}}`

const decodeEntity = (inputStr) => {
var textarea = document.createElement("textarea");
Expand Down Expand Up @@ -151,4 +151,4 @@
}
</style>

{% include "footer.html" %}
{% include "footer.html" %}
7 changes: 6 additions & 1 deletion templates/pastalist.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ <h3>URL Redirects</h3>
</td>
<td>
<a style="margin-right:1rem" href="{{ args.public_path }}/url/{{pasta.id_as_animals()}}">Open</a>
{% if args.short_path.to_string() == "" %}
<a style="margin-right:1rem; cursor: pointer;" id="copy-button"
data-url="{{ args.public_path }}/url/{{pasta.id_as_animals()}}">Copy</a>
{% else %}
<a style="margin-right:1rem; cursor: pointer;" id="copy-button"
data-url="{{ args.short_path }}/u/{{pasta.id_as_animals()}}">Copy</a>
{% endif %}
{% if pasta.editable %}
<a style="margin-right:1rem" href="{{ args.public_path }}/edit/{{pasta.id_as_animals()}}">Edit</a>
{%- endif %}
Expand All @@ -118,4 +123,4 @@ <h3>URL Redirects</h3>

</script>

{% include "footer.html" %}
{% include "footer.html" %}

0 comments on commit b6841a5

Please sign in to comment.