示例#1
0
func (s *server) copy(ctx context.Context, w http.ResponseWriter, r *http.Request) {

	logger := MustFromLogContext(ctx)

	src := getPathFromReq(r)
	u, err := url.Parse(r.Header.Get("Destination"))
	if err != nil {
		logger.Error(err)
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	dst := path.Clean(strings.TrimPrefix(u.Path, remoteURL))
	if dst == "" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	logger.Infof("src is %s", src)
	logger.Infof("dst is %s", dst)

	con, err := getConnection(s.p.metaServer)
	if err != nil {
		logger.Error(err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	defer con.Close()

	client := metapb.NewMetaClient(con)

	in := &metapb.CpReq{}
	in.Src = src
	in.Dst = dst
	in.AccessToken = authlib.MustFromTokenContext(ctx)

	_, err = client.Cp(ctx, in)
	if err != nil {
		logger.Error(err)

		gErr := grpc.Code(err)
		switch {
		case gErr == codes.PermissionDenied:
			http.Error(w, "", http.StatusForbidden)
			return
		default:
			http.Error(w, "", http.StatusInternalServerError)
			return
		}
	}

	w.WriteHeader(http.StatusNoContent)
}
示例#2
0
func (s *server) mkcol(ctx context.Context, w http.ResponseWriter, r *http.Request) {

	logger := MustFromLogContext(ctx)

	p := getPathFromReq(r)

	logger.Infof("path is %s", p)

	con, err := getConnection(s.p.metaServer)
	if err != nil {
		logger.Error(err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	defer con.Close()

	client := metapb.NewMetaClient(con)

	in := &metapb.MkdirReq{}
	in.AccessToken = authlib.MustFromTokenContext(ctx)
	in.Path = p

	_, err = client.Mkdir(ctx, in)
	if err != nil {
		logger.Error(err)

		gErr := grpc.Code(err)
		switch {
		case gErr == codes.PermissionDenied:
			http.Error(w, "", http.StatusForbidden)
			return
		default:
			http.Error(w, "", http.StatusInternalServerError)
			return
		}
	}

	w.WriteHeader(http.StatusCreated)
}
示例#3
0
func getMeta(ctx context.Context, addr, p string, children bool) (*metapb.Metadata, error) {

	in := &metapb.StatReq{}
	in.AccessToken = authlib.MustFromTokenContext(ctx)
	in.Children = children
	in.Path = p

	con, err := getConnection(addr)
	if err != nil {
		return nil, err
	}

	defer con.Close()

	client := metapb.NewMetaClient(con)

	meta, err := client.Stat(ctx, in)
	if err != nil {
		return nil, err
	}

	return meta, nil
}
示例#4
0
func (s *server) move(ctx context.Context, w http.ResponseWriter, r *http.Request) {

	logger := MustFromLogContext(ctx)

	src := getPathFromReq(r)
	u, err := url.Parse(r.Header.Get("Destination"))
	if err != nil {
		logger.Error(err)
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	dst := path.Clean(strings.TrimPrefix(u.Path, remoteURL))
	if dst == "" {
		http.Error(w, "", http.StatusBadRequest)
		return
	}

	logger.Infof("src is %s", src)
	logger.Infof("dst is %s", dst)

	con, err := getConnection(s.p.metaServer)
	if err != nil {
		logger.Error(err)
		http.Error(w, "", http.StatusInternalServerError)
		return
	}

	defer con.Close()

	client := metapb.NewMetaClient(con)

	in := &metapb.MvReq{}
	in.Src = src
	in.Dst = dst
	in.AccessToken = authlib.MustFromTokenContext(ctx)

	_, err = client.Mv(ctx, in)
	if err != nil {
		logger.Error(err)

		gErr := grpc.Code(err)
		switch {
		case gErr == codes.PermissionDenied:
			http.Error(w, "", http.StatusForbidden)
			return
		default:
			http.Error(w, "", http.StatusInternalServerError)
			return
		}
	}

	meta, err := getMeta(ctx, s.p.metaServer, dst, false)
	if err != nil {
		logger.Error(err)

		gErr := grpc.Code(err)
		switch {
		case gErr == codes.NotFound:
			http.Error(w, "", http.StatusNotFound)
			return
		case gErr == codes.PermissionDenied:
			http.Error(w, "", http.StatusForbidden)
			return
		default:
			http.Error(w, "", http.StatusInternalServerError)
			return
		}
	}

	w.Header().Set("ETag", meta.Etag)
	w.Header().Set("OC-FileId", meta.Id)
	w.Header().Set("OC-ETag", meta.Etag)
	t := time.Unix(int64(meta.Modified), 0)
	lastModifiedString := t.Format(time.RFC1123)
	w.Header().Set("Last-Modified", lastModifiedString)

	// TODO(labkode) is resource existed (overwrite the code should be 204)
	w.WriteHeader(http.StatusCreated)
}