Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add button for copying CID to listing in gateway #9254

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions assets/dir-index-html/dir-index.html

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion assets/dir-index-html/src/dir-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,12 @@
</td>
<td class="no-linebreak">
{{ if .Hash }}
<a class="ipfs-hash" translate="no" href={{ if $root.DNSLink }}"https://cid.ipfs.io/#{{ .Hash | urlEscape}}" target="_blank" rel="noreferrer noopener"{{ else }}"{{ $root.GatewayURL }}/ipfs/{{ .Hash | urlEscape}}?filename={{ .Name | urlEscape }}"{{ end }}>
<a class="ipfs-hash" translate="no" href={{ if $root.DNSLink }}"https://cid.ipfs.io/#{{ .Hash | urlEscape }}" title={{ .Hash | jsonEncode }} target="_blank" rel="noreferrer noopener"{{ else }}"{{ $root.GatewayURL }}/ipfs/{{ .Hash | urlEscape }}?filename={{ .Name | urlEscape }}"{{ end }}>
{{ .ShortHash }}
</a>
<button class="type-icon" title="Copy CID" onclick=copyText({{ .Hash | jsonEncode }})>
<div class="ipfs-_copy">&nbsp;</div>
</button>
{{ end }}
</td>
<td class="no-linebreak">{{ .Size }}</td>
Expand All @@ -96,3 +99,8 @@
</div>
</body>
</html>
<script>
function copyText(text) {
navigator.clipboard.writeText(text);
}
</script>
7 changes: 7 additions & 0 deletions assets/dir-index-html/src/icons.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions assets/dir-index-html/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ a:visited {
color:#00b0e9
}

button {
padding: 0;
border: none;
background: none;
vertical-align: middle;
}

button:hover {
background-color: #00b0e9;
cursor: pointer;
}

strong {
font-weight:700
}
Expand Down
8 changes: 8 additions & 0 deletions assets/dir-index-html/test/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
Expand Down Expand Up @@ -95,6 +96,13 @@ func main() {
pathUrl := url.URL{Path: rawUrl}
return pathUrl.String()
},
"jsonEncode": func(rawString string) string {
encodedStr, err := json.Marshal(rawString)
if err != nil {
panic(err)
}
return string(encodedStr)
Comment on lines +100 to +104
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
encodedStr, err := json.Marshal(rawString)
if err != nil {
panic(err)
}
return string(encodedStr)
var builder strings.Builder
err := json.NewEncoder(&builder).Encode(rawString)
if err != nil {
panic(err)
}
return builder.String()

},
}).ParseFiles(templateFile)
if err != nil {
http.Error(w, fmt.Sprintf("failed to parse template file: %s", err), http.StatusInternalServerError)
Expand Down
11 changes: 11 additions & 0 deletions core/corehttp/gateway_indexPage.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package corehttp

import (
"encoding/json"
"html/template"
"net/url"
"path"
Expand Down Expand Up @@ -121,6 +122,15 @@ func init() {
return pathUrl.String()
}

// custom function to encode strings in json, to future-proof for new encoding
jsonEncode := func(rawString string) string {
encodedStr, err := json.Marshal(rawString)
if err != nil {
panic(err)
}
return string(encodedStr)
Comment on lines +127 to +131
Copy link
Contributor

@Jorropo Jorropo Nov 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
encodedStr, err := json.Marshal(rawString)
if err != nil {
panic(err)
}
return string(encodedStr)
var builder strings.Builder
err := json.NewEncoder(&builder).Encode(rawString)
if err != nil {
panic(err)
}
return builder.String()

}

// Directory listing template
dirIndexBytes, err := assets.Asset.ReadFile("dir-index-html/dir-index.html")
if err != nil {
Expand All @@ -130,5 +140,6 @@ func init() {
listingTemplate = template.Must(template.New("dir").Funcs(template.FuncMap{
"iconFromExt": iconFromExt,
"urlEscape": urlEscape,
"jsonEncode": jsonEncode,
}).Parse(string(dirIndexBytes)))
}