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 1 commit
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
8 changes: 4 additions & 4 deletions assets/dir-index-html/dir-index.html

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions assets/dir-index-html/src/dir-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@
</td>
<td class="no-linebreak">
{{ if .Hash }}
<a class="ipfs-hash" translate="no" href={{ if $root.DNSLink }}"https://cid.ipfs.io/#{{ .Hash | urlEscape}}" title="{{ .Hash }}" 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 }}')">
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 115.77 122.88" style="enable-background:new 0 0 115.77 122.88" xml:space="preserve"><style type="text/css">.st0{fill-rule:evenodd;clip-rule:evenodd;}</style><g><path class="st0" d="M89.62,13.96v7.73h12.19h0.01v0.02c3.85,0.01,7.34,1.57,9.86,4.1c2.5,2.51,4.06,5.98,4.07,9.82h0.02v0.02 v73.27v0.01h-0.02c-0.01,3.84-1.57,7.33-4.1,9.86c-2.51,2.5-5.98,4.06-9.82,4.07v0.02h-0.02h-61.7H40.1v-0.02 c-3.84-0.01-7.34-1.57-9.86-4.1c-2.5-2.51-4.06-5.98-4.07-9.82h-0.02v-0.02V92.51H13.96h-0.01v-0.02c-3.84-0.01-7.34-1.57-9.86-4.1 c-2.5-2.51-4.06-5.98-4.07-9.82H0v-0.02V13.96v-0.01h0.02c0.01-3.85,1.58-7.34,4.1-9.86c2.51-2.5,5.98-4.06,9.82-4.07V0h0.02h61.7 h0.01v0.02c3.85,0.01,7.34,1.57,9.86,4.1c2.5,2.51,4.06,5.98,4.07,9.82h0.02V13.96L89.62,13.96z M79.04,21.69v-7.73v-0.02h0.02 c0-0.91-0.39-1.75-1.01-2.37c-0.61-0.61-1.46-1-2.37-1v0.02h-0.01h-61.7h-0.02v-0.02c-0.91,0-1.75,0.39-2.37,1.01 c-0.61,0.61-1,1.46-1,2.37h0.02v0.01v64.59v0.02h-0.02c0,0.91,0.39,1.75,1.01,2.37c0.61,0.61,1.46,1,2.37,1v-0.02h0.01h12.19V35.65 v-0.01h0.02c0.01-3.85,1.58-7.34,4.1-9.86c2.51-2.5,5.98-4.06,9.82-4.07v-0.02h0.02H79.04L79.04,21.69z M105.18,108.92V35.65v-0.02 h0.02c0-0.91-0.39-1.75-1.01-2.37c-0.61-0.61-1.46-1-2.37-1v0.02h-0.01h-61.7h-0.02v-0.02c-0.91,0-1.75,0.39-2.37,1.01 c-0.61,0.61-1,1.46-1,2.37h0.02v0.01v73.27v0.02h-0.02c0,0.91,0.39,1.75,1.01,2.37c0.61,0.61,1.46,1,2.37,1v-0.02h0.01h61.7h0.02 v0.02c0.91,0,1.75-0.39,2.37-1.01c0.61-0.61,1-1.46,1-2.37h-0.02V108.92L105.18,108.92z"/></g></svg>
<button class="type-icon" title="Copy CID" onclick=copyText({{ .Hash | jsonEncode }})>
<div class="ipfs-_copy">&nbsp;</div>
</button>
{{ end }}
</td>
Expand Down
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.

5 changes: 3 additions & 2 deletions assets/dir-index-html/src/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ button {
vertical-align: middle;
}

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

strong {
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)))
}
3 changes: 0 additions & 3 deletions core/corehttp/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,6 @@ func TestIPNSHostnameBacklinks(t *testing.T) {
if !strings.Contains(s, k2.Cid().String()) {
t.Fatalf("expected hash in directory listing")
}
if !strings.Contains(s, "<button class=\"type-icon\" title=\"Copy CID\" onclick=\"copyText(") {
t.Fatalf("expected button to copy CID in directory listing")
}

// make request to directory listing at root
req, err = http.NewRequest(http.MethodGet, ts.URL, nil)
Expand Down