示例#1
0
// UploadHandler file
func UploadHandler(c *gin.Context) {
	os.Mkdir(uploadPath, 0777)

	file, header, err := c.Request.FormFile(uploadField)
	if err != nil {
		x.Error("FILE_UPLOAD_ERROR", err.Error())

		return
	}

	ext := getExtension(header.Filename)
	name := x.GenerateHash() + "." + ext
	filePath := uploadPath + name
	out, err := os.Create(filePath)
	if err != nil {
		x.Error("FILE_UPLOAD_CREATE_ERROR", err.Error())

		return
	}

	defer out.Close()
	_, err = io.Copy(out, file)
	if err != nil {
		x.Error("FILE_UPLOAD_COPY_ERROR", err.Error())

		return
	}

	size, err := getFileSize(file)
	if err != nil {
		x.Error("FILE_UPLOAD_SIZE_ERROR", err.Error())

		return
	}

	// check mime type
	mime := header.Header.Get("Content-Type")
	if mime == "" {
		x.Error("FILE_UPLOAD_MIME_ERROR", "cant get file content-type")

		return
	}

	// save meta on database
	payload := Object{
		ID:   name,
		Ext:  ext,
		Size: size,
		Mime: mime,
	}

	service.Create(payload)

	x.Output(payload)
}
示例#2
0
文件: resource.go 项目: javinc/puto
// Create file
func (t *Resource) Create(p Object) (Object, error) {
	// set uuid if not set
	if p.ID == "" {
		p.ID = module.GenerateHash()
	}

	// meta data
	p.CreatedAt = time.Now()
	p.UpdatedAt = time.Now()

	// insert to database
	_, err := r.Table(resourceName).Insert(p).RunWrite(module.RSession)
	if err != nil {
		return Object{}, err
	}

	return p, err
}