Skip to content

Commit

Permalink
Fix formatting in curl and UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Philipp Heckel committed Jun 28, 2021
1 parent 1a621ac commit 2671261
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 18 deletions.
14 changes: 7 additions & 7 deletions server/curl.tmpl
Original file line number Diff line number Diff line change
@@ -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=<duration>][&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
Expand All @@ -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=<duration> 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
Expand All @@ -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.
17 changes: 12 additions & 5 deletions server/index.gohtml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{{- /*gotype: heckel.io/pcopy.webTemplateConfig*/ -}}
{{- /*gotype: heckel.io/pcopy/server.webTemplateConfig*/ -}}
<!DOCTYPE html>
<html lang="en">
<head>
Expand Down Expand Up @@ -83,16 +83,21 @@
</div>
<div class="col-auto">
<div class="divider"></div>
<label for="ttl">Expires: {{if lt .Config.FileExpireAfter.Seconds 1800.0}} {{.Config.FileExpireAfter | durationToHuman}}{{end}}</label>
<label for="ttl">Expires: {{if lt .Config.FileExpireAfterDefault.Seconds 600.0}} {{.Config.FileExpireAfterDefault | durationToHuman}}{{end}}</label>
<select id="ttl" class="button">
<option value="600">10 min</option>
<option value="1800">30 min</option>
<option value="3600">1 hour</option>
<option value="10800">3 hours</option>
<option value="21600">6 hours</option>
<option value="86400">1 day</option>
<option value="259200">3 days</option>
<option value="604800">1 week</option>
<option value="1814400">3 weeks</option>
<option value="3628800">6 weeks</option>
<option value="5184000">2 months</option>
<option value="15552000">6 months</option>
<option value="31536000">1 year</option>
<option value="63072000">2 years</option>
<option value="0">never</option>
</select>
</div>
Expand Down Expand Up @@ -136,7 +141,7 @@
<b>Total clipboard size limit:</b> <em>{{if .Config.ClipboardSizeLimit}}{{.Config.ClipboardSizeLimit | bytesToHuman }}{{else}}no limit{{end}}</em><br/>
<b>Total number of files:</b> <em>{{if .Config.ClipboardCountLimit}} max. {{.Config.ClipboardCountLimit }} files{{else}}no limit{{end}}</em><br/>
<b>Per-file size limit:</b> <em>{{if .Config.FileSizeLimit}}{{.Config.FileSizeLimit | bytesToHuman }}{{else}}no limit{{end}}</em><br/>
<b>Per-file expiration:</b> <em>{{if .Config.FileExpireAfter}}deleted after {{.Config.FileExpireAfter | durationToHuman }}{{else}}never{{end}}</em>
<b>Per-file expiration limits:</b> <em>{{if .Config.FileExpireAfterTextMax}}{{.Config.FileExpireAfterTextMax | durationToHuman }}{{else}}never{{end}} if text, {{if .Config.FileExpireAfterNonTextMax}}{{.Config.FileExpireAfterNonTextMax | durationToHuman }}{{else}}never{{end}} otherwise</em>
</p>
</div>
<div id="info-upload-header-active" class="info-header">
Expand Down Expand Up @@ -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}}
}
</script>
<script src="static/vendor/crypto-js.min.js"></script>
Expand Down
49 changes: 43 additions & 6 deletions server/static/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,24 +217,57 @@ 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)
}
})

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)
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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);
Expand Down

0 comments on commit 2671261

Please sign in to comment.