Beispiel #1
0
func (s ConsoleServer) Start() {
	// here is where I would specify the console interface if I actually planned on using the
	// from the command line.  For now it will just expect a command to look like `./calhoun
	// -ui=cli upload file=/path/to/file`
	url := s.Args[0]
	filepath := strings.SplitAfter(s.Args[1], "=")[1] // file=/path/to/file, so I need everything after the =

	var file multipart.File

	switch url {
	case "upload":
		file, err := os.Open(filepath)
		defer file.Close()

		if err != nil {
			log.Fatal("error reading photo upload: ", err)
		}
	default:
		log.Fatal("invalid command: `", url, "`")
	}

	route := s.routeWithPath(url)
	baseHandler := s.App.LookupHandler(route.Action)
	calhounHandler := route.ApplyMiddlewareToBase(baseHandler)
	request := CalhounRequest{UploadFile: &file}

	calhounHandler(os.Stdout, &request)
}
Beispiel #2
0
//Execute the file save process and return the result
func (f *filerSaver) Exec() (path string, status int, err error) {
	// bug  if the path's stat is not ok, try make this dir.
	if _, err := os.Stat(f.path); err != nil {
		if err1 := os.MkdirAll(f.path, 0755); err1 != nil {
			//make dir fail,return status and err
			status = SAVEFILE_CREATE_DIR_ERROR
			return "", status, err1
		}
	}
	var file multipart.File
	file, f.header, err = f.request.FormFile(f.key)
	if file != nil {
		defer file.Close()
	}
	if err == nil {
		if fname, err := f.nameSetter(f.header.Filename); err == nil {
			var fwriter *os.File
			path = filepath.Join(f.path, fname)
			fwriter, err = os.Create(path)
			if err == nil {
				defer fwriter.Close()
				_, err = io.Copy(fwriter, file)
			} else {
				status = SAVEFILE_COPY_ERROR
			}
		} else {
			status = SAVEFILE_RENAME_ERROR_BY_USER
		}
	} else {
		status = SAVEFILE_FORMFILE_ERROR
	}
	return
}
Beispiel #3
0
//Execute the file save process and return the result
func (f *filerSaver) Exec() (path string, status int, err error) {
	if _, err := os.Stat(f.path); err == nil {
		if err := os.MkdirAll(f.path, 0755); err == nil {
			var file multipart.File
			file, f.header, err = f.request.FormFile(f.key)
			if file != nil {
				defer file.Close()
			}
			if err == nil {
				fname := f.nameSetter(f.header.Filename)
				var fwriter *os.File
				path = filepath.Join(f.path, fname)
				fwriter, err = os.Create(path)
				if err == nil {
					defer fwriter.Close()
					_, err = io.Copy(fwriter, file)
				} else {
					status = SAVEFILE_COPY_ERROR
				}
			} else {
				status = SAVEFILE_FORMFILE_ERROR
			}
		} else {
			status = SAVEFILE_CREATE_DIR_ERROR
		}
	} else {
		status = SAVEFILE_STATE_DIR_ERROR
	}
	return
}
Beispiel #4
0
func (s *Server) upload(wr http.ResponseWriter, r *http.Request) {
	var (
		vid    int64
		key    int64
		cookie int64
		size   int64
		err    error
		str    string
		v      *volume.Volume
		n      *needle.Needle
		file   multipart.File
		res    = map[string]interface{}{}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), &err, res)
	if err = checkContentLength(r, s.conf.NeedleMaxSize); err != nil {
		return
	}
	str = r.FormValue("vid")
	if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		err = errors.ErrParam
		return
	}
	str = r.FormValue("key")
	if key, err = strconv.ParseInt(str, 10, 64); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		err = errors.ErrParam
		return
	}
	str = r.FormValue("cookie")
	if cookie, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		err = errors.ErrParam
		return
	}
	if file, _, err = r.FormFile("file"); err != nil {
		log.Errorf("r.FormFile() error(%v)", err)
		err = errors.ErrInternal
		return
	}
	if size, err = checkFileSize(file, s.conf.NeedleMaxSize); err == nil {
		n = s.store.Needle()
		if err = n.WriteFrom(key, int32(cookie), int32(size), file); err == nil {
			if v = s.store.Volumes[int32(vid)]; v != nil {
				err = v.Write(n)
			} else {
				err = errors.ErrVolumeNotExist
			}
		}
		s.store.FreeNeedle(n)
	}
	file.Close()
	return
}
Beispiel #5
0
func (h httpUploadHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		v                *Volume
		n                int
		vid, key, cookie int64
		err              error
		buf              []byte
		file             multipart.File
		res              = map[string]interface{}{"ret": RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	if err = r.ParseMultipartForm(NeedleMaxSize); err != nil {
		res["ret"] = RetInternalErr
		return
	}
	if vid, err = strconv.ParseInt(r.FormValue("vid"), 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("vid"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if key, err = strconv.ParseInt(r.FormValue("key"), 10, 64); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("key"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if cookie, err = strconv.ParseInt(r.FormValue("cookie"), 10, 64); err !=
		nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("cookie"),
			err)
		res["ret"] = RetParamErr
		return
	}
	if v = h.s.Volumes[int32(vid)]; v == nil {
		res["ret"] = RetNoVolume
		return
	}
	if file, _, err = r.FormFile("file"); err != nil {
		res["ret"] = RetInternalErr
		return
	}
	buf = v.Buffer()
	if n, err = file.Read(buf); err == nil {
		err = v.Add(key, cookie, buf[:n])
	}
	file.Close()
	v.FreeBuffer(buf)
	if err != nil {
		res["ret"] = RetUploadErr
	}
	return
}
Beispiel #6
0
func UploadProfilePic(wr http.ResponseWriter, req *http.Request) {
	var (
		profileImgLoc string
		id            int64
		err           error
		p             *data.Profile
		file          multipart.File
		header        *multipart.FileHeader
		enc           *json.Encoder
	)

	enc = json.NewEncoder(wr)
	params := mux.Vars(req)
	id, err = strconv.ParseInt(params["id"], 10, 64)
	p, err = data.FindProfileById(id)
	req.ParseMultipartForm(10 << 20) //approx 10MB
	file, header, err = req.FormFile("file")

	handleUploadErr := func(err error, status int) {
		if nil != err {
			wr.WriteHeader(status)
			enc.Encode(NewErrorJSON(err.Error(), status))
		}
	}

	if err != nil {
		log.Println("error upload pic " + err.Error())
		handleUploadErr(err, http.StatusBadRequest)
		return
	}
	defer file.Close()
	uploadedFilePath, err := service.SaveUploadedFile(file, header.Filename)
	if err != nil {
		log.Println("failed to create thumbnail file  " + err.Error())
		handleUploadErr(err, http.StatusInternalServerError)
		return
	}

	uploadedFilePath, err = service.ThumbnailMultipart(file, header.Filename)
	if err != nil {
		log.Println("failed to create thumbnail file  " + err.Error())
		handleUploadErr(err, http.StatusInternalServerError)
		return
	}

	profileImgLoc, err = data.PutInBucket(uploadedFilePath, header.Filename)

	if err != nil {
		log.Println("failed up upload to s3 " + err.Error())
		handleUploadErr(err, http.StatusInternalServerError)
		return
	}

	p.UpdateProfilePic(profileImgLoc)
	enc.Encode(p)

}
func saveUpload(file multipart.File, handler *multipart.FileHeader) {
	defer file.Close()
	fmt.Printf("Uploaded file info: %#v", handler.Header)
	localFilename := fmt.Sprintf("./uploads/%v.%v", handler.Filename, submissions.NewToken())
	f, err := os.OpenFile(localFilename, os.O_WRONLY|os.O_CREATE, 0666)
	checkError(err)
	defer f.Close()
	_, err = io.Copy(f, file)
	checkError(err)
}
Beispiel #8
0
func getMultipart(r *knot.WebContext, fileName string) (server colonycore.ServerByType, payload colonycore.FileBrowserPayload, err error) {
	var tmp map[string]interface{}
	_, s, err := r.GetPayloadMultipart(&tmp)

	if err != nil {
		return
	}
	payload.ServerId = s["serverId"][0]
	payload.ServerType = s["serverType"][0]
	payload.Path = s["path"][0]

	err = r.Request.ParseMultipartForm(100000)
	if err != nil {
		return
	}

	m := r.Request.MultipartForm
	files := m.File[fileName]

	for key := range files {
		var file multipart.File
		file, err = files[key].Open()
		defer file.Close()
		if err != nil {
			return
		}

		payload.File = append(payload.File, file)
		payload.FileName = append(payload.FileName, s["filename"][key])

		tmpSize, _ := strconv.ParseInt(s["filesizes"][key], 10, 64)
		payload.FileSizes = append(payload.FileSizes, tmpSize)
	}

	query := dbox.Eq("_id", payload.ServerId)

	cursor, err := colonycore.Find(new(colonycore.Server), query)
	if err != nil {
		return
	}

	data := []colonycore.ServerByType{}
	err = cursor.Fetch(&data, 0, false)
	if err != nil {
		return
	}
	defer cursor.Close()

	if len(data) != 0 {
		server = data[0]
		server.ServerType = payload.ServerType
	}

	return
}
Beispiel #9
0
func uploadPhoto(src multipart.File, hdr *multipart.FileHeader, session *sessions.Session) {
	defer src.Close()
	fName := getSha(src) + ".jpg"
	wd, _ := os.Getwd()
	path := filepath.Join(wd, "assets", "imgs", fName)
	dst, _ := os.Create(path)
	defer dst.Close()
	src.Seek(0, 0)
	io.Copy(dst, src)
	addPhoto(fName, session)
}
Beispiel #10
0
func uploadPhoto(src multipart.File, hdr *multipart.FileHeader, c *http.Cookie) *http.Cookie {
	defer src.Close()
	fName := getSha(src) + ".jpg"
	wd, _ := os.Getwd()
	path := filepath.Join(wd, "assets", "imgs", fName)
	dst, _ := os.Create(path)
	defer dst.Close()
	src.Seek(0, 0)
	io.Copy(dst, src)
	return addPhoto(fName, c)
}
Beispiel #11
0
func uploadPhoto(m map[string]string, src multipart.File, hdr *multipart.FileHeader) map[string]string {
	defer src.Close()
	fName := getSha(src) + ".jpg"
	wd, _ := os.Getwd()
	path := filepath.Join(wd, "assets", "imgs", fName)
	dst, _ := os.Create(path)
	defer dst.Close()
	src.Seek(0, 0)
	io.Copy(dst, src)
	addPhoto(m, fName)
	return m
}
Beispiel #12
0
func uploadPhoto(src multipart.File, hdr *multipart.FileHeader, c *http.Cookie) *http.Cookie {
	defer src.Close()
	fName := getSha(src) + filepath.Ext(hdr.Filename)
	wd, _ := os.Getwd()
	m := Model(c)
	path := filepath.Join(wd, "assets", "imgs", m.Name, fName)
	dst, _ := os.Create(path)
	defer dst.Close()
	src.Seek(0, 0)
	io.Copy(dst, src)
	return addPhoto("/imgs/"+m.Name+"/"+fName, c)
}
Beispiel #13
0
func cropField(field *gorm.Field, scope *gorm.Scope) (cropped bool) {
	if field.Field.CanAddr() {
		// TODO Handle scanner
		if media, ok := field.Field.Addr().Interface().(Media); ok && !media.Cropped() {
			option := parseTagOption(field.Tag.Get("media_library"))
			if media.GetFileHeader() != nil || media.NeedCrop() {
				var file multipart.File
				var err error
				if fileHeader := media.GetFileHeader(); fileHeader != nil {
					file, err = media.GetFileHeader().Open()
				} else {
					file, err = media.Retrieve(media.URL("original"))
				}

				if err != nil {
					scope.Err(err)
					return false
				}

				media.Cropped(true)

				if url := media.GetURL(option, scope, field, media); url == "" {
					scope.Err(errors.New("invalid URL"))
				} else {
					result, _ := json.Marshal(map[string]string{"Url": url})
					media.Scan(string(result))
				}

				if file != nil {
					defer file.Close()
					var handled = false
					for _, handler := range mediaHandlers {
						if handler.CouldHandle(media) {
							file.Seek(0, 0)
							if scope.Err(handler.Handle(media, file, option)) == nil {
								handled = true
							}
						}
					}

					// Save File
					if !handled {
						scope.Err(media.Store(media.URL(), option, file))
					}
				}
				return true
			}
		}
	}
	return false
}
Beispiel #14
0
func SaveUploadedFile(file multipart.File, fileName string) (string, error) {
	tmpFile := "/tmp/" + fileName
	f, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE, 0666)
	if err != nil {
		log.Println("error uploading pic " + err.Error())
		return "", err
	}
	_, err = io.Copy(f, file)
	if err != nil {
		log.Println("failed to copy file  " + err.Error())
		return "", err
	}
	file.Close()
	f.Close()
	return tmpFile, err
}
func createFileInFS(file multipart.File, extension string) (string, error) {
	defer file.Close()

	newfilename := randomString()
	imgfile, err := os.Create("tmp/" + newfilename + extension)
	if err != nil {
		return "", err
	}

	defer imgfile.Close()

	_, err = io.Copy(imgfile, file)
	if err != nil {
		return "", err
	}
	return newfilename, nil
}
Beispiel #16
0
/** Move upload file to a folder. */
func MoveFile(uploadFile multipart.File, newPath string) error {
	/* Condition validation */
	if uploadFile == nil {
		return fmt.Errorf("Upload file is empty.")
	}
	defer uploadFile.Close()

	// Create new file
	output, err := os.Create(newPath)
	if err != nil {
		return err
	}
	defer output.Close()

	// Move file to new location
	_, err = io.Copy(output, uploadFile)
	return err
}
Beispiel #17
0
func UploadFile(realFile *multipart.FileHeader) (file *File, err error) {
	var source multipart.File
	if source, err = realFile.Open(); err != nil {
		return
	}
	defer source.Close()
	var img image.Config
	if img, _, err = image.DecodeConfig(source); err != nil {
		return
	}
	source.Seek(0, 0)
	hasher := sha1.New()
	io.Copy(hasher, source)
	fileKey := hex.EncodeToString(hasher.Sum(nil))
	file = &File{Key: fileKey}
	has, _ := engine.Get(file)
	if !has {
		var dst *os.File
		var fileType = realFile.Header.Get("content-type")
		var ext, ok = config.FILE_EXTS[fileType]
		if !ok {
			ext = ".jpg"
		}
		if dst, err = os.Create(config.UPLOADPATH + fileKey + ext); err != nil {
			return
		}
		defer dst.Close()
		source.Seek(0, 0)
		if _, err = io.Copy(dst, source); err != nil {
			return
		}

		file.Width = img.Width
		file.Height = img.Height
		file.Type = fileType

		if _, err = engine.Insert(file); err != nil {
			return
		}
	}
	return
}
Beispiel #18
0
func writeToGridFile(file multipart.File, gridFile *mgo.GridFile) error {
	reader := bufio.NewReader(file)
	defer func() { file.Close() }()
	// make a buffer to keep chunks that are read
	buf := make([]byte, 1024)
	for {
		// read a chunk
		n, err := reader.Read(buf)
		if err != nil && err != io.EOF {
			return errors.New("Could not read the input file")
		}
		if n == 0 {
			break
		}
		// write a chunk
		if _, err := gridFile.Write(buf[:n]); err != nil {
			return errors.New("Could not write to GridFs for " + gridFile.Name())
		}
	}
	gridFile.Close()
	return nil
}
Beispiel #19
0
// ReadRequestFiles reads the files from the request, and calls ReaderToFile on them
func ReadRequestFiles(r *http.Request) (filenames []string, status int, err error) {
	defer r.Body.Close()
	err = r.ParseMultipartForm(1 << 20)
	if err != nil {
		status, err = 405, errors.New("cannot parse request as multipart-form: "+err.Error())
		return
	}
	if r.MultipartForm == nil || len(r.MultipartForm.File) == 0 {
		status, err = 405, errors.New("no files?")
		return
	}

	filenames = make([]string, 0, len(r.MultipartForm.File))
	var f multipart.File
	var fn string
	for _, fileHeaders := range r.MultipartForm.File {
		for _, fileHeader := range fileHeaders {
			if f, err = fileHeader.Open(); err != nil {
				status, err = 405, fmt.Errorf("error reading part %q: %s", fileHeader.Filename, err)
				return
			}
			Log.Debug("part", "filename", fileHeader.Filename)
			if fn, err = temp.ReaderToFile(f, fileHeader.Filename, ""); err != nil {
				f.Close()
				status, err = 500, fmt.Errorf("error saving %q: %s", fileHeader.Filename, err)
				return
			}
			f.Close()
			filenames = append(filenames, fn)
		}
	}
	if len(filenames) == 0 {
		status, err = 405, errors.New("no files??")
		return
	}
	status = 200
	return
}
Beispiel #20
0
// ideally, I would be able to define an adapter fn like this for every route, and the Route would
// know how to adapt itself, but then either 1) presentation details (e.g. http) would leak into the
// app, or 2) I would have to add `interface{}` to my types.  This way has some duplication (and a
// lot of indirection...), but I think it best separates app from presentation.
func (s WebServer) calhounToHttpHandler(calhounHandler CalhounHandler, route Route) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
		calhounReq := CalhounRequest{Url: route.Path}

		var file multipart.File
		var err error

		switch route.Path {
		case "/upload":
			file, _, err = r.FormFile("photoUpload")
			defer file.Close()

			if err != nil || file == nil {
				fmt.Fprintln(w, "error reading photo upload: ", err)
				return
			}

			calhounReq.UploadFile = &file
		}

		calhounHandler(w, &calhounReq)
	}
}
Beispiel #21
0
func (s *Server) uploads(wr http.ResponseWriter, r *http.Request) {
	var (
		i, nn   int
		err     error
		vid     int64
		key     int64
		cookie  int64
		size    int64
		str     string
		keys    []string
		cookies []string
		v       *volume.Volume
		ns      *needle.Needles
		file    multipart.File
		fh      *multipart.FileHeader
		fhs     []*multipart.FileHeader
		res     = map[string]interface{}{}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), &err, res)
	if err = checkContentLength(r, s.conf.NeedleMaxSize*s.conf.BatchMaxNum); err != nil {
		return
	}
	str = r.FormValue("vid")
	if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		err = errors.ErrParam
		return
	}
	keys = r.MultipartForm.Value["keys"]
	cookies = r.MultipartForm.Value["cookies"]
	if len(keys) != len(cookies) {
		log.Errorf("param length not match, keys: %d, cookies: %d", len(keys), len(cookies))
		err = errors.ErrParam
		return
	}
	fhs = r.MultipartForm.File["file"]
	nn = len(fhs)
	if len(keys) != nn {
		log.Errorf("param length not match, keys: %d, cookies: %d, files: %d", len(keys), len(cookies), len(fhs))
		err = errors.ErrParam
		return
	}
	ns = s.store.Needles(nn)
	for i, fh = range fhs {
		if key, err = strconv.ParseInt(keys[i], 10, 64); err != nil {
			log.Errorf("strconv.ParseInt(\"%s\") error(%v)", keys[i], err)
			err = errors.ErrParam
			break
		}
		if cookie, err = strconv.ParseInt(cookies[i], 10, 32); err != nil {
			log.Errorf("strconv.ParseInt(\"%s\") error(%v)", cookies[i], err)
			err = errors.ErrParam
			break
		}
		if file, err = fh.Open(); err != nil {
			log.Errorf("fh.Open() error(%v)", err)
			break
		}
		if size, err = checkFileSize(file, s.conf.NeedleMaxSize); err == nil {
			err = ns.WriteFrom(key, int32(cookie), int32(size), file)
		}
		file.Close()
		if err != nil {
			break
		}
	}
	if err == nil {
		if v = s.store.Volumes[int32(vid)]; v != nil {
			err = v.Writes(ns)
		} else {
			err = errors.ErrVolumeNotExist
		}
	}
	s.store.FreeNeedles(nn, ns)
	return
}
Beispiel #22
0
func api(mart *martini.ClassicMartini) {
	var engine = backend.GetEngine()
	mart.Post(API+"/datasets/?", binding.Bind(DatasetForm{}), func(form DatasetForm, r render.Render) {
		var err error
		var file *backend.File
		var tag *backend.Tag
		var dataset *backend.Dataset

		if form.FileId > 0 {
			file = &backend.File{Id: form.FileId}
			if has, _ := engine.Get(file); !has {
				r.JSON(http.StatusOK, map[string]interface{}{"err": "file not exists."})
				return
			}
		} else {
			if file, err = backend.UploadFile(form.File); err != nil {
				r.JSON(http.StatusInternalServerError, map[string]interface{}{"err": err.Error()})
				return
			}
		}

		if tag, err = backend.SaveTag(form.Tag); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]interface{}{"err": err.Error()})
			return
		}

		if dataset, err = backend.SaveDataset(file, tag, form.DataType, form.Description); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]interface{}{"err": err.Error()})
			return
		}

		r.JSON(http.StatusOK, map[string]*backend.Dataset{"dataset": dataset})
	})

	mart.Post(API+"/datasets/(?P<dataset_id>\\d+)/?", func(req *http.Request, params martini.Params, r render.Render) {
		datasetId, _ := strconv.Atoi(params["dataset_id"])
		var dataset = new(backend.Dataset)
		if has, err := engine.Id(datasetId).Get(dataset); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			dataset.FillObject()
			dataset.Description = req.Form.Get("description")
			if _, err := engine.Id(datasetId).Update(dataset); err != nil {
				r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
				return
			}
			r.JSON(http.StatusOK, map[string]*backend.Dataset{"dataset": dataset})
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Dataset not exists."})
		}
	})

	mart.Get(API+"/datasets/(?P<dataset_id>\\d+)/?", func(params martini.Params, r render.Render) {
		datasetId, _ := strconv.Atoi(params["dataset_id"])
		var dataset = new(backend.Dataset)
		if has, err := engine.Id(datasetId).Get(dataset); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			dataset.FillObject()
			r.JSON(http.StatusOK, map[string]*backend.Dataset{"dataset": dataset})
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Dataset not exists."})
		}
	})

	mart.Delete(API+"/datasets/(?P<dataset_id>\\d+)/?", func(params martini.Params, r render.Render) {
		datasetId, _ := strconv.Atoi(params["dataset_id"])
		var dataset = new(backend.Dataset)
		if has, err := engine.Id(datasetId).Get(dataset); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			if _, err := engine.Id(datasetId).Delete(dataset); err != nil {
				r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			} else {
				var sql string
				if dataset.DataType == backend.TRAIN {
					sql = "update `tag` set `train_count` = `train_count` - 1 where `id` = ?"
				} else if dataset.DataType == backend.VAL {
					sql = "update `tag` set `test_count` = `test_count` - 1 where `id` = ?"
				}
				engine.Exec(sql, dataset.TagId)
				r.JSON(http.StatusOK, map[string]string{})
			}
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Dataset not exists."})
		}

	})

	mart.Get(API+"/datasets/?", func(req *http.Request, r render.Render) {
		var qs = req.URL.Query()
		var err error
		var max, limit int
		if max, err = strconv.Atoi(qs.Get("max")); err != nil {
			max = -1
		}

		if limit, err = strconv.Atoi(qs.Get("limit")); err != nil {
			limit = 10
		}

		if limit > 100 {
			limit = 100
		}

		var dataType = qs.Get("data_type")

		var tagName = qs.Get("tag")

		var datasets = make([]backend.Dataset, 0)
		var q = engine.Desc("id")
		if max > -1 {
			q = q.Where("id < ?", max)
		}

		if tagName != "" {
			tag := &backend.Tag{Name: tagName}
			has, _ := engine.Get(tag)
			if !has {
				r.JSON(http.StatusNotFound,
					map[string]string{"err": "tag: " + tagName + " not found."})
				return
			}
			q = q.And("tag_id = ?", tag.Id)
		}

		if dataType == "train" {
			q = q.And("data_type = ?", backend.TRAIN)
		} else if dataType == "val" {
			q = q.And("data_type = ?", backend.VAL)
		}

		q = q.Limit(limit)
		if err = q.Find(&datasets); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		}

		for idx, dataset := range datasets {
			dataset.FillObject()
			datasets[idx] = dataset
		}

		r.JSON(http.StatusOK, map[string][]backend.Dataset{"datasets": datasets})
	})

	mart.Post(API+"/upload/?", binding.Bind(FileForm{}), func(form FileForm, r render.Render) {
		var err error
		var file *backend.File

		if file, err = backend.UploadFile(form.File); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]interface{}{"err": err.Error()})
			return
		}

		r.JSON(http.StatusOK, map[string]*backend.File{"file": file})
	})

	mart.Post(API+"/tags/?", binding.Bind(TagForm{}), func(form TagForm, r render.Render) {
		var err error
		var tag *backend.Tag

		if tag, err = backend.SaveTag(form.Tag); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]interface{}{"err": err.Error()})
		}

		r.JSON(http.StatusOK, map[string]*backend.Tag{"file": tag})
	})

	mart.Post(API+"/tags/(?P<tag_id>\\d+)/?", binding.Bind(TagForm{}), func(form TagForm, params martini.Params, r render.Render) {
		tagId, _ := strconv.Atoi(params["tag_id"])
		var tag = new(backend.Tag)
		if has, err := engine.Id(tagId).Get(tag); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			if tag.Name != form.Tag {
				tag.Name = form.Tag
				if _, err := engine.Id(tagId).Update(tag); err != nil {
					r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
					return
				}
			}
			r.JSON(http.StatusOK, map[string]*backend.Tag{"tag": tag})
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Tag not exists."})
		}
	})

	mart.Get(API+"/tags/(?P<tag_id>\\d+)/?", func(params martini.Params, r render.Render) {
		tagId, _ := strconv.Atoi(params["tag_id"])
		var tag = new(backend.Tag)
		if has, err := engine.Id(tagId).Get(tag); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			r.JSON(http.StatusOK, map[string]*backend.Tag{"tag": tag})
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Tag not exists."})
		}
	})

	mart.Delete(API+"/tags/(?P<tag_id>\\d+)/?", func(params martini.Params, r render.Render) {
		tagId, _ := strconv.Atoi(params["tag_id"])
		var tag = new(backend.Tag)
		if has, err := engine.Id(tagId).Get(tag); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		} else if has {
			backend.DeleteTag(tag.Id)
			r.JSON(http.StatusOK, map[string]*backend.Tag{"tag": tag})
		} else {
			r.JSON(http.StatusNotFound, map[string]string{"err": "Tag not exists."})
		}
	})

	mart.Get(API+"/tags/hint/?", func(req *http.Request, r render.Render) {
		var qs = req.URL.Query()
		var word = qs.Get("word")
		var q = engine.Desc("id")
		q = q.And("name like \"%" + word + "%\"")
		q = q.Limit(5)
		var tags = make([]backend.Tag, 0)
		var err = q.Find(&tags)
		log.Printf("err: %s\n", err)
		r.JSON(http.StatusOK, map[string][]backend.Tag{"tags": tags})
	})

	mart.Get(API+"/tags/?", func(req *http.Request, r render.Render) {
		var qs = req.URL.Query()
		var err error
		var max, limit int
		if max, err = strconv.Atoi(qs.Get("max")); err != nil {
			max = -1
		}

		if limit, err = strconv.Atoi(qs.Get("limit")); err != nil {
			limit = 10
		}

		if limit > 100 {
			limit = 100
		}

		var tags = make([]backend.Tag, 0)
		var q = engine.Desc("id")
		if max > -1 {
			q = q.Where("id < ?", max)
		}
		q = q.Limit(limit)
		if err = q.Find(&tags); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
		}

		r.JSON(http.StatusOK, map[string][]backend.Tag{"tags": tags})
	})

	mart.Post(API+"/solve/?", func(r render.Render) {
		caffe.Solve()
		r.JSON(http.StatusOK, caffe.LastStatus())
		return
	})

	mart.Get(API+"/solve/?", func(r render.Render) {
		r.JSON(http.StatusOK, caffe.LastStatus())
		return
	})

	mart.Delete(API+"/solve/?", func(r render.Render) {
		caffe.StopSolve()
		r.JSON(http.StatusOK, caffe.LastStatus())
		return
	})

	mart.Get(API+"/train.txt", func(r render.Render) {
		text, err := backend.ExportDataset(backend.TRAIN)
		if err != nil {
			r.Data(http.StatusInternalServerError, nil)
			return
		}
		r.Data(http.StatusOK, []byte(text))
	})

	mart.Get(API+"/val.txt", func(r render.Render) {
		text, err := backend.ExportDataset(backend.VAL)
		if err != nil {
			r.Data(http.StatusInternalServerError, nil)
			return
		}
		r.Data(http.StatusOK, []byte(text))
	})

	mart.Get(API+"/loss.png", func(r render.Render) {
		result, err := caffe.Plot("loss")
		if err != nil {
			r.Redirect("/static/images/loading.png")
			return
		}
		r.Data(http.StatusOK, result)
		return
	})

	mart.Get(API+"/acc.png", func(r render.Render) {
		result, err := caffe.Plot("acc")
		if err != nil {
			r.Redirect("/static/images/loading.png")
			return
		}
		r.Data(http.StatusOK, result)
		return
	})

	mart.Post(API+"/predict/?", binding.Bind(PredictForm{}), func(form PredictForm, r render.Render) {
		var err error
		var result caffe.PredictResult
		if len(form.ImgUrl) > 0 {
			result, err = caffe.PredictUrl(form.ImgUrl)
		} else {
			var source multipart.File
			if source, err = form.File.Open(); err == nil {
				defer source.Close()
				result, err = caffe.Predict(source)
			}
		}
		if err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, result)
		return
	})

	mart.Post(API+"/predict/process/?", func(r render.Render) {
		if err := caffe.StartPredict(); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string]string{})
		return
	})

	mart.Delete(API+"/predict/process/?", func(r render.Render) {
		if err := caffe.StopPredict(); err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string]string{})
		return
	})

	mart.Get(API+"/proxy/?", func(req *http.Request, r render.Render) {
		var qs = req.URL.Query()
		var url = qs.Get("url")
		var resp *http.Response
		var err error
		if resp, err = http.Get(url); err != nil {
			r.Data(http.StatusNotFound, nil)
			return
		}
		defer resp.Body.Close()
		data, _ := ioutil.ReadAll(resp.Body)
		r.Data(http.StatusOK, data)
		return
	})

	mart.Get(API+"/models", func(r render.Render) {
		modelNames, err := caffe.ListModels()
		if err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string][]string{"models": modelNames})
		return
	})

	mart.Get(API+"/models/current", func(r render.Render) {
		modelName, err := caffe.GetCurrentModel()
		if err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string]string{"model": modelName})
		return
	})

	mart.Post(API+"/models/apply", binding.Bind(ModelForm{}), func(model ModelForm, r render.Render) {
		err := caffe.ApplyModel(model.ModelName)
		if err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string]string{})
		return
	})

	mart.Delete(API+"/models/(?P<modelName>[^/]+.caffemodel)", func(params martini.Params, r render.Render) {
		err := caffe.RemoveModel(params["modelName"])
		if err != nil {
			r.JSON(http.StatusInternalServerError, map[string]string{"err": err.Error()})
			return
		}
		r.JSON(http.StatusOK, map[string]string{})
		return
	})
}
Beispiel #23
0
func uploadPhoto(src multipart.File, id string, req *http.Request) error {
	defer src.Close()
	fName := getSha(src) + ".jpg"
	return addPhoto(fName, id, req)
}
Beispiel #24
0
func init() {
	cfg := config.NewConfig()

	http.Handle("/clients", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		params := struct{ ViewOnly bool }{cfg.ViewOnly}
		util.RenderHTML(w, []string{"clients/index.tmpl"}, params, nil)
	}))
	http.Handle("/clients/export", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Disposition", "attachment; filename=docker-clients.json")
		w.Header().Set("Content-Type", "application/force-download")
		http.ServeFile(w, r, models.DockerClientSavePath)
	}))
	http.Handle("/clients/import", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		var err error
		if err = r.ParseMultipartForm(32 << 20); nil != err {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		for _, headers := range r.MultipartForm.File {
			var in multipart.File
			if in, err = headers[0].Open(); nil != err {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			defer in.Close()

			var out *os.File
			if out, err = os.Create(models.DockerClientSavePath); nil != err {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			defer out.Close()

			if _, err = io.Copy(out, in); nil != err {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
		}
		w.WriteHeader(http.StatusOK)
	}))

	/**
	 * Docker client's API
	 */
	http.Handle("/api/clients", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		result := []cInformation{}
		clients, err := models.LoadDockerClients()
		if err != nil {
			renderErrorJSON(w, err)
			return
		}
		c := make(chan cInformation, len(clients))
		for _, client := range clients {
			go func(client *models.DockerClient) {
				engine.Configure(client.Endpoint, client.CertPath)
				client.IsActive = true

				docker, err := engine.Docker()
				if err != nil {
					client.IsActive = false
					c <- cInformation{client, nil, nil}
					return
				}
				info, _ := docker.Info()
				version, _ := docker.Version()
				c <- cInformation{client, info, version}
			}(client)
		}
		for i := 0; i < len(clients); i++ {
			info := <-c
			info.Client.Save()
			result = append(result, info)
		}
		close(c)
		util.RenderJSON(w, result, nil)
	}))

	http.Handle("/api/client/", util.Chain(func(w http.ResponseWriter, r *http.Request) {
		if endpoint, found := util.RequestPostParam(r, "endpoint"); found {
			cert, _ := util.RequestPostParam(r, "cert")
			engine.Configure(endpoint, cert)
			engine.Save()
			_, err := engine.Docker()
			if err != nil {
				models.RemoveDockerClientByEndpoint(endpoint)
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			http.Redirect(w, r, "/api/client/", http.StatusFound)
			return
		}
		if r.Method == "DELETE" {
			models.RemoveDockerClient(r.URL.Path[len("/api/client/"):])
			w.WriteHeader(http.StatusOK)
			return
		}
		docker, err := engine.Docker()
		if err != nil {
			renderErrorJSON(w, err)
			return
		}
		info, _ := docker.Info()
		version, _ := docker.Version()
		util.RenderJSON(w, cInformation{nil, info, version}, nil)
	}))
}
Beispiel #25
0
func SaveAndCropImage(isCreate bool) func(scope *gorm.Scope) {
	return func(scope *gorm.Scope) {
		for _, field := range scope.Fields() {
			if media, ok := field.Field.Addr().Interface().(MediaLibrary); ok {
				option := parseTagOption(field.Tag.Get("media_library"))
				if media.GetFileHeader() != nil || media.NeedCrop() {
					var file multipart.File
					var err error
					if fileHeader := media.GetFileHeader(); fileHeader != nil {
						file, err = media.GetFileHeader().Open()
					} else {
						file, err = media.Retrieve(media.URL("original"))
					}

					if scope.Err(err) != nil {
						return
					}

					if url := media.GetURL(option, scope, field, media); url == "" {
						scope.Err(errors.New("invalid URL"))
					} else {
						result, _ := json.Marshal(map[string]string{"Url": url})
						media.Scan(string(result))
					}

					if isCreate && !scope.HasError() {
						if value, err := media.Value(); err == nil {
							gorm.Update(scope.New(scope.Value).InstanceSet("gorm:update_attrs", map[string]interface{}{field.DBName: value}))
						}
					}

					if file != nil {
						defer file.Close()

						if media.IsImage() {
							// Save Original Image
							if scope.Err(media.Store(media.URL("original"), option, file)) == nil {
								file.Seek(0, 0)

								// Crop & Resize
								if img, err := imaging.Decode(file); scope.Err(err) == nil {
									if format, err := getImageFormat(media.URL()); scope.Err(err) == nil {
										if cropOption := media.GetCropOption("original"); cropOption != nil {
											img = imaging.Crop(img, *cropOption)
										}

										// Save default image
										var buffer bytes.Buffer
										imaging.Encode(&buffer, img, *format)
										media.Store(media.URL(), option, &buffer)

										for key, size := range media.GetSizes() {
											newImage := img
											if cropOption := media.GetCropOption(key); cropOption != nil {
												newImage = imaging.Crop(newImage, *cropOption)
											}

											dst := imaging.Thumbnail(newImage, size.Width, size.Height, imaging.Lanczos)
											var buffer bytes.Buffer
											imaging.Encode(&buffer, dst, *format)
											media.Store(media.URL(key), option, &buffer)
										}
									}
								}
							}
						} else {
							// Save File
							scope.Err(media.Store(media.URL(), option, file))
						}
					}
				}
			}
		}
	}
}
func handleReviewRegister(resp http.ResponseWriter, req *http.Request) {
	email := public.EmailFilter(req.FormValue("email"))
	username := req.FormValue("username")
	formalId := req.FormValue("formalId")
	password := req.FormValue("password")

	//Verify values first
	var errorFields []string
	if len(email) <= 0 {
		errorFields = append(errorFields, "Email")
	}
	if len(username) <= 0 {
		errorFields = append(errorFields, "Username")
	}
	if len(password) <= 0 {
		errorFields = append(errorFields, "Password")
	}
	if len(formalId) != 10 {
		errorFields = append(errorFields, "FormalId")
	} else {
		if match, _ := regexp.MatchString("[A-Z][12][0-9]{8}", formalId); match {
			if !public.FormalIdVerifier(formalId) {
				errorFields = append(errorFields, "FormalId")
			}
		} else {
			errorFields = append(errorFields, "FormalId")
		}
	}

	if len(errorFields) > 0 {
		r := public.SimpleResult{
			Message:     "Error",
			Description: "Wrong Format: " + strings.Join(errorFields, ","),
		}
		public.ResponseStatusAsJson(resp, 400, &r)
	} else {
		//Get thumbnail if exist
		var thumb multipart.File = nil
		var thumbHeader *multipart.FileHeader = nil
		if f, h, err := req.FormFile("thumbnail"); err == nil && f != nil {
			thumb = f
			thumbHeader = h
		}

		reviewerDb := public.GetNewReviewerDatabase()
		defer reviewerDb.Session.Close()

		profile := reviewerDb.C(REVIEWER_DB_PROFILE_COLLECTION)
		q := profile.Find(bson.M{"baseprofile.email": email})
		if cnt, err := q.Count(); cnt != 0 || err != nil {
			if err != nil {
				r := public.SimpleResult{
					Message:     "Error",
					Description: err.Error(),
				}
				public.ResponseStatusAsJson(resp, 500, &r)
			} else {
				//User exist
				r := public.SimpleResult{
					Message:     "Error",
					Description: "User Exists",
				}
				public.ResponseStatusAsJson(resp, 400, &r)
			}
		} else {
			baseUser := db.User{
				Email:    email,
				Username: username,
				FormalId: formalId,
			}
			hash, _ := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
			baseUser.AuthInfo = db.UserAuth{
				BcryptCost: bcrypt.DefaultCost,
				BcyptHash:  string(hash),
			}

			//Store thumbnail
			if thumb != nil {
				defer thumb.Close()
				if client, err := storage.GetNewStorageClient(); err == nil {

					h := public.NewHashString()
					objName := storage.PathJoin(storage.THUMBNAILS_FOLDER_NAME, h)
					//Determine the extension
					var ext string = ""
					if thumbHeader != nil {
						if segs := strings.Split(thumbHeader.Filename, "."); len(segs) > 1 {
							ext = "." + segs[len(segs)-1]
							objName = (objName + ext)
						}
					}

					obj := client.GetDefaultBucket().Object(objName)
					if attr, _ := obj.Attrs(client.Ctx); attr != nil {
						if mimeStr := mime.TypeByExtension(ext); len(mimeStr) > 0 {
							attr.ContentType = mimeStr
						}
					}
					objWriter := obj.NewWriter(client.Ctx)
					defer objWriter.Close()

					_, err = io.Copy(objWriter, thumb)
					if err == nil {
						baseUser.Thumbnail = objName
					}
				}
			}

			newUser := db.Reviewer{
				BaseProfile: baseUser,
			}

			if err := profile.Insert(&newUser); err != nil {
				r := public.SimpleResult{
					Message:     "Register Failed",
					Description: err.Error(),
				}
				public.ResponseStatusAsJson(resp, 400, &r)
			} else {
				if err := public.SetReviewerSessionValue(req, resp, public.REVIEWER_ID_SESSION_KEY, newUser.Id.Hex()); err != nil {
					public.LogE.Printf("Error setting session user id: %s\n", err.Error())
				}

				r := public.SimpleResult{
					Message:     "Register Successed",
					Description: email,
				}
				public.ResponseOkAsJson(resp, &r)
			}
		}
	}
}
Beispiel #27
0
//文件上传
func (this *base) upload(key string) (files []*models.UploadFile, err error) {
	//处理上传文件
	var header *multipart.FileHeader
	var file multipart.File
	var f *os.File

	//根据年月选择文件夹
	t := time.Now().Format(time.RFC3339)
	//文件夹是否存在或创建文件夹
	UploadPath := beego.AppConfig.String("UploadPath")
	folder := utils.MergePath(UploadPath)
	err = utils.GetDir(folder)
	if err != nil {
		return
	}
	// //用户文件夹是否存在或创建文件夹
	// folder = filepath.Join(folder, strconv.Itoa(int(this.xm.Updator)))
	// err = utils.GetDir(folder)
	// if err != nil {
	// 	return
	// }
	//文件夹是否存在或创建文件夹
	UploadPath = path.Join(UploadPath, beego.Substr(t, 0, 7))
	folder = path.Join(folder, beego.Substr(t, 0, 7))
	err = utils.GetDir(folder)
	if err != nil {
		return
	}

	fs := this.Ctx.Request.MultipartForm.File[key]

	n := len(fs)
	if n == 0 {
		err = errors.New("files not found")
		return
	}

	for i := 0; i < n; i++ {
		header = fs[i]
		file, err = fs[i].Open()

		if err != nil {
			return
		}

		defer file.Close()

		//提取原始文件信息
		disposition := strings.Split(header.Header.Get("Content-Disposition"), ";")

		var key, value string
		for _, v := range disposition {

			pos := strings.Index(v, "=")
			if pos > -1 {
				key = v[:pos]

				if strings.TrimSpace(key) == "filename" {
					value = strings.Replace(v[pos+1:], "\"", "", -1)
					break
				}
			}
		}
		//
		filename := filepath.Base(value)

		//新建文件
		UploadPath = path.Join("/", UploadPath, fmt.Sprintf("%d%s", time.Now().UnixNano(), filepath.Ext(filename)))
		f, err = os.OpenFile(path.Join(folder, fmt.Sprintf("%d%s", time.Now().UnixNano(), filepath.Ext(filename))), os.O_WRONLY|os.O_CREATE, 0666)
		if err != nil {
			return
		}

		defer f.Close()

		io.Copy(f, file)

		upf := new(models.UploadFile)
		upf.Name = filename
		upf.Ext = filepath.Ext(filename)
		upf.Path = UploadPath
		fi, _ := f.Stat()
		upf.Size = fi.Size()

		files = append(files, upf)
	}
	return
}
Beispiel #28
0
func (h httpUploadsHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		i, wn               int
		v                   *Volume
		n                   *Needle
		buf                 []byte
		str                 string
		err                 error
		vid, key, cookie    int64
		keyStrs, cookieStrs []string
		keys, cookies       []int64
		fh                  *multipart.FileHeader
		fhs                 []*multipart.FileHeader
		file                multipart.File
		res                 = map[string]interface{}{"ret": RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	if err = r.ParseMultipartForm(NeedleMaxSize); err != nil {
		res["ret"] = RetInternalErr
		return
	}
	if vid, err = strconv.ParseInt(r.FormValue("vid"), 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", r.FormValue("vid"),
			err)
		res["ret"] = RetParamErr
		return
	}
	keyStrs = strings.Split(r.FormValue("keys"), HttpParamSpliter)
	cookieStrs = strings.Split(r.FormValue("cookies"), HttpParamSpliter)
	if len(keyStrs) != len(cookieStrs) {
		log.Errorf("param length not match, keys: %d, cookies: %d",
			len(keyStrs), len(cookieStrs))
		res["ret"] = RetParamErr
		return
	}
	for i, str = range keyStrs {
		if key, err = strconv.ParseInt(str, 10, 64); err != nil {
			res["ret"] = RetParamErr
			return
		}
		if cookie, err = strconv.ParseInt(str, 10, 64); err != nil {
			res["ret"] = RetParamErr
			return
		}
		keys = append(keys, key)
		cookies = append(cookies, cookie)
	}
	if r.MultipartForm != nil {
		if fhs = r.MultipartForm.File["file"]; len(fhs) > HttpMaxUploadFiles {
			res["ret"] = RetUploadMaxFile
			return
		}
	}
	if len(keys) != len(fhs) {
		log.Errorf("param length not match, keys: %d, cookies: %d, files: %d",
			len(keys), len(cookies), len(fhs))
		res["ret"] = RetParamErr
		return
	}
	if v = h.s.Volumes[int32(vid)]; v == nil {
		res["ret"] = RetNoVolume
		return
	}
	// TODO?
	// use a large buffer stored all file buffer
	// this can let the lock without file read and needle parse.
	buf = v.Buffer()
	n = v.Needle()
	v.Lock()
	for i, fh = range fhs {
		if file, err = fh.Open(); err == nil {
			if wn, err = file.Read(buf); err == nil {
				if err = n.Parse(keys[i], cookies[i], buf[:wn]); err == nil {
					err = v.Write(n)
				}
			}
			file.Close()
		}
		if err != nil {
			goto free
		}
	}
	v.Flush()
free:
	v.Unlock()
	v.FreeNeedle(n)
	v.FreeBuffer(buf)
	if err != nil {
		res["ret"] = RetUploadErr
	}
	return
}
Beispiel #29
0
func (h httpUploadsHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		i, rn, tn, nn, nb int
		ok                bool
		buf               []byte
		err               error
		vid               int64
		key               int64
		cookie            int64
		size              int64
		str               string
		keys              []string
		cookies           []string
		sr                sizer
		fr                *os.File
		fi                os.FileInfo
		v                 *Volume
		n                 *needle.Needle
		ns                []needle.Needle
		uerr              errors.Error
		file              multipart.File
		fh                *multipart.FileHeader
		fhs               []*multipart.FileHeader
		res               = map[string]interface{}{"ret": errors.RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	// check total content-length
	if size, err = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64); err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	if size > int64(h.c.NeedleMaxSize*h.c.BatchMaxNum) {
		res["ret"] = errors.RetNeedleTooLarge
		return
	}
	str = r.FormValue("vid")
	if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	keys = r.MultipartForm.Value["keys"]
	cookies = r.MultipartForm.Value["cookies"]
	if len(keys) != len(cookies) {
		log.Errorf("param length not match, keys: %d, cookies: %d", len(keys), len(cookies))
		res["ret"] = errors.RetParamErr
		return
	}
	fhs = r.MultipartForm.File["file"]
	nn = len(fhs)
	if len(keys) != nn {
		log.Errorf("param length not match, keys: %d, cookies: %d, files: %d", len(keys), len(cookies), len(fhs))
		res["ret"] = errors.RetParamErr
		return
	}
	nb = int(size-1)/(h.c.NeedleMaxSize) + 1
	buf = h.s.Buffer(nb)
	ns = h.s.Needle(nn)
	for i, fh = range fhs {
		if key, err = strconv.ParseInt(keys[i], 10, 64); err != nil {
			log.Errorf("strconv.ParseInt(\"%s\") error(%v)", keys[i], err)
			err = errors.ErrParam
			break
		}
		if cookie, err = strconv.ParseInt(cookies[i], 10, 32); err != nil {
			log.Errorf("strconv.ParseInt(\"%s\") error(%v)", cookies[i], err)
			err = errors.ErrParam
			break
		}
		file, err = fh.Open()
		file.Close()
		if err != nil {
			log.Errorf("fh.Open() error(%v)", err)
			break
		}
		// check size
		if sr, ok = file.(sizer); ok {
			size = sr.Size()
		} else if fr, ok = file.(*os.File); ok {
			if fi, err = fr.Stat(); err != nil {
				break
			}
			size = fi.Size()
		}
		if size > int64(h.c.NeedleMaxSize) {
			err = errors.ErrNeedleTooLarge
			break
		}
		if rn, err = file.Read(buf[tn:]); err != nil {
			log.Errorf("file.Read() error(%v)", err)
			break
		}
		n = &(ns[i])
		n.Parse(key, int32(cookie), buf[tn:tn+rn])
		tn += rn
	}
	if err == nil {
		h.s.RLockVolume()
		if v = h.s.Volumes[int32(vid)]; v != nil {
			v.Lock()
			for i = 0; i < nn; i++ {
				n = &(ns[i])
				if err = v.Write(n); err != nil {
					break
				}
			}
			if err == nil {
				err = v.Flush()
			}
			v.Unlock()
		} else {
			err = errors.ErrVolumeNotExist
		}
		h.s.RUnlockVolume()
	}
	h.s.FreeBuffer(nb, buf)
	h.s.FreeNeedle(nn, ns)
	if err != nil {
		if uerr, ok = err.(errors.Error); ok {
			res["ret"] = int(uerr)
		} else {
			res["ret"] = errors.RetInternalErr
		}
	}
	return
}
Beispiel #30
0
func (h httpUploadHandler) ServeHTTP(wr http.ResponseWriter, r *http.Request) {
	var (
		ok     bool
		rn     int
		vid    int64
		key    int64
		cookie int64
		size   int64
		err    error
		str    string
		buf    []byte
		v      *Volume
		n      *needle.Needle
		ns     []needle.Needle
		file   multipart.File
		sr     sizer
		fr     *os.File
		fi     os.FileInfo
		uerr   errors.Error
		res    = map[string]interface{}{"ret": errors.RetOK}
	)
	if r.Method != "POST" {
		http.Error(wr, "method not allowed", http.StatusMethodNotAllowed)
		return
	}
	defer HttpPostWriter(r, wr, time.Now(), res)
	// check total content-length
	if size, err = strconv.ParseInt(r.Header.Get("Content-Length"), 10, 64); err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	if size > int64(h.c.NeedleMaxSize) {
		res["ret"] = errors.RetNeedleTooLarge
		return
	}
	str = r.FormValue("vid")
	if vid, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	str = r.FormValue("key")
	if key, err = strconv.ParseInt(str, 10, 64); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	str = r.FormValue("cookie")
	if cookie, err = strconv.ParseInt(str, 10, 32); err != nil {
		log.Errorf("strconv.ParseInt(\"%s\") error(%v)", str, err)
		res["ret"] = errors.RetParamErr
		return
	}
	if file, _, err = r.FormFile("file"); err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	if sr, ok = file.(sizer); ok {
		size = sr.Size()
	} else if fr, ok = file.(*os.File); ok {
		if fi, err = fr.Stat(); err != nil {
			res["ret"] = errors.RetInternalErr
			return
		}
		size = fi.Size()
	}
	if size > int64(h.c.NeedleMaxSize) {
		res["ret"] = errors.RetNeedleTooLarge
		return
	}
	ns = h.s.Needle(1)
	n = &(ns[0])
	buf = h.s.Buffer(1)
	rn, err = file.Read(buf)
	file.Close()
	if err != nil {
		res["ret"] = errors.RetInternalErr
		return
	}
	n.Parse(key, int32(cookie), buf[:rn])
	h.s.RLockVolume()
	if v = h.s.Volumes[int32(vid)]; v != nil {
		err = v.Add(n)
	} else {
		err = errors.ErrVolumeNotExist
	}
	h.s.RUnlockVolume()
	h.s.FreeBuffer(1, buf)
	h.s.FreeNeedle(1, ns)
	if err != nil {
		if uerr, ok = err.(errors.Error); ok {
			res["ret"] = int(uerr)
		} else {
			res["ret"] = errors.RetInternalErr
		}
	}
	return
}