當前位置: 首頁>>代碼示例>>Golang>>正文


Golang File.Seek方法代碼示例

本文整理匯總了Golang中mime/multipart.File.Seek方法的典型用法代碼示例。如果您正苦於以下問題:Golang File.Seek方法的具體用法?Golang File.Seek怎麽用?Golang File.Seek使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在mime/multipart.File的用法示例。


在下文中一共展示了File.Seek方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: uploadFile

func uploadFile(req *http.Request, mpf multipart.File, hdr *multipart.FileHeader) (string, error) {

	ext, err := fileFilter(req, hdr)
	if err != nil {
		return "", err
	}
	name := getSha(mpf) + `.` + ext

	rand.Seed(time.Now().Unix())
	x := rand.Intn(3)
	fmt.Println(x)

	var pfx string
	switch x {
	case 0:
		pfx = "bob/"
	case 1:
		pfx = "james/"
	case 2:
		pfx = "stacey/"
	}

	name = pfx + name

	mpf.Seek(0, 0)

	ctx := appengine.NewContext(req)
	return name, putFile(ctx, name, mpf)
}
開發者ID:mjrmyke,項目名稱:GoLangCourse,代碼行數:29,代碼來源:file.go

示例2: generateThumbnails

func generateThumbnails(file multipart.File, userId, pathPrefix string, bucket *s3.Bucket) error {
	file.Seek(0, 0)

	img, _, err := image.Decode(file)
	if err != nil {
		log.Println("Error decoding image", err)
		return err
	}

	var buf bytes.Buffer
	for _, i := range THUMBNAIL_SIZES {
		resized := resize.Resize(uint(i), 0, img, resize.Lanczos3)
		err = jpeg.Encode(&buf, resized, nil)
		if err != nil {
			return err
		}
		path := fmt.Sprintf("%s/%d.jpg", pathPrefix, i)
		err = bucket.Put(path, buf.Bytes(), "image/jpeg", s3.PublicRead)
		if err != nil {
			return err
		}
		buf.Reset()
	}

	return err
}
開發者ID:AndrewJHart,項目名稱:irlmoji,代碼行數:26,代碼來源:handlers_upload.go

示例3: ThumbnailMultipart

/**
takes a multipart file. Does not close it. creates a thumbnail and returns the file path
*/
func ThumbnailMultipart(file multipart.File, fileName string) (string, error) {
	var (
		err      error
		img      image.Image
		thumbImg *image.NRGBA
	)
	if _, err := file.Seek(0, 0); err != nil {
		log.Printf("failed to seek to beginning of img " + err.Error())
		return "", err
	}
	var thumbPath string = "/tmp/" + time.Now().String() + fileName
	img, _, err = image.Decode(file)
	if nil != err {
		log.Printf("failed to decode img " + err.Error())
		return "", err
	}

	thumbImg = imaging.Thumbnail(img, 300, 300, imaging.Lanczos)

	out, err := os.Create(thumbPath)

	if nil != err {
		log.Printf("failed to create thumb path " + err.Error())
		return "", err
	}

	defer out.Close()

	// write new image to file
	err = jpeg.Encode(out, thumbImg, nil)

	return thumbPath, err
}
開發者ID:maleck13,項目名稱:locals-api,代碼行數:36,代碼來源:pics.go

示例4: Create

// Create takes the Profile Folder, a content-type (passed to S3), and the binary file
// data, and saves the file to S3, then saving the S3 information to the database.
// It returns the number of bytes sent to S3, for no particular reason, and any error.
func (p *Photo) Create(folder, ctype string, file multipart.File) (int64, error) {
	end, err := file.Seek(0, 2)
	if err != nil {
		return 0, err
	}
	_, err = file.Seek(0, 0)
	if err != nil {
		return 0, err
	}
	// ensure that folder actually exists in S3, here!
	b := s3Photos.Bucket(BaseBucket + "/" + folder)
	// ensure that folder exists... this doesn't error if it's already there
	err = b.PutBucket(s3.Private)
	if err != nil {
		return 0, err
	}
	err = b.PutReader(p.Href, file, end, ctype, s3.Private)
	if err != nil {
		return 0, err
	}

	err = dbmap.Insert(p)
	if err != nil {
		return 0, err
	}
	return end, nil
}
開發者ID:randallsquared,項目名稱:gochute,代碼行數:30,代碼來源:profile.go

