From 2671261c10f3e617e808d345758d990d783db1f6 Mon Sep 17 00:00:00 2001
From: Philipp Heckel
Date: Mon, 28 Jun 2021 13:48:57 -0400
Subject: [PATCH] Fix formatting in curl and UI
---
server/curl.tmpl | 14 ++++++------
server/index.gohtml | 17 +++++++++-----
server/static/js/app.js | 49 ++++++++++++++++++++++++++++++++++++-----
3 files changed, 62 insertions(+), 18 deletions(-)
diff --git a/server/curl.tmpl b/server/curl.tmpl
index bd536f2..f0283da 100644
--- a/server/curl.tmpl
+++ b/server/curl.tmpl
@@ -1,10 +1,10 @@
-{{- /*gotype: heckel.io/pcopy.webTemplateConfig*/ -}}
+{{- /*gotype: heckel.io/pcopy/server.webTemplateConfig*/ -}}
{{- $url := .Config.ServerAddr | expandServerAddr -}}
NAME
pcopy - copy/paste across machines
USAGE:
- curl [-T FILE] [-d DATA] [-u:PASS] {{$url}}[/FILENAME][?s=1][&m=rw|ro][&t=][&f=text|json]
+ curl [-T FILE] [-d DATA] [-u:PASS] {{$url}}[/FILENAME][?s=1][&m=rw|ro][&t=DURATION][&f=text|json]
DESCRIPTION:
This is is the curl-endpoint for pcopy, a tool to copy/paste across machines. You may use
@@ -30,10 +30,10 @@ DESCRIPTION:
OPTIONS:
Query params:
- ?s=1 stream data without storing on the server
- ?m=rw|ro defines whether to set the file mode as read-write or read-only (default: {{index .Config.FileModesAllowed 0}}, allowed: {{stringsJoin .Config.FileModesAllowed ", "}})
- ?t= time-to-live (TTL) for this file, after which it will be deleted (default/max: {{if .Config.FileExpireAfter}}{{.Config.FileExpireAfter | durationToHuman}}{{else}}never{{end}})
- ?f=text|json output format for PUT/POSTs (default: text)
+ ?s=1 stream data without storing on the server
+ ?m=rw|ro defines whether to set the file mode as read-write or read-only (default: {{index .Config.FileModesAllowed 0}}, allowed: {{stringsJoin .Config.FileModesAllowed ", "}})
+ ?t=DURATION time-to-live after which the file will be deleted (default: {{if .Config.FileExpireAfterDefault}}{{.Config.FileExpireAfterDefault | durationToHuman}}{{else}}never{{end}}, nontext-max: {{if .Config.FileExpireAfterNonTextMax}}{{.Config.FileExpireAfterNonTextMax | durationToHuman}}{{else}}never{{end}}, text-max: {{if .Config.FileExpireAfterTextMax}}{{.Config.FileExpireAfterTextMax | durationToHuman}}{{else}}never{{end}})
+ ?f=text|json output format for PUT/POSTs (default: text)
Common curl options (see 'man curl' for more):
-T FILE uploads file FILE to the server
@@ -47,7 +47,7 @@ LIMITS:
Total clipboard size limit: {{if .Config.ClipboardSizeLimit}}{{.Config.ClipboardSizeLimit | bytesToHuman }}{{else}}no limit{{end}}
Total number of files: {{if .Config.ClipboardCountLimit}} max. {{.Config.ClipboardCountLimit }} files{{else}}no limit{{end}}
Per-file size limit: {{if .Config.FileSizeLimit}}{{.Config.FileSizeLimit | bytesToHuman }}{{else}}no limit{{end}}
- Per-file expiration: {{if .Config.FileExpireAfter}}deleted after {{.Config.FileExpireAfter | durationToHuman }}{{else}}never{{end}}
+ Per-file expiration limits: {{if .Config.FileExpireAfterTextMax}}{{.Config.FileExpireAfterTextMax | durationToHuman }}{{else}}never{{end}} if text, {{if .Config.FileExpireAfterNonTextMax}}{{.Config.FileExpireAfterNonTextMax | durationToHuman }}{{else}}never{{end}} otherwise
Allowed file modes: {{stringsJoin .Config.FileModesAllowed ", "}}
To find out more about pcopy, check out https://heckel.io/pcopy.
diff --git a/server/index.gohtml b/server/index.gohtml
index a2c6c25..392d045 100644
--- a/server/index.gohtml
+++ b/server/index.gohtml
@@ -1,4 +1,4 @@
-{{- /*gotype: heckel.io/pcopy.webTemplateConfig*/ -}}
+{{- /*gotype: heckel.io/pcopy/server.webTemplateConfig*/ -}}
@@ -83,16 +83,21 @@
-
+
@@ -136,7 +141,7 @@
Total clipboard size limit:{{if .Config.ClipboardSizeLimit}}{{.Config.ClipboardSizeLimit | bytesToHuman }}{{else}}no limit{{end}} Total number of files:{{if .Config.ClipboardCountLimit}} max. {{.Config.ClipboardCountLimit }} files{{else}}no limit{{end}} Per-file size limit:{{if .Config.FileSizeLimit}}{{.Config.FileSizeLimit | bytesToHuman }}{{else}}no limit{{end}}
- Per-file expiration:{{if .Config.FileExpireAfter}}deleted after {{.Config.FileExpireAfter | durationToHuman }}{{else}}never{{end}}
+ Per-file expiration limits:{{if .Config.FileExpireAfterTextMax}}{{.Config.FileExpireAfterTextMax | durationToHuman }}{{else}}never{{end}} if text, {{if .Config.FileExpireAfterNonTextMax}}{{.Config.FileExpireAfterNonTextMax | durationToHuman }}{{else}}never{{end}} otherwise
@@ -243,7 +248,9 @@
KeyLenBytes: {{.KeyLenBytes}},
DefaultPort: {{.DefaultPort}},
FileSizeLimit: {{.Config.FileSizeLimit}},
- FileExpireAfter: {{.Config.FileExpireAfter.Seconds}}
+ FileExpireAfterDefault: {{.Config.FileExpireAfterDefault.Seconds}},
+ FileExpireAfterTextMax: {{.Config.FileExpireAfterTextMax.Seconds}},
+ FileExpireAfterNonTextMax: {{.Config.FileExpireAfterNonTextMax.Seconds}}
}
diff --git a/server/static/js/app.js b/server/static/js/app.js
index 91d706c..f4590e6 100644
--- a/server/static/js/app.js
+++ b/server/static/js/app.js
@@ -217,17 +217,32 @@ headerStream.addEventListener('change', (e) => { storeStreamEnabled(e.target.che
headerTTL.addEventListener('change', (e) => {
storeTTL(e.target.value)
+ changeWidth(e.target)
})
let ttl = getTTL()
Array.from(headerTTL.options).forEach(function(option) {
- const removeNeverOption = parseInt(option.value) === 0 && config.FileExpireAfter > 0
- const removeHigherOption = option.value > config.FileExpireAfter && config.FileExpireAfter > 0
- const isStoredTTL = ttl !== null && parseInt(option.value) === ttl
- if (removeNeverOption || removeHigherOption) {
+ const value = parseInt(option.value)
+ const allowOptionText = config.FileExpireAfterTextMax === 0 || (value > 0 && value <= config.FileExpireAfterTextMax)
+ const allowOptionNonText = config.FileExpireAfterNonTextMax === 0 || (value > 0 && value <= config.FileExpireAfterNonTextMax)
+
+ // Remove option if not allowed
+ if (!allowOptionText && !allowOptionNonText) {
headerTTL.removeChild(option)
- } else if (isStoredTTL) {
+ return
+ }
+
+ // Change option text if only allowed for text or non-text
+ if (allowOptionText && !allowOptionNonText) {
+ option.text += " (text only)"
+ } else if (!allowOptionText && allowOptionNonText) {
+ option.text += " (non-text only)"
+ }
+
+ // Select option if stored in local storage
+ if (value === ttl || (value > 0 && value < ttl)) {
option.selected = 'selected'
+ changeWidth(headerTTL)
}
})
@@ -235,6 +250,24 @@ if (headerTTL.options.length === 0) {
headerTTL.classList.add('hidden')
}
+/* From: https://stackoverflow.com/a/35567280/1440785 & https://jsfiddle.net/Hatchet/a0xzz6mf/ */
+function changeWidth(select) {
+ var o = select.options[select.selectedIndex];
+ var s = document.createElement('span');
+
+ s.textContent = o.textContent;
+
+ var ostyles = getComputedStyle(o);
+ s.style.fontFamily = ostyles.fontFamily;
+ s.style.fontStyle = ostyles.fontStyle;
+ s.style.fontWeight = ostyles.fontWeight;
+ s.style.fontSize = ostyles.fontSize;
+
+ document.body.appendChild(s);
+ select.style.width = (s.offsetWidth + 30) + 'px';
+ document.body.removeChild(s);
+}
+
/* Text field & saving text */
headerSaveButton.addEventListener('click', save)
@@ -726,7 +759,7 @@ function getTTL() {
if (localStorage.getItem('ttl') !== null) {
return parseInt(localStorage.getItem('ttl'))
} else {
- return parseInt(headerTTL.value)
+ return parseInt(config.FileExpireAfterDefault)
}
}
@@ -750,6 +783,10 @@ function secondsToHuman(seconds) {
function numberEnding (number) {
return (number > 1) ? 's' : '';
}
+ let years = Math.floor(seconds / 31536000)
+ if (years) {
+ return years + ' year' + numberEnding(years);
+ }
let days = Math.floor((seconds %= 31536000) / 86400);
if (days) {
return days + ' day' + numberEnding(days);