Skip to content

Commit c34246f

Browse files
committed
add copy button
1 parent 422649e commit c34246f

File tree

4 files changed

+69
-16
lines changed

4 files changed

+69
-16
lines changed

src/config.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl Config {
2323
pub fn load(path: &Path) -> Result<Self> {
2424
Figment::new()
2525
.merge(Toml::string(include_str!("../config.toml")))
26-
.merge(Toml::file(&path))
26+
.merge(Toml::file(path))
2727
.merge(Env::prefixed("PASTEMP_"))
2828
.extract()
2929
.context("Loading Config")

src/simple.rs

+18-7
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,14 @@ async fn get_ext(
192192
file_name: FileName,
193193
delete_at: Option<DateTime>,
194194
owner: bool,
195+
copy: Option<String>,
195196
}
196197

197198
Image {
198199
file_name,
199200
delete_at,
200201
owner,
202+
copy: None,
201203
}
202204
.to_response()
203205
}
@@ -212,6 +214,7 @@ async fn get_ext(
212214
file_name: FileName,
213215
delete_at: Option<DateTime>,
214216
owner: bool,
217+
copy: Option<String>,
215218
}
216219

217220
if let Some(syntax) = syntax {
@@ -229,10 +232,11 @@ async fn get_ext(
229232
.is_err()
230233
{
231234
return Ok(UnHighlighted {
232-
code: file,
235+
code: file.clone(),
233236
file_name,
234237
delete_at,
235238
owner,
239+
copy: Some(file),
236240
}
237241
.to_response()
238242
.customize());
@@ -246,21 +250,24 @@ async fn get_ext(
246250
file_name: FileName,
247251
delete_at: Option<DateTime>,
248252
owner: bool,
253+
copy: Option<String>,
249254
}
250255

251256
Highlighted {
252257
code: html_generator.finalize(),
253258
file_name,
254259
delete_at,
255260
owner,
261+
copy: Some(file),
256262
}
257263
.to_response()
258264
} else {
259265
UnHighlighted {
260-
code: file,
266+
code: file.clone(),
261267
file_name,
262268
delete_at,
263269
owner,
270+
copy: Some(file),
264271
}
265272
.to_response()
266273
}
@@ -271,12 +278,14 @@ async fn get_ext(
271278
file_name: FileName,
272279
delete_at: Option<DateTime>,
273280
owner: bool,
281+
copy: Option<String>,
274282
}
275283

276284
WrongType {
277285
file_name,
278286
delete_at,
279287
owner,
288+
copy: None,
280289
}
281290
.to_response()
282291
}
@@ -288,12 +297,14 @@ async fn get_ext(
288297
file_name: FileName,
289298
delete_at: Option<DateTime>,
290299
owner: bool,
300+
copy: Option<String>,
291301
}
292302

293303
TooLarge {
294304
file_name,
295305
delete_at,
296306
owner,
307+
copy: None,
297308
}
298309
.to_response()
299310
}
@@ -357,7 +368,7 @@ async fn delete_entry(
357368
.map_err(ErrorInternalServerError)?;
358369
}
359370

360-
Ok(HttpResponse::Found()
371+
Ok(HttpResponse::SeeOther()
361372
.append_header((header::LOCATION, "/"))
362373
.finish())
363374
}
@@ -427,16 +438,16 @@ fn response(
427438
extension: Option<String>,
428439
config: &Data<Config>,
429440
) -> impl Responder {
430-
let name = name.clone() + &extension.map(|e| format!(".{e}")).unwrap_or_default();
431-
HttpResponse::Found()
441+
let name = name + &extension.map(|e| format!(".{e}")).unwrap_or_default();
442+
HttpResponse::SeeOther()
432443
.append_header((header::LOCATION, name.clone()))
433444
.cookie_delta(&cookies)
434445
.body(format!(
435446
"{}{name}\n",
436447
if !config.base_url.is_empty() && !config.base_url.ends_with('/') {
437448
format!("{}{}", config.base_url, "/")
438449
} else {
439-
format!("{}", config.base_url)
450+
config.base_url.to_string()
440451
}
441452
))
442453
}
@@ -509,7 +520,7 @@ async fn post_form(
509520
}
510521

511522
Ok(response(
512-
file.ok_or_else(|| UploadError::NoData)?,
523+
file.ok_or(UploadError::NoData)?,
513524
cookies,
514525
extension,
515526
&config,

templates/get.html

+46-8
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,52 @@
44
<a href=".." class="btn filled blue" id="new">New Paste</a>
55
{% block file %} {% endblock %}
66

7-
<a id="download" class="btn outlined" style="margin-inline: auto" href="download/{{file_name}}" download="{{file_name}}">
8-
<svg width="24px" height="24px" fill="currentColor" version="1.1" viewBox="0 0 24 24"
9-
xmlns="http://www.w3.org/2000/svg">
10-
<path
11-
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" />
12-
</svg>
13-
Download
14-
</a>
7+
<row style="gap: 1em; justify-content: center;" id="buttons">
8+
<a id="download" class="btn outlined" href="download/{{file_name}}" download="{{file_name}}">
9+
<svg width="24px" height="24px" fill="currentColor" version="1.1" viewBox="0 0 24 24"
10+
xmlns="http://www.w3.org/2000/svg">
11+
<path
12+
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" />
13+
</svg>
14+
Download
15+
</a>
16+
17+
<a class="btn outlined" href="raw/{{file_name}}">
18+
<!-- <svg width="24px" height="24px" fill="currentColor" version="1.1" viewBox="0 0 24 24" -->
19+
<!-- xmlns="http://www.w3.org/2000/svg"> -->
20+
<!-- <path -->
21+
<!-- 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" /> -->
22+
<!-- </svg> -->
23+
Raw
24+
</a>
25+
26+
{% match copy %}
27+
{% when Some with (copy) %}
28+
<button class="btn outlined" hidden id="copy"
29+
onclick="copy()">
30+
<svg width="24px" height="24px" fill="currentColor" version="1.1" viewBox="0 -960 960 960"
31+
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>
32+
Copy
33+
</button>
34+
<script>
35+
document.querySelector("#copy").hidden=false
36+
function copy() {
37+
navigator.clipboard.writeText({{copy|json}})
38+
}
39+
// TODO wait for FF https://bugzilla.mozilla.org/show_bug.cgi?id=1809106
40+
// async function copy() {
41+
// console.log('hello');
42+
// const data = await fetch('raw/{{file_name}}');
43+
// const blob = await data.blob();
44+
// console.log(blob);
45+
// navigator.clipboard.write([
46+
// new ClipboardItem({[blob.type]: blob})
47+
// ]);
48+
// }
49+
</script>
50+
{% else %}
51+
{% endmatch %}
52+
</row>
1553

1654
<!-- <p>Should you no longer want to share this, or this entry should be deleted for a different reason e.g. copyright -->
1755
<!-- infringement, it can easily be deleted below.</p> -->

templates/style.css

+4
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ row {
253253
display: flex;
254254
}
255255

256+
*[hidden] {
257+
display: none !important
258+
}
259+
256260
dialog {
257261
position: fixed;
258262
margin: auto;

0 commit comments

Comments
 (0)