forked from jriou/coller
feat: Add copier and curl links
Signed-off-by: Julien Riou <julien@riou.xyz>
This commit is contained in:
parent
2d8d7efbcb
commit
b316c6ef67
4 changed files with 77 additions and 3 deletions
|
@ -119,7 +119,7 @@ func handleMain() int {
|
|||
var req *http.Request
|
||||
if *password != "" {
|
||||
body := &NotePayload{
|
||||
Password: *password,
|
||||
Password: internal.Encode([]byte(*password)),
|
||||
}
|
||||
payload, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
|
|
|
@ -213,8 +213,14 @@ func (h *GetProtectedNoteHandler) ServeHTTP(w http.ResponseWriter, r *http.Reque
|
|||
}
|
||||
}
|
||||
|
||||
password, err := internal.Decode(body.Password)
|
||||
if err != nil {
|
||||
APIError(w, logger, ErrCouldNotDecodePassword, err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(note.PasswordHash) > 0 {
|
||||
err := bcrypt.CompareHashAndPassword(note.PasswordHash, []byte(body.Password))
|
||||
err := bcrypt.CompareHashAndPassword(note.PasswordHash, password)
|
||||
if err != nil {
|
||||
APIErrorBadRequest(w, logger, ErrInvalidPassword, err)
|
||||
return
|
||||
|
|
|
@ -34,6 +34,7 @@ type PageData struct {
|
|||
AceDirectory string
|
||||
BootstrapDirectory string
|
||||
DisableEditor bool
|
||||
Password string // Not stored in the database
|
||||
}
|
||||
|
||||
func WebError(w http.ResponseWriter, pageData PageData, templates *template.Template, templateName string, logger *slog.Logger, topLevelErr error, err error) {
|
||||
|
@ -439,6 +440,7 @@ func (h *GetProtectedWebNoteHandler) ServeHTTP(w http.ResponseWriter, r *http.Re
|
|||
}
|
||||
}
|
||||
|
||||
h.PageData.Password = password
|
||||
h.PageData.Note = note
|
||||
|
||||
logger.Debug("rendering page")
|
||||
|
|
|
@ -6,6 +6,12 @@
|
|||
<div class="d-flex flex-wrap py-2">
|
||||
<span class="fs-4 d-flex mb-3 mb-md-0 me-md-auto text-decoration-none">Note {{.Note.ID}}</span>
|
||||
<ul class="nav nav-pills align-items-center">
|
||||
<li class="nav-item">
|
||||
<button type="button" class="btn btn-link" id="copier">copier</button>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<button type="button" class="btn btn-link" id="curl">curl</button>
|
||||
</li>
|
||||
<li class="nav-item px-2">
|
||||
<a href="" id="rawURL">raw</a>
|
||||
<script>document.getElementById("rawURL").href = window.location.href.replace(".html", "");</script>
|
||||
|
@ -20,6 +26,65 @@
|
|||
{{end}}
|
||||
</ul>
|
||||
</div>
|
||||
<script>
|
||||
var password = "{{ .Password }}";
|
||||
var encryptionKey = window.location.hash.substr(1);
|
||||
</script>
|
||||
<div id="copierContainer" style="display: none;" class="alert alert-info alert-dismissible" role="alert">
|
||||
<p>Access the note with <strong>copier</strong>:</p>
|
||||
<pre id="copierCommand" style="border: 1px solid;" class="p-2"></pre>
|
||||
<button type="button" class="btn-close" data-dismiss="alert" aria-label="Close" id="copierClose">
|
||||
<span aria-hidden="true"></span>
|
||||
</button>
|
||||
</div>
|
||||
<script>
|
||||
var copierCommand = "copier";
|
||||
var copierOpts = "";
|
||||
if (password != "") {
|
||||
copierOpts += " -password '" + password + "'";
|
||||
}
|
||||
copierCommand += copierOpts + " " + window.location.origin + "/{{ .Note.ID }}";
|
||||
|
||||
if (encryptionKey != "") {
|
||||
copierCommand += "#" + encryptionKey;
|
||||
}
|
||||
document.getElementById("copierCommand").innerHTML = copierCommand;
|
||||
document.getElementById("copier").addEventListener("click", () => {
|
||||
document.getElementById("copierContainer").style.display = "";
|
||||
});
|
||||
document.getElementById("copierClose").addEventListener("click", () => {
|
||||
document.getElementById("copierContainer").style.display = "none";
|
||||
});
|
||||
</script>
|
||||
<div id="curlContainer" style="display: none;" class="alert alert-info alert-dismissible" role="alert">
|
||||
<p>Access the note with <strong>curl</strong>:</p>
|
||||
<pre id="curlCommand" style="border: 1px solid;" class="p-2"></pre>
|
||||
<button type="button" class="btn-close" data-dismiss="alert" aria-label="Close" id="curlClose">
|
||||
<span aria-hidden="true"></span>
|
||||
</button>
|
||||
</div>
|
||||
<script>
|
||||
var curlCommand = "curl";
|
||||
var curlData = {};
|
||||
if (encryptionKey != "") {
|
||||
curlData.encryption_key = encryptionKey;
|
||||
};
|
||||
if (password != "") {
|
||||
curlData.password = window.btoa(password);
|
||||
}
|
||||
var payload = JSON.stringify(curlData);
|
||||
if (payload != "{}") {
|
||||
curlCommand += " -XPOST -d '" + payload + "'";
|
||||
}
|
||||
curlCommand += " " + window.location.origin + "/api/note/{{ .Note.ID }}";
|
||||
document.getElementById("curlCommand").innerHTML = curlCommand;
|
||||
document.getElementById("curl").addEventListener("click", () => {
|
||||
document.getElementById("curlContainer").style.display = "";
|
||||
});
|
||||
document.getElementById("curlClose").addEventListener("click", () => {
|
||||
document.getElementById("curlContainer").style.display = "none";
|
||||
});
|
||||
</script>
|
||||
{{if .DisableEditor}}
|
||||
<div class="row">
|
||||
<pre class="border px-3 pt-3" style="min-height: 300px; resize: vertical; overflow: auto;">
|
||||
|
@ -28,7 +93,8 @@
|
|||
</div>
|
||||
{{else}}
|
||||
<div class="row">
|
||||
<div id="editor" name="editor" class="form-control" style="min-height: 300px; resize: vertical; overflow: auto;">
|
||||
<div id="editor" name="editor" class="form-control"
|
||||
style="min-height: 300px; resize: vertical; overflow: auto;">
|
||||
</div>
|
||||
</div>
|
||||
<script
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue