attachments now format absolute url

master
breel 2020-07-31 23:09:29 -06:00
parent e706548f55
commit a056b045a9
3 changed files with 42 additions and 1 deletions

@ -1 +1 @@
Subproject commit 0b553984035d46f1b150d600303001b4164f3124 Subproject commit c5da36f592820312e10d05ff27c32d0ef644f282

View File

@ -7,10 +7,13 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"local/dndex/config"
"local/dndex/storage" "local/dndex/storage"
"local/dndex/storage/entity" "local/dndex/storage/entity"
"local/dndex/storage/operator" "local/dndex/storage/operator"
"net/http" "net/http"
"net/url"
"path"
"regexp" "regexp"
"sort" "sort"
"strings" "strings"
@ -75,6 +78,18 @@ func whoGet(namespace string, g storage.RateLimitedGraph, w http.ResponseWriter,
} }
one := ones[0] one := ones[0]
baseUrl := *r.URL
if baseUrl.Host == "" {
baseUrl.Host = r.Host
}
baseUrl.Path = ""
baseUrl.RawQuery = ""
for k := range one.Attachments {
if _, err := url.Parse(one.Attachments[k]); err != nil || !strings.Contains(one.Attachments[k], ":") {
one.Attachments[k] = path.Join(baseUrl.String(), config.New().FilePrefix, namespace, one.Attachments[k])
}
}
if !light && len(one.Connections) > 0 { if !light && len(one.Connections) > 0 {
ones, err := g.ListCaseInsensitive(r.Context(), namespace, one.Peers()...) ones, err := g.ListCaseInsensitive(r.Context(), namespace, one.Peers()...)
if err != nil { if err != nil {

View File

@ -14,6 +14,7 @@ import (
"net/http/httptest" "net/http/httptest"
"net/url" "net/url"
"os" "os"
"path"
"regexp" "regexp"
"sort" "sort"
"strings" "strings"
@ -95,6 +96,21 @@ func TestWho(t *testing.T) {
if len(o.Connections) != len(iwant.Connections) { if len(o.Connections) != len(iwant.Connections) {
t.Fatal(len(o.Connections), len(iwant.Connections)) t.Fatal(len(o.Connections), len(iwant.Connections))
} }
if len(o.Attachments) != len(iwant.Attachments) {
t.Fatal(len(o.Attachments), len(iwant.Attachments))
}
for k := range o.Attachments {
if _, ok := iwant.Attachments[k]; !ok {
t.Fatal(k, ok)
}
if o.Attachments[k] == iwant.Attachments[k] {
t.Fatal(k, o.Attachments[k], iwant.Attachments[k])
}
if !strings.HasSuffix(o.Attachments[k], path.Join(config.New().FilePrefix, "col", iwant.Attachments[k])) {
t.Fatal(k, o.Attachments[k], iwant.Attachments[k])
}
}
iwant.Attachments = o.Attachments
iwant.Connections = o.Connections iwant.Connections = o.Connections
iwant.Modified = 0 iwant.Modified = 0
o.Modified = 0 o.Modified = 0
@ -125,6 +141,7 @@ func TestWho(t *testing.T) {
t.Fatal(len(o.Connections), len(iwant.Connections)) t.Fatal(len(o.Connections), len(iwant.Connections))
} }
iwant.Connections = o.Connections iwant.Connections = o.Connections
iwant.Attachments = o.Attachments
iwant.Modified = 0 iwant.Modified = 0
o.Modified = 0 o.Modified = 0
if fmt.Sprint(o) != fmt.Sprint(iwant) { if fmt.Sprint(o) != fmt.Sprint(iwant) {
@ -162,6 +179,7 @@ func TestWho(t *testing.T) {
t.Fatal(len(o.Connections), len(iwant.Connections)) t.Fatal(len(o.Connections), len(iwant.Connections))
} }
iwant.Connections = o.Connections iwant.Connections = o.Connections
iwant.Attachments = o.Attachments
iwant.Modified = 0 iwant.Modified = 0
o.Modified = 0 o.Modified = 0
if fmt.Sprint(o) != fmt.Sprint(iwant) { if fmt.Sprint(o) != fmt.Sprint(iwant) {
@ -197,6 +215,7 @@ func TestWho(t *testing.T) {
t.Fatal(len(o.Connections), len(iwant.Connections)) t.Fatal(len(o.Connections), len(iwant.Connections))
} }
iwant.Connections = o.Connections iwant.Connections = o.Connections
iwant.Attachments = o.Attachments
iwant.Modified = 0 iwant.Modified = 0
o.Modified = 0 o.Modified = 0
if fmt.Sprint(o) != fmt.Sprint(iwant) { if fmt.Sprint(o) != fmt.Sprint(iwant) {
@ -528,6 +547,8 @@ func TestWho(t *testing.T) {
} }
got.Connections = nil got.Connections = nil
from.Connections = nil from.Connections = nil
got.Attachments = nil
from.Attachments = nil
if fmt.Sprint(got) != fmt.Sprint(from) { if fmt.Sprint(got) != fmt.Sprint(from) {
t.Fatalf("without connections and modified, got != want: want \n %+v, got \n %+v", from, got) t.Fatalf("without connections and modified, got != want: want \n %+v, got \n %+v", from, got)
} }
@ -569,6 +590,7 @@ func TestWho(t *testing.T) {
t.Fatal(len(got.Connections), len(want.Connections)+1) t.Fatal(len(got.Connections), len(want.Connections)+1)
} }
got.Connections = want.Connections got.Connections = want.Connections
got.Attachments = want.Attachments
got.Modified = 0 got.Modified = 0
want.Modified = 0 want.Modified = 0
if fmt.Sprint(got) != fmt.Sprint(want) { if fmt.Sprint(got) != fmt.Sprint(want) {
@ -753,6 +775,7 @@ func TestWho(t *testing.T) {
b.Modified = 0 b.Modified = 0
o.Connections[k] = b o.Connections[k] = b
} }
o.Attachments = want.Attachments
if fmt.Sprint(o) != fmt.Sprint(want) { if fmt.Sprint(o) != fmt.Sprint(want) {
t.Fatalf("GET put != expected: want:\n%+v, got \n%+v", want, o) t.Fatalf("GET put != expected: want:\n%+v, got \n%+v", want, o)
} }
@ -822,6 +845,9 @@ func randomOne() entity.One {
Text: "text-" + uuid.New().String()[:5], Text: "text-" + uuid.New().String()[:5],
Modified: time.Now().UnixNano(), Modified: time.Now().UnixNano(),
Connections: map[string]entity.One{}, Connections: map[string]entity.One{},
Attachments: map[string]string{
uuid.New().String()[:5]: uuid.New().String()[:5],
},
} }
} }