当前位置: 首页>>代码示例>>Golang>>正文


Golang GridFile.Close方法代码示例

本文整理汇总了Golang中labix/org/v2/mgo.GridFile.Close方法的典型用法代码示例。如果您正苦于以下问题:Golang GridFile.Close方法的具体用法?Golang GridFile.Close怎么用?Golang GridFile.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在labix/org/v2/mgo.GridFile的用法示例。


在下文中一共展示了GridFile.Close方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: SetGridFile

func SetGridFile(sf StructFile) {
	var (
		dbFile *mgo.GridFile
		err    error
	)
	dbFile, err = gridFS.Create(sf.Name)
	check(err)

	io.Copy(dbFile, sf.File)
	check(err)
	err = dbFile.Close()
	check(err)

}
开发者ID:kurze,项目名称:dtc,代码行数:14,代码来源:dtc.go

示例2: GridFSFileUpload

func GridFSFileUpload(w http.ResponseWriter, rew *http.Request) {
	if auth := HttpAuthenticate(w, rew); auth {
		if rew.Method == "POST" {
			fmt.Println("METHOD WAS POST")
			GridFS := MongoDB.GridFS("fs")
			var file *mgo.GridFile
			var filename string

			rew.ParseMultipartForm(500000)
			formfileheaderarr := rew.MultipartForm.File["file"]
			formfileheader := formfileheaderarr[0]
			formfile, err := formfileheader.Open()

			//formfile, formfileheader, err := rew.FormFile("file")
			if err == nil {
				if rew.FormValue("filename") == "" {
					filename = formfileheader.Filename
				} else {
					filename = rew.FormValue("filename")
				}
				fmt.Println(filename)

				file, err = GridFS.Create(filename)

				if err == nil {
					_, err = io.Copy(file, formfile)
					if err == nil {
						file.SetContentType(formfileheader.Header.Get("Content-Type"))
						err = file.Close()
						if err == nil {
							Template(TemplateFileUploaded).Execute(w, "Grid/"+filename)
						}
					}
				}

			}

			if err != nil {
				io.WriteString(w, "Error occured: "+err.Error())
			}

			//GridFS.Create(
			//io.Copy()
			//rew.FormFile("file").
		} else if rew.Method == "GET" {
		}
	}
}
开发者ID:ylmbtm,项目名称:nitori-go,代码行数:48,代码来源:http_file.go

示例3: Put

func (s *Storage) Put(filename string, contentType string, body io.Reader, attachment *tenpu.Attachment) (err error) {
	var f *mgo.GridFile
	s.database.DatabaseDo(func(db *mgo.Database) {
		f, err = db.GridFS("fs").Create(filename)
		defer f.Close()
		if err != nil {
			panic(err)
		}
		if attachment.Id != "" {
			f.SetId(bson.ObjectIdHex(attachment.Id))
		}
		f.SetContentType(contentType)
		_, err = io.Copy(f, body)
	})

	if err == io.ErrUnexpectedEOF {
		attId := f.Id().(bson.ObjectId)
		s.Delete(attId.Hex())
		return
	}

	if attachment.Id == "" {
		attachment.Id = f.Id().(bson.ObjectId).Hex()
		attachment.ContentLength = f.Size()
		attachment.ContentType = f.ContentType()
		attachment.Filename = f.Name()
		attachment.MD5 = f.MD5()
		if attachment.IsImage() {
			s.database.DatabaseDo(func(db *mgo.Database) {
				f, err := db.GridFS("fs").OpenId(bson.ObjectIdHex(attachment.Id))
				if err == nil {
					config, _, err := image.DecodeConfig(f)
					f.Close()
					if err == nil {
						attachment.Width = config.Width
						attachment.Height = config.Height
					}
				}
			})
		}
	}

	return
}
开发者ID:jmptrader,项目名称:tenpu,代码行数:44,代码来源:fs.go

示例4: Put

func (s *Storage) Put(filename string, contentType string, body io.Reader, attachment *tenpu.Attachment) (err error) {
	var f *mgo.GridFile

	s.database.DatabaseDo(func(db *mgo.Database) {
		f, err = db.GridFS("fs").Create(filename)
		defer f.Close()
		if err != nil {
			panic(err)
		}
		f.SetContentType(contentType)
		io.Copy(f, body)

	})

	attachment.Id = f.Id().(bson.ObjectId).Hex()
	attachment.ContentLength = f.Size()
	attachment.ContentType = f.ContentType()
	attachment.Filename = f.Name()
	attachment.MD5 = f.MD5()
	return
}
开发者ID:athom,项目名称:tenpu,代码行数:21,代码来源:fs.go

示例5: Copy

func (s *mongodbStorage) Copy(localName, remoteName string, toRemote bool, checksum, moduleType string) (stat *contester_proto.FileStat, err error) {
	ec := tools.ErrorContext("mongodb.Copy")

	if toRemote {
		stat, err = tools.StatFile(localName, true)
		if err != nil {
			err = ec.NewError(err, "local.CalculateChecksum")
		}
		// If file doesn't exist then stat == nil.
		if err != nil || stat == nil {
			return
		}

		if checksum != "" && *stat.Checksum != checksum {
			return nil, ec.NewError(fmt.Errorf("Checksum mismatch, local %s != %s", stat.Checksum, checksum))
		}

		checksum = *stat.Checksum
	}

	var local *os.File
	if toRemote {
		local, err = os.Open(localName)
	} else {
		local, err = os.Create(localName)
	}

	if err != nil {
		return nil, ec.NewError(err, "local.Open")
	}
	defer local.Close()

	var remote *mgo.GridFile
	if toRemote {
		// Remove all files with the same remoteName.
		if err = s.GridFS.Remove(remoteName); err != nil {
			return nil, ec.NewError(err, "remote.Remove")
		}
		remote, err = s.GridFS.Create(remoteName)
	} else {
		remote, err = s.GridFS.Open(remoteName)
	}
	if err != nil {
		return nil, ec.NewError(err, "remote.Open")
	}
	defer remote.Close()

	var source io.ReadCloser
	if toRemote {
		source = local
	} else {
		source = remote
		var meta fileMetadata
		if err = remote.GetMeta(&meta); err != nil {
			return nil, ec.NewError(err, "remote.GetMeta")
		}
		if meta.CompressionType == "ZLIB" {
			source, err = zlib.NewReader(source)
			if err != nil {
				return nil, ec.NewError(err, "zlib.NewReader")
			}
		}
	}

	var destination io.WriteCloser
	if toRemote {
		destination = zlib.NewWriter(remote)
	} else {
		destination = local
	}

	size, err := io.Copy(destination, source)
	if err != nil {
		return nil, ec.NewError(err, "io.Copy")
	}

	if toRemote {
		var meta fileMetadata
		meta.OriginalSize = uint64(size)
		meta.CompressionType = "ZLIB"
		meta.Checksum = *stat.Checksum
		meta.ModuleType = moduleType

		remote.SetMeta(meta)
	}

	if err = destination.Close(); err != nil {
		return nil, ec.NewError(err, "destination.Close")
	}

	if err = source.Close(); err != nil {
		return nil, ec.NewError(err, "source.Close")
	}

	if !toRemote {
		stat, err = tools.StatFile(localName, true)
		if err != nil {
			return nil, ec.NewError(err, "StatFile")
		}
	}
//.........这里部分代码省略.........
开发者ID:petemoore,项目名称:runlib,代码行数:101,代码来源:mongodb.go


注:本文中的labix/org/v2/mgo.GridFile.Close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。