本文整理匯總了Golang中github.com/flynn/flynn/blobstore/data.FileRepo.Put方法的典型用法代碼示例。如果您正苦於以下問題:Golang FileRepo.Put方法的具體用法?Golang FileRepo.Put怎麽用?Golang FileRepo.Put使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/flynn/flynn/blobstore/data.FileRepo
的用法示例。
在下文中一共展示了FileRepo.Put方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: testDelete
func testDelete(r *data.FileRepo, t *testing.T) {
put := func(path string) {
if err := r.Put(path, bytes.NewReader([]byte("data")), 0, "text/plain"); err != nil {
t.Fatal(err)
}
}
del := func(path string) {
if err := r.Delete(path); err != nil {
t.Fatal(err)
}
}
assertExists := func(path string) {
if _, err := r.Get(path, false); err != nil {
t.Fatal(err)
}
}
assertNotExists := func(path string) {
if _, err := r.Get(path, false); err != backend.ErrNotFound {
t.Fatalf("expected path %q to not exist, got err=%v", path, err)
}
}
put("/dir/foo")
put("/dir/foo.txt")
put("/dir/bar.txt")
del("/dir/foo")
assertNotExists("/dir/foo")
assertExists("/dir/foo.txt")
assertExists("/dir/bar.txt")
del("/dir")
assertNotExists("/dir/foo")
assertNotExists("/dir/foo.txt")
assertNotExists("/dir/bar.txt")
}
示例2: testExternalBackendReplace
func testExternalBackendReplace(r *data.FileRepo, t *testing.T) {
put := func(path string) {
if err := r.Put(path, bytes.NewReader([]byte("data")), 0, "text/plain"); err != nil {
t.Fatal(err)
}
}
get := func(path string) string {
file, err := r.Get("/replace.txt", true)
if err != nil {
t.Fatal(err)
}
redirect, ok := file.FileStream.(backend.Redirector)
if !ok {
t.Fatal("file stream is not a redirector")
}
url := redirect.RedirectURL()
res, err := http.Get(url)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode != 200 {
t.Fatalf("expected status 200, got %d", res.StatusCode)
}
return url
}
put("/replace.txt")
firstURL := get("/replace.txt")
put("/replace.txt")
res, err := http.Get(firstURL)
if err != nil {
t.Fatal(err)
}
res.Body.Close()
if res.StatusCode == 200 {
t.Fatal("unexpected status 200")
}
get("/replace.txt")
}
示例3: handler
func handler(r *data.FileRepo) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
path := path.Clean(req.URL.Path)
if path == "/" {
if req.Method == "HEAD" {
return
} else if req.Method != "GET" {
w.WriteHeader(404)
return
}
paths, err := r.List(req.URL.Query().Get("dir"))
if err != nil && err != backend.ErrNotFound {
errorResponse(w, err)
return
}
if paths == nil {
paths = []string{}
}
sort.Strings(paths)
w.WriteHeader(200)
json.NewEncoder(w).Encode(paths)
return
}
switch req.Method {
case "HEAD", "GET":
file, err := r.Get(path, req.Method == "GET")
if err != nil {
errorResponse(w, err)
return
}
if file.FileStream != nil {
defer file.Close()
}
if r, ok := file.FileStream.(backend.Redirector); ok && req.Method == "GET" {
http.Redirect(w, req, r.RedirectURL(), http.StatusFound)
return
}
w.Header().Set("Content-Length", strconv.FormatInt(file.Size, 10))
w.Header().Set("Content-Type", file.Type)
w.Header().Set("Etag", file.ETag)
http.ServeContent(w, req, path, file.ModTime, file)
case "PUT":
var err error
if src := req.Header.Get("Blobstore-Copy-From"); src != "" {
err = r.Copy(path, src)
} else {
var offset int64
if s := req.Header.Get("Blobstore-Offset"); s != "" {
offset, err = strconv.ParseInt(s, 10, 64)
if err != nil {
errorResponse(w, err)
return
}
}
err = r.Put(path, req.Body, offset, req.Header.Get("Content-Type"))
}
if err != nil {
errorResponse(w, err)
return
}
w.WriteHeader(200)
case "DELETE":
err := r.Delete(path)
if err != nil {
errorResponse(w, err)
return
}
w.WriteHeader(200)
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
})
}