Example #1
0
func (c *client) download(serverUrl, localPath, remotePath, dirFileFilterPattern string) (returnErr error) {
	defer CatchPanicAsError(&returnErr)

	var fileFilterQueryPart = ""
	if dirFileFilterPattern != "" {
		fileFilterQueryPart = "&filefilter=" + url.QueryEscape(dirFileFilterPattern)
	}

	resp, err := http.Get(serverUrl + "?path=" + url.QueryEscape(remotePath) + fileFilterQueryPart)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	if err = c.checkServerResponse(resp); err != nil {
		return err
	}

	ziputils.SaveTarReaderToPath(c.simpleLogger, resp.Body, localPath)
	return nil
}
Example #2
0
func (a *appContext) handler(w http.ResponseWriter, r *http.Request) {
	defer a.recoveryFunc(w, r, "ERROR in handler: %+v")

	if r.Method == "POST" {
		path, isDir := a.getFileOrFolderFromRequest(r)

		if isDir {
			a.logger.Info("Receiving directory (zipped) %s", path)
			ziputils.SaveTarReaderToPath(a.logger, r.Body, path)
		} else {
			a.logger.Info("Receiving file to %s", path)
			ziputils.SaveTarReaderToPath(a.logger, r.Body, path)
		}
	} else if r.Method == "GET" {
		path := a.getPathFromRequest(r)

		if a.isDir(path) {
			a.logger.Info("Sending directory %s", path)
			walkContext := ziputils.NewDirWalkContext(a.getDirFileFilterPatternFromRequest(r))
			ziputils.UploadDirectoryToHttpResponseWriter(a.logger, w, path, walkContext)
		} else {
			a.logger.Info("Sending file %s", path)
			ziputils.UploadFileToHttpResponseWriter(a.logger, w, path)
		}
	} else if r.Method == "DELETE" {
		path := a.getPathFromRequest(r)

		if a.isDir(path) {
			a.logger.Info("Deleting directory %s", path)
			walkContext := ziputils.NewDirWalkContext(a.getDirFileFilterPatternFromRequest(r))
			walkContext.DeleteDirectory(path)
		} else {
			a.logger.Info("Deleting file %s", path)
			err := os.Remove(path)
			CheckError(err)
		}
	} else if r.Method == "PUT" {
		action := a.getRequiredQueryValue(r, "action")
		switch strings.ToLower(action) {
		case "move":
			oldPath := a.getPathFromRequest(r)
			newPath := a.getRequiredQueryValue(r, "newpath")

			err := os.Rename(oldPath, newPath)
			CheckError(err)
			break
		default:
			panic("Unsupported action '" + action + "'")
		}
	} else if r.Method == "HEAD" {
		path := a.getPathFromRequest(r)

		a.logger.Info("Sending stats for path %s", path)

		info, err := os.Stat(path)
		if os.IsNotExist(err) {
			w.Header().Set("EXISTS", "0")
			return
		}
		CheckError(err)

		w.Header().Set("EXISTS", "1")

		if info.IsDir() {
			w.Header().Set("IS_DIR", "1")
		} else {
			w.Header().Set("IS_DIR", "0")
		}
	} else {
		panic("Unsupported method " + r.Method)
	}
}