func()

in go/wsl/upload/upload.go [37:94]


func (u *Uploader) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	if r.Method == http.MethodGet || r.Method == http.MethodDelete {
		errorResponse(w, http.StatusMethodNotAllowed, 9, "unknown method", "file deletion and fetching is not allowed")
		return
	}

	reqJSON := struct {
		File *string `json:"file"`
	}{}

	if err := json.NewDecoder(r.Body).Decode(&reqJSON); err != nil {
		errorResponse(w, http.StatusBadRequest, 13, "invalid argument", err.Error())
		return
	}

	if reqJSON.File == nil {
		errorResponse(w, http.StatusBadRequest, 13, "invalid argument", "file field is required in request")
		return
	}

	buffer := &bytes.Buffer{}

	if _, err := io.Copy(buffer, base64.NewDecoder(base64.StdEncoding, strings.NewReader(*reqJSON.File))); err != nil {
		errorResponse(w, http.StatusBadRequest, 13, "invalid argument", err.Error())
		return
	}

	dir, err := ioutil.TempDir(u.Root, "")
	if err != nil {
		errorResponse(w, http.StatusInternalServerError, 13, "unknown error", err.Error())
		return
	}

	files, err := unzipFiles(dir, buffer.Bytes())
	if err != nil {
		f, err := uploadFile(dir, buffer.Bytes())
		if err != nil {
			errorResponse(w, http.StatusInternalServerError, 13, "unknown error", err.Error())
			return
		}
		files = append(files, f)
	}

	respJSON := map[string]interface{}{
		"status": 0,
	}

	if len(files) == 1 {
		respJSON["value"] = files[0]
	} else {
		respJSON["value"] = files
	}

	w.Header().Set("Content-Type", "application/json; charset=utf-8")
	httphelper.SetDefaultResponseHeaders(w.Header())
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(respJSON)
}