示例5: uploadPhoto

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)
}
開發者ID:GoesToEleven,項目名稱:golang-web,代碼行數:11,代碼來源:photos.go

示例6: uploadPhoto

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)
}
開發者ID:RobertoSuarez,項目名稱:GolangTraining,代碼行數:11,代碼來源:main.go

示例7: uploadPhoto

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)
}
開發者ID:shadon09,項目名稱:CSCI130,代碼行數:12,代碼來源:photos.go

示例8: uploadPhoto

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
}
開發者ID:RobertoSuarez,項目名稱:GolangTraining,代碼行數:12,代碼來源:main.go

示例9: uploadFile

func uploadFile(req *http.Request, mpf multipart.File, hdr *multipart.FileHeader) (string, error) {

	ext, err := fileFilter(req, hdr)
	if err != nil {
		return "", err
	}
	name := getSha(mpf) + `.` + ext
	mpf.Seek(0, 0)

	ctx := appengine.NewContext(req)
	return name, putFile(ctx, name, mpf)
}
開發者ID:deeptical17,項目名稱:Deepti,代碼行數:12,代碼來源:file.go

示例10: storeFile

func storeFile(req *http.Request, mpf multipart.File, hdr *multipart.FileHeader) (string, error) {
	ext, err := fileType(req, hdr) //get type of file based on file extension

	if err != nil {
		return "", err
	}

	name := getSha(mpf) + `.` + ext //give unique name to the file
	mpf.Seek(0, 0)

	ctx := appengine.NewContext(req)
	return name, store(ctx, name, mpf)
}
開發者ID:jgtaruc,項目名稱:GoLang130,代碼行數:13,代碼來源:file.go

示例11: cropField

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
}
開發者ID:qor,項目名稱:media_library,代碼行數:51,代碼來源:callback.go

示例12: decodePNG

func decodePNG(f multipart.File) (*Image, error) {
	var image Image

	ic, err := png.DecodeConfig(f)
	_, err = f.Seek(0, 0)
	i, err := png.Decode(f)

	if err != nil {
		return nil, fmt.Errorf("Could not decode PNG file.")
	}

	image.Config = ic
	image.Image = i

	return &image, nil
}
開發者ID:danhardman,項目名稱:image-to-table,代碼行數:16,代碼來源:app.go

示例13: UploadFile

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
}
開發者ID:Kevinwenya,項目名稱:huabot-brain,代碼行數:42,代碼來源:dataset.go

示例14: judgeContentType

func judgeContentType(f multipart.File) (string, string, error) {
	f.Seek(0, 0)
	buf := make([]byte, 16)
	count, err := f.Read(buf)
	if err != nil {
		return "", "", err
	}

	if count >= 10 && string(buf[6:10]) == "JFIF" {
		return "image/jpeg", ".jpg", nil
	}
	if count >= 4 && string(buf[0:3]) == "GIF" {
		return "image/gif", ".gif", nil
	}
	if count >= 2 && string(buf[1:4]) == "PNG" {
		return "image/png", ".png", nil
	}

	return "application/octet-stream", "", nil
}
開發者ID:moznion,項目名稱:gyazo-server,代碼行數:20,代碼來源:image.go

示例15: isSupported

func isSupported(file multipart.File) (bool, string, error) {
	buff := make([]byte, 512)
	_, err := file.Read(buff)

	// Reset the file
	file.Seek(0, 0)

	if err != nil {
		return false, "", err
	}

	filetype := http.DetectContentType(buff)

	for _, i := range supportedFileTypes {
		if i == filetype {
			return true, filetype, nil
		}
	}

	log.Println("File not supported: " + filetype)
	return false, filetype, nil
}
開發者ID:kizbitz,項目名稱:webapp,代碼行數:22,代碼來源:photo.go


注:本文中的mime/multipart.File.Seek方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。