Skip to content

Commit

Permalink
add copy button
Browse files Browse the repository at this point in the history
  • Loading branch information
ModProg committed Jun 14, 2023
1 parent 422649e commit c34246f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl Config {
pub fn load(path: &Path) -> Result<Self> {
Figment::new()
.merge(Toml::string(include_str!("../config.toml")))
.merge(Toml::file(&path))
.merge(Toml::file(path))
.merge(Env::prefixed("PASTEMP_"))
.extract()
.context("Loading Config")
Expand Down
25 changes: 18 additions & 7 deletions src/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ async fn get_ext(
file_name: FileName,
delete_at: Option<DateTime>,
owner: bool,
copy: Option<String>,
}

Image {
file_name,
delete_at,
owner,
copy: None,
}
.to_response()
}
Expand All @@ -212,6 +214,7 @@ async fn get_ext(
file_name: FileName,
delete_at: Option<DateTime>,
owner: bool,
copy: Option<String>,
}

if let Some(syntax) = syntax {
Expand All @@ -229,10 +232,11 @@ async fn get_ext(
.is_err()
{
return Ok(UnHighlighted {
code: file,
code: file.clone(),
file_name,
delete_at,
owner,
copy: Some(file),
}
.to_response()
.customize());
Expand All @@ -246,21 +250,24 @@ async fn get_ext(
file_name: FileName,
delete_at: Option<DateTime>,
owner: bool,
copy: Option<String>,
}

Highlighted {
code: html_generator.finalize(),
file_name,
delete_at,
owner,
copy: Some(file),
}
.to_response()
} else {
UnHighlighted {
code: file,
code: file.clone(),
file_name,
delete_at,
owner,
copy: Some(file),
}
.to_response()
}
Expand All @@ -271,12 +278,14 @@ async fn get_ext(
file_name: FileName,
delete_at: Option<DateTime>,
owner: bool,
copy: Option<String>,
}

WrongType {
file_name,
delete_at,
owner,
copy: None,
}
.to_response()
}
Expand All @@ -288,12 +297,14 @@ async fn get_ext(
file_name: FileName,
delete_at: Option<DateTime>,
owner: bool,
copy: Option<String>,
}

TooLarge {
file_name,
delete_at,
owner,
copy: None,
}
.to_response()
}
Expand Down Expand Up @@ -357,7 +368,7 @@ async fn delete_entry(
.map_err(ErrorInternalServerError)?;
}

Ok(HttpResponse::Found()
Ok(HttpResponse::SeeOther()
.append_header((header::LOCATION, "/"))
.finish())
}
Expand Down Expand Up @@ -427,16 +438,16 @@ fn response(
extension: Option<String>,
config: &Data<Config>,
) -> impl Responder {
let name = name.clone() + &extension.map(|e| format!(".{e}")).unwrap_or_default();
HttpResponse::Found()
let name = name + &extension.map(|e| format!(".{e}")).unwrap_or_default();
HttpResponse::SeeOther()
.append_header((header::LOCATION, name.clone()))
.cookie_delta(&cookies)
.body(format!(
"{}{name}\n",
if !config.base_url.is_empty() && !config.base_url.ends_with('/') {
format!("{}{}", config.base_url, "/")
} else {
format!("{}", config.base_url)
config.base_url.to_string()
}
))
}
Expand Down Expand Up @@ -509,7 +520,7 @@ async fn post_form(
}

Ok(response(
file.ok_or_else(|| UploadError::NoData)?,
file.ok_or(UploadError::NoData)?,
cookies,
extension,
&config,
Expand Down
54 changes: 46 additions & 8 deletions templates/get.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,52 @@
<a href=".." class="btn filled blue" id="new">New Paste</a>
{% block file %} {% endblock %}

<a id="download" class="btn outlined" style="margin-inline: auto" href="download/{{file_name}}" download="{{file_name}}">
<svg width="24px" height="24px" fill="currentColor" version="1.1" viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg">
<path
d="m12 3c-0.55228 0-1 0.44772-1 1v8.1699l-2.5391-2.5293-0.015625-0.017578a1 1 0 0 0-0.74023-0.32812 1 1 0 0 0-1 1 1 1 0 0 0 0.29492 0.70508 1 1 0 0 0 0.050781 0.05078l3.5352 3.5352c0.78101 0.78085 2.0471 0.78085 2.8281 0l3.5215-3.5215a1 1 0 0 0 0.36133-0.76758 1 1 0 0 0-1-1 1 1 0 0 0-0.70508 0.29492l-2e-3 -0.00195-2.5898 2.5801v-8.1699c0-0.55228-0.44772-1-1-1zm-7 11c-0.55228 0-1 0.44772-1 1v3c0 1.1 0.9 2 2 2h12c1.1 0 2-0.9 2-2v-3c0-0.55228-0.44772-1-1-1s-1 0.44772-1 1v3h-12v-3c0-0.55228-0.44772-1-1-1z" />
</svg>
Download
</a>
<row style="gap: 1em; justify-content: center;" id="buttons">
<a id="download" class="btn outlined" href="download/{{file_name}}" download="{{file_name}}">
<svg width="24px" height="24px" fill="currentColor" version="1.1" viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg">
<path
d="m12 3c-0.55228 0-1 0.44772-1 1v8.1699l-2.5391-2.5293-0.015625-0.017578a1 1 0 0 0-0.74023-0.32812 1 1 0 0 0-1 1 1 1 0 0 0 0.29492 0.70508 1 1 0 0 0 0.050781 0.05078l3.5352 3.5352c0.78101 0.78085 2.0471 0.78085 2.8281 0l3.5215-3.5215a1 1 0 0 0 0.36133-0.76758 1 1 0 0 0-1-1 1 1 0 0 0-0.70508 0.29492l-2e-3 -0.00195-2.5898 2.5801v-8.1699c0-0.55228-0.44772-1-1-1zm-7 11c-0.55228 0-1 0.44772-1 1v3c0 1.1 0.9 2 2 2h12c1.1 0 2-0.9 2-2v-3c0-0.55228-0.44772-1-1-1s-1 0.44772-1 1v3h-12v-3c0-0.55228-0.44772-1-1-1z" />
</svg>
Download
</a>

<a class="btn outlined" href="raw/{{file_name}}">
<!-- <svg width="24px" height="24px" fill="currentColor" version="1.1" viewBox="0 0 24 24" -->
<!-- xmlns="http://www.w3.org/2000/svg"> -->
<!-- <path -->
<!-- d="m12 3c-0.55228 0-1 0.44772-1 1v8.1699l-2.5391-2.5293-0.015625-0.017578a1 1 0 0 0-0.74023-0.32812 1 1 0 0 0-1 1 1 1 0 0 0 0.29492 0.70508 1 1 0 0 0 0.050781 0.05078l3.5352 3.5352c0.78101 0.78085 2.0471 0.78085 2.8281 0l3.5215-3.5215a1 1 0 0 0 0.36133-0.76758 1 1 0 0 0-1-1 1 1 0 0 0-0.70508 0.29492l-2e-3 -0.00195-2.5898 2.5801v-8.1699c0-0.55228-0.44772-1-1-1zm-7 11c-0.55228 0-1 0.44772-1 1v3c0 1.1 0.9 2 2 2h12c1.1 0 2-0.9 2-2v-3c0-0.55228-0.44772-1-1-1s-1 0.44772-1 1v3h-12v-3c0-0.55228-0.44772-1-1-1z" /> -->
<!-- </svg> -->
Raw
</a>

{% match copy %}
{% when Some with (copy) %}
<button class="btn outlined" hidden id="copy"
onclick="copy()">
<svg width="24px" height="24px" fill="currentColor" version="1.1" viewBox="0 -960 960 960"
xmlns="http://www.w3.org/2000/svg"><path d="M200-80q-33 0-56.5-23.5T120-160v-560h80v560h440v80H200Zm160-160q-33 0-56.5-23.5T280-320v-480q0-33 23.5-56.5T360-880h360q33 0 56.5 23.5T800-800v480q0 33-23.5 56.5T720-240H360Zm0-80h360v-480H360v480Zm0 0v-480 480Z"/></svg>
Copy
</button>
<script>
document.querySelector("#copy").hidden=false
function copy() {
navigator.clipboard.writeText({{copy|json}})
}
// TODO wait for FF https://bugzilla.mozilla.org/show_bug.cgi?id=1809106
// async function copy() {
// console.log('hello');
// const data = await fetch('raw/{{file_name}}');
// const blob = await data.blob();
// console.log(blob);
// navigator.clipboard.write([
// new ClipboardItem({[blob.type]: blob})
// ]);
// }
</script>
{% else %}
{% endmatch %}
</row>

<!-- <p>Should you no longer want to share this, or this entry should be deleted for a different reason e.g. copyright -->
<!-- infringement, it can easily be deleted below.</p> -->
Expand Down
4 changes: 4 additions & 0 deletions templates/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,10 @@ row {
display: flex;
}

*[hidden] {
display: none !important
}

dialog {
position: fixed;
margin: auto;
Expand Down

0 comments on commit c34246f

Please sign in to comment.