Example #1
0
func (h httpHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
	res.Header().Set("Access-Control-Allow-Origin", "*")
	res.Header().Set("Cashe-Control", "no-store")
	res.Header().Set("Connection", "keep-alive")
	res.Header().Set("Expires", "Sat, 01 Jan 2000 00:00:00 GMT")
	res.Header().Set("Pragma", "no-cache")

	path := strings.TrimPrefix(req.URL.Path, h.URLPrefix)
	path = strings.TrimRight(path, "/")

	if len(path) == 0 {
		path = "/"
	}

	contentType := ""
	var body []byte = nil

	if req.Method == "POST" || req.Method == "PUT" || req.Method == "PATCH" {
		rContentType, exists := req.Header["Content-Type"]
		if exists {
			if len(rContentType) != 1 {
				replyError(res, 400, "invalid mutiple content-type")
				return
			}

			contentType = rContentType[0]
		}

		var err error
		body, err = ioutil.ReadAll(req.Body)
		if err != nil {
			replyError(res, 500, "unable to read request body")
			return
		}
	}

	user := oauth.GetUserOrFail(res, req)
	if user == nil {
		return
	}

	response, err := h.Module.Request(
		req.Method,
		path+"?"+req.URL.RawQuery,
		contentType,
		body,
		user.(*nano.User),
	)

	if err != nil {
		module.Log.Error(err)
		res.WriteHeader(500)
		return
	}

	res.Header().Set("Content-Type", response.ContentType)
	res.WriteHeader(response.StatusCode)
	res.Write(response.Body)
}
Example #2
0
// uploadHandler tries to get and save a chunk.
func uploadHandler(w http.ResponseWriter, r *http.Request) {
	user := oauth.GetUserOrFail(w, r)
	if user == nil {
		http.Error(w, "", http.StatusUnauthorized)
		return
	}
	userPath := filepath.Join(conf.UploadDir, user.(*nano.User).Id)

	// get the multipart data
	err := r.ParseMultipartForm(2 * 1024 * 1024) // chunkSize
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	chunkNum := r.FormValue("flowChunkNumber")
	totalChunks := r.FormValue("flowTotalChunks")
	filename := r.FormValue("flowFilename")
	// module := r.FormValue("module")

	err = writeChunk(filepath.Join(userPath, "incomplete", filename), chunkNum, r)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	// it's done if it's not the last chunk
	if chunkNum < totalChunks {
		return
	}

	upPath := filepath.Join(userPath, filename)

	// now finish the job
	err = assembleUpload(userPath, filename)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		module.Log.WithFields(log.Fields{
			"error": err,
		}).Error("unable to assemble the uploaded chunks")
		return
	}
	module.Log.WithFields(log.Fields{
		"path": upPath,
	}).Info("file uploaded")

	syncOut, err := syncUploadedFile(upPath)
	if err != nil {
		module.Log.WithFields(log.Fields{
			"output": syncOut,
			"error":  err,
		}).Error("unable to scp the uploaded file to Windows")
	}
	module.Log.WithFields(log.Fields{
		"path":   upPath,
		"output": syncOut,
	}).Info("file synced")
}
Example #3
0
func getMeHandler(w http.ResponseWriter, r *http.Request) {
	user := oauth.GetUserOrFail(w, r)
	if user != nil {
		b, err := json.Marshal(user)
		if err != nil {
			module.Log.Error(err)
			w.WriteHeader(500)
		}
		w.Header().Set("Content-Type", "application/json")
		w.Write(b)
	}
}
Example #4
0
// checkUploadHandler checks a chunk.
// If it doesn't exist then flowjs tries to upload it via uploadHandler.
func checkUploadHandler(w http.ResponseWriter, r *http.Request) {
	user := oauth.GetUserOrFail(w, r)
	if user == nil {
		http.Error(w, "", http.StatusUnauthorized)
		return
	}

	chunkPath := filepath.Join(conf.UploadDir, user.(*nano.User).Id, "incomplete", r.FormValue("flowFilename"), r.FormValue("flowChunkNumber"))
	if _, err := os.Stat(chunkPath); err != nil {
		http.Error(w, "chunk not found", http.StatusSeeOther)
		return
	}
}