Ensure cleaning up
parent
09b2a94dd8
commit
f90ccaae38
|
|
@ -39,6 +39,7 @@ func New() Config {
|
||||||
as.Append(args.INT, "max-file-size", "max file size for uploads in bytes", 50*(1<<20))
|
as.Append(args.INT, "max-file-size", "max file size for uploads in bytes", 50*(1<<20))
|
||||||
|
|
||||||
if err := as.Parse(); err != nil {
|
if err := as.Parse(); err != nil {
|
||||||
|
os.Remove(f.Name())
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ paths:
|
||||||
summary: "Fetch a file"
|
summary: "Fetch a file"
|
||||||
parameters:
|
parameters:
|
||||||
- $ref: "#/components/parameters/path"
|
- $ref: "#/components/parameters/path"
|
||||||
|
- $ref: "#/components/parameters/namespace"
|
||||||
responses:
|
responses:
|
||||||
200:
|
200:
|
||||||
content:
|
content:
|
||||||
|
|
@ -18,6 +19,7 @@ paths:
|
||||||
summary: "Provide or direct link to a file"
|
summary: "Provide or direct link to a file"
|
||||||
parameters:
|
parameters:
|
||||||
- $ref: "#/components/parameters/path"
|
- $ref: "#/components/parameters/path"
|
||||||
|
- $ref: "#/components/parameters/namespace"
|
||||||
- $ref: "#/components/parameters/direct"
|
- $ref: "#/components/parameters/direct"
|
||||||
requestBody:
|
requestBody:
|
||||||
description: "If ?direct=true, then a direct link to the file, else a multi-part form"
|
description: "If ?direct=true, then a direct link to the file, else a multi-part form"
|
||||||
|
|
@ -39,6 +41,9 @@ components:
|
||||||
path:
|
path:
|
||||||
$ref: "./swagger.yaml#/components/parameters/path"
|
$ref: "./swagger.yaml#/components/parameters/path"
|
||||||
|
|
||||||
|
namespace:
|
||||||
|
$ref: "./swagger.yaml#/components/parameters/namespace"
|
||||||
|
|
||||||
direct:
|
direct:
|
||||||
name: direct
|
name: direct
|
||||||
in: query
|
in: query
|
||||||
|
|
|
||||||
|
|
@ -22,11 +22,12 @@ func files(_ storage.Graph, w http.ResponseWriter, r *http.Request) error {
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, path.Join(config.New().FilePrefix, namespace))
|
r.URL.Path = strings.TrimPrefix(r.URL.Path, config.New().FilePrefix)
|
||||||
if len(r.URL.Path) < 2 {
|
if len(r.URL.Path) < 2 {
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
r.URL.Path = path.Join(namespace, r.URL.Path)
|
||||||
simpleserve.SetContentTypeIfMedia(w, r)
|
simpleserve.SetContentTypeIfMedia(w, r)
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodGet:
|
||||||
|
|
|
||||||
|
|
@ -30,12 +30,15 @@ func TestFiles(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
defer os.RemoveAll(d)
|
defer os.RemoveAll(d)
|
||||||
|
if err := os.MkdirAll(path.Join(d, "col"), os.ModePerm); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
os.Setenv("FILEROOT", d)
|
os.Setenv("FILEROOT", d)
|
||||||
|
|
||||||
t.Logf("config: %+v", config.New())
|
t.Logf("config: %+v", config.New())
|
||||||
handler := jsonHandler(storage.Graph{})
|
handler := jsonHandler(storage.Graph{})
|
||||||
|
|
||||||
prefix := path.Join(config.New().FilePrefix, "col")
|
prefix := path.Join(config.New().FilePrefix)
|
||||||
qparam := "namespace=col"
|
qparam := "namespace=col"
|
||||||
|
|
||||||
t.Run("bad qparam doesnt have namespace", func(t *testing.T) {
|
t.Run("bad qparam doesnt have namespace", func(t *testing.T) {
|
||||||
|
|
@ -47,15 +50,6 @@ func TestFiles(t *testing.T) {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("bad prefix doesnt have namespace", func(t *testing.T) {
|
|
||||||
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/fake?%s", config.New().FilePrefix, qparam), nil)
|
|
||||||
w := httptest.NewRecorder()
|
|
||||||
handler.ServeHTTP(w, r)
|
|
||||||
if w.Code != http.StatusNotFound {
|
|
||||||
t.Fatalf("%d: %s", w.Code, w.Body.Bytes())
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
t.Run("get fake file 404", func(t *testing.T) {
|
t.Run("get fake file 404", func(t *testing.T) {
|
||||||
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/fake?%s", prefix, qparam), nil)
|
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/fake?%s", prefix, qparam), nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
@ -75,13 +69,14 @@ func TestFiles(t *testing.T) {
|
||||||
}
|
}
|
||||||
for ext, ct := range cases {
|
for ext, ct := range cases {
|
||||||
for _, extC := range []string{strings.ToLower(ext), strings.ToUpper(ext)} {
|
for _, extC := range []string{strings.ToLower(ext), strings.ToUpper(ext)} {
|
||||||
f, err := ioutil.TempFile(d, "*."+extC)
|
f, err := ioutil.TempFile(path.Join(d, "col"), "*."+extC)
|
||||||
t.Logf("tempFile(%q, *.%s) = %q", d, extC, f.Name())
|
t.Logf("tempFile(%q/col, *.%s) = %q", d, extC, f.Name())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
f.Write([]byte("hello, world"))
|
f.Write([]byte("hello, world"))
|
||||||
f.Close()
|
f.Close()
|
||||||
|
defer os.Remove(f.Name())
|
||||||
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s?direct=true&%s", path.Join(prefix, path.Base(f.Name())), qparam), nil)
|
r := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s?direct=true&%s", path.Join(prefix, path.Base(f.Name())), qparam), nil)
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
t.Logf("URL = %q", r.URL.String())
|
t.Logf("URL = %q", r.URL.String())
|
||||||
|
|
@ -106,6 +101,7 @@ func TestFiles(t *testing.T) {
|
||||||
}
|
}
|
||||||
f.Write([]byte("hello, world"))
|
f.Write([]byte("hello, world"))
|
||||||
f.Close()
|
f.Close()
|
||||||
|
defer os.Remove(f.Name())
|
||||||
name := path.Base(f.Name())
|
name := path.Base(f.Name())
|
||||||
|
|
||||||
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
@ -138,6 +134,7 @@ func TestFiles(t *testing.T) {
|
||||||
}
|
}
|
||||||
f.Write([]byte("hello, world"))
|
f.Write([]byte("hello, world"))
|
||||||
f.Close()
|
f.Close()
|
||||||
|
defer os.Remove(f.Name())
|
||||||
|
|
||||||
r := httptest.NewRequest(http.MethodPost, path.Join(prefix, path.Base(f.Name()))+"?"+qparam, strings.NewReader(`bad link teehee`))
|
r := httptest.NewRequest(http.MethodPost, path.Join(prefix, path.Base(f.Name()))+"?"+qparam, strings.NewReader(`bad link teehee`))
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
|
|
@ -153,6 +150,7 @@ func TestFiles(t *testing.T) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
f.Close()
|
f.Close()
|
||||||
|
defer os.Remove(f.Name())
|
||||||
name := path.Base(f.Name())
|
name := path.Base(f.Name())
|
||||||
b := bytes.NewBuffer(nil)
|
b := bytes.NewBuffer(nil)
|
||||||
writer := multipart.NewWriter(b)
|
writer := multipart.NewWriter(b)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue