Files to uuids only
parent
c85465accf
commit
bb9b91eef5
|
|
@ -0,0 +1,17 @@
|
||||||
|
paths:
|
||||||
|
summary: "Create files"
|
||||||
|
post:
|
||||||
|
tags:
|
||||||
|
- files
|
||||||
|
parameters:
|
||||||
|
- $ref: "#/components/parameters/token"
|
||||||
|
requestBody:
|
||||||
|
$ref: "#/components/schemas/requestForm"
|
||||||
|
|
||||||
|
components:
|
||||||
|
parameters:
|
||||||
|
token:
|
||||||
|
$ref: "../swagger.yaml#/components/parameters/token"
|
||||||
|
schemas:
|
||||||
|
requestForm:
|
||||||
|
$ref: "../swagger.yaml#/components/schemas/requestForm"
|
||||||
|
|
@ -6,14 +6,6 @@ paths:
|
||||||
parameters:
|
parameters:
|
||||||
- $ref: "#/components/parameters/token"
|
- $ref: "#/components/parameters/token"
|
||||||
- $ref: "#/components/parameters/path"
|
- $ref: "#/components/parameters/path"
|
||||||
post:
|
|
||||||
tags:
|
|
||||||
- files
|
|
||||||
parameters:
|
|
||||||
- $ref: "#/components/parameters/token"
|
|
||||||
- $ref: "#/components/parameters/path"
|
|
||||||
requestBody:
|
|
||||||
$ref: "#/components/schemas/requestForm"
|
|
||||||
put:
|
put:
|
||||||
tags:
|
tags:
|
||||||
- files
|
- files
|
||||||
|
|
@ -32,9 +24,9 @@ paths:
|
||||||
components:
|
components:
|
||||||
parameters:
|
parameters:
|
||||||
token:
|
token:
|
||||||
$ref: "./swagger.yaml#/components/parameters/token"
|
$ref: "../swagger.yaml#/components/parameters/token"
|
||||||
path:
|
path:
|
||||||
$ref: "./swagger.yaml#/components/parameters/path"
|
$ref: "../swagger.yaml#/components/parameters/path"
|
||||||
schemas:
|
schemas:
|
||||||
requestForm:
|
requestForm:
|
||||||
$ref: "./swagger.yaml#/components/schemas/requestForm"
|
$ref: "../swagger.yaml#/components/schemas/requestForm"
|
||||||
|
|
@ -17,8 +17,10 @@ servers:
|
||||||
paths:
|
paths:
|
||||||
/version:
|
/version:
|
||||||
$ref: "./version.yaml#/paths"
|
$ref: "./version.yaml#/paths"
|
||||||
|
/files:
|
||||||
|
$ref: "./files/index.yaml#/paths"
|
||||||
/files/{path}:
|
/files/{path}:
|
||||||
$ref: "./files.yaml#/paths"
|
$ref: "./files/one.yaml#/paths"
|
||||||
/users/register:
|
/users/register:
|
||||||
$ref: "./users/register.yaml#/paths"
|
$ref: "./users/register.yaml#/paths"
|
||||||
/users/login:
|
/users/login:
|
||||||
|
|
|
||||||
|
|
@ -6,28 +6,35 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (rest *REST) files(w http.ResponseWriter, r *http.Request) {
|
func (rest *REST) files(w http.ResponseWriter, r *http.Request) {
|
||||||
if len(r.URL.Path) < 2 {
|
if len(r.URL.Path) < 2 {
|
||||||
rest.respNotFound(w)
|
switch r.Method {
|
||||||
return
|
case http.MethodPost:
|
||||||
}
|
rest.filesCreate(w, r)
|
||||||
switch r.Method {
|
default:
|
||||||
case http.MethodPut:
|
rest.respNotFound(w)
|
||||||
rest.filesUpdate(w, r)
|
}
|
||||||
case http.MethodPost:
|
} else {
|
||||||
rest.filesCreate(w, r)
|
switch r.Method {
|
||||||
case http.MethodGet:
|
case http.MethodPut:
|
||||||
rest.filesGet(w, r)
|
rest.filesUpdate(w, r)
|
||||||
case http.MethodDelete:
|
case http.MethodGet:
|
||||||
rest.filesDelete(w, r)
|
rest.filesGet(w, r)
|
||||||
default:
|
case http.MethodDelete:
|
||||||
rest.respNotFound(w)
|
rest.filesDelete(w, r)
|
||||||
|
default:
|
||||||
|
rest.respNotFound(w)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rest *REST) filesCreate(w http.ResponseWriter, r *http.Request) {
|
func (rest *REST) filesCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
|
id := uuid.New().String()
|
||||||
|
r.URL.Path = "/" + id
|
||||||
localPath := rest.filesPath(r)
|
localPath := rest.filesPath(r)
|
||||||
if stat, err := os.Stat(localPath); !os.IsNotExist(err) || (stat != nil && stat.IsDir()) {
|
if stat, err := os.Stat(localPath); !os.IsNotExist(err) || (stat != nil && stat.IsDir()) {
|
||||||
rest.respConflict(w)
|
rest.respConflict(w)
|
||||||
|
|
@ -46,7 +53,7 @@ func (rest *REST) filesCreate(w http.ResponseWriter, r *http.Request) {
|
||||||
if _, err := io.Copy(f, r.Body); err != nil {
|
if _, err := io.Copy(f, r.Body); err != nil {
|
||||||
rest.respError(w, err)
|
rest.respError(w, err)
|
||||||
}
|
}
|
||||||
rest.respOK(w)
|
w.Write([]byte(id))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rest *REST) filesDelete(w http.ResponseWriter, r *http.Request) {
|
func (rest *REST) filesDelete(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
|
|
@ -12,38 +12,23 @@ import (
|
||||||
func TestFiles(t *testing.T) {
|
func TestFiles(t *testing.T) {
|
||||||
cases := map[string]func(*testing.T, *REST, func(*http.Request)){
|
cases := map[string]func(*testing.T, *REST, func(*http.Request)){
|
||||||
"create-get": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
"create-get": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
||||||
s := uuid.New().String()
|
content := uuid.New().String()
|
||||||
w := testFilesPost(t, rest, s, scope, s)
|
w := testFilesPost(t, rest, scope, content)
|
||||||
if w.Code != http.StatusOK {
|
if w.Code != http.StatusOK {
|
||||||
t.Fatal(w.Code, string(w.Body.Bytes()))
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
||||||
}
|
}
|
||||||
|
s := string(w.Body.Bytes())
|
||||||
w = testFilesGet(t, rest, s, scope)
|
w = testFilesGet(t, rest, s, scope)
|
||||||
if w.Code != http.StatusOK {
|
if w.Code != http.StatusOK {
|
||||||
t.Fatal(w.Code, string(w.Body.Bytes()))
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
||||||
}
|
}
|
||||||
if s2 := string(w.Body.Bytes()); s2 != s {
|
if s2 := string(w.Body.Bytes()); s2 != content {
|
||||||
t.Fatalf("want %q, got %q", s, s2)
|
t.Fatalf("want %q, got %q", content, s2)
|
||||||
}
|
|
||||||
},
|
|
||||||
"create-collision": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
|
||||||
s := uuid.New().String()
|
|
||||||
for i := 0; i < 2; i++ {
|
|
||||||
w := testFilesPost(t, rest, s, scope, s)
|
|
||||||
ok := false
|
|
||||||
switch i {
|
|
||||||
case 0:
|
|
||||||
ok = w.Code == http.StatusOK
|
|
||||||
default:
|
|
||||||
ok = w.Code == http.StatusConflict
|
|
||||||
}
|
|
||||||
if !ok {
|
|
||||||
t.Fatal(w.Code, string(w.Body.Bytes()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"delete": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
"delete": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
||||||
s := uuid.New().String()
|
w := testFilesPost(t, rest, scope, uuid.New().String())
|
||||||
w := testFilesPost(t, rest, s, scope, s)
|
s := string(w.Body.Bytes())
|
||||||
if w.Code != http.StatusOK {
|
if w.Code != http.StatusOK {
|
||||||
t.Fatal(w.Code, string(w.Body.Bytes()))
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
||||||
}
|
}
|
||||||
|
|
@ -64,8 +49,8 @@ func TestFiles(t *testing.T) {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"update": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
"update": func(t *testing.T, rest *REST, scope func(r *http.Request)) {
|
||||||
s := uuid.New().String()
|
w := testFilesPost(t, rest, scope, uuid.New().String())
|
||||||
w := testFilesPost(t, rest, s, scope, s)
|
s := string(w.Body.Bytes())
|
||||||
if w.Code != http.StatusOK {
|
if w.Code != http.StatusOK {
|
||||||
t.Fatal(w.Code, string(w.Body.Bytes()))
|
t.Fatal(w.Code, string(w.Body.Bytes()))
|
||||||
}
|
}
|
||||||
|
|
@ -105,8 +90,8 @@ func testFilesPut(t *testing.T, rest *REST, id string, scope func(*http.Request)
|
||||||
return testFilesReq(t, rest, id, scope, http.MethodPut, body)
|
return testFilesReq(t, rest, id, scope, http.MethodPut, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testFilesPost(t *testing.T, rest *REST, id string, scope func(*http.Request), body string) *httptest.ResponseRecorder {
|
func testFilesPost(t *testing.T, rest *REST, scope func(*http.Request), body string) *httptest.ResponseRecorder {
|
||||||
return testFilesReq(t, rest, id, scope, http.MethodPost, body)
|
return testFilesReq(t, rest, "", scope, http.MethodPost, body)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testFilesReq(t *testing.T, rest *REST, id string, scope func(*http.Request), method, body string) *httptest.ResponseRecorder {
|
func testFilesReq(t *testing.T, rest *REST, id string, scope func(*http.Request), method, body string) *httptest.ResponseRecorder {
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,6 @@ package server
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
|
@ -124,7 +123,7 @@ func TestMiddlewareShift(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
rest := &REST{}
|
rest := &REST{}
|
||||||
foo := rest.shift(func(http.ResponseWriter, *http.Request) {})
|
foo := rest.shift(func(http.ResponseWriter, *http.Request) {})
|
||||||
log.Printf("%+v", r.URL)
|
t.Logf("%+v", r.URL)
|
||||||
foo(w, r)
|
foo(w, r)
|
||||||
if r.URL.String() != c.output {
|
if r.URL.String() != c.output {
|
||||||
t.Fatalf("from %q, want %q, got %q", c.input, c.output, r.URL.String())
|
t.Fatalf("from %q, want %q, got %q", c.input, c.output, r.URL.String())
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ func TestRESTRouter(t *testing.T) {
|
||||||
fmt.Sprintf(`%s/%s?pu`, config.New().FilePrefix, testFilename): {
|
fmt.Sprintf(`%s/%s?pu`, config.New().FilePrefix, testFilename): {
|
||||||
method: http.MethodPut,
|
method: http.MethodPut,
|
||||||
},
|
},
|
||||||
fmt.Sprintf(`%s/%s?po`, config.New().FilePrefix, testFilename): {
|
fmt.Sprintf(`%s?po`, config.New().FilePrefix): {
|
||||||
method: http.MethodPost,
|
method: http.MethodPost,
|
||||||
},
|
},
|
||||||
fmt.Sprintf(`%s/%s?d`, config.New().FilePrefix, testFilename): {
|
fmt.Sprintf(`%s/%s?d`, config.New().FilePrefix, testFilename): {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue