예제 #1
0
func (h *handler) handleUploadTarFunc(c echo.Context) error {
	req := c.Request()
	basePath := req.Header().Get(shared.BASE_PATH_HTTP_HEADER_NAME)
	if strings.TrimSpace(basePath) == "" {
		return fmt.Errorf("Request does not contain header '%s'", shared.BASE_PATH_HTTP_HEADER_NAME)
	}
	isDirVal := req.Header().Get(shared.IS_DIR_HTTP_HEADER_NAME)
	if strings.TrimSpace(isDirVal) == "" {
		return fmt.Errorf("Request does not contain header '%s'", shared.IS_DIR_HTTP_HEADER_NAME)
	}
	isDir := strings.TrimSpace(isDirVal) == "1"

	if isDir {
		//TODO: Is permission fine?
		if err := os.MkdirAll(basePath, 0755); err != nil {
			return fmt.Errorf("Unable to create base path %s, error: %s", basePath, err.Error())
		}
	}

	if isDir {
		h.logger.Infof("Now starting to receive dir '%s'", basePath)
		tarReceiver := tar_io.Factories.TarReceiver.Dir(basePath)
		return tar_io.SaveTarToReceiver(req.Body(), tarReceiver)
	} else {
		h.logger.Infof("Now starting to receive file '%s'", basePath)
		tarReceiver := tar_io.Factories.TarReceiver.File(basePath)
		return tar_io.SaveTarToReceiver(req.Body(), tarReceiver)
	}
}
예제 #2
0
func (s *session) DownloadTarStream(queryValues url.Values, tarReceiver tar_io.TarReceiver) error {
	relUrl := "/auth/download-tar?" + queryValues.Encode()
	url := s.GetFullServerUrl(relUrl)

	req, err := s.newNativeHttpRequest("GET", url, nil)
	if err != nil {
		return fmt.Errorf("Unable to create http request, error: %s", err.Error())
	}

	resp, err := s.newHttpClient().Do(req)
	if err != nil {
		return fmt.Errorf("Unable to make GET request, error: %s", err.Error())
	}
	defer resp.Body.Close()

	err = checkResponse(resp)
	if err != nil {
		return fmt.Errorf("Response error in downloading tar stream, error: %s", err.Error())
	}

	err = tar_io.SaveTarToReceiver(resp.Body, tarReceiver)
	if err != nil {
		return fmt.Errorf("Unable to save response body as TAR, error: %s", err.Error())
	}

	return nil
